Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Cool to see Facebook release code in C! I clicked the "C" tag in the blog post, and this is the only post referencing C. Heh. Anyway, finally code from an Internet Giant in a language I care about enough to go and take a look at.

It seems ... smallish, which was a pleasant surprise. The core Yoga/ folder contains six files which is certainly fewer than I expected.

Didn't have time to do a full read-through, but one thing that I couldn't ignore is the use of a pointer-hiding typedef for the core layout node:

    typedef struct YGNode *YGNodeRef;
I'm really opposed to "hiding the asterisk" in C, since whether or not a thing is a pointer typically matters, and code becomes harder to read when you need to think about this more.

Even stranger, though, is that then most function protypes look like this:

    void YGNodeMarkDirty(const YGNodeRef node);
So, you have a function called "mark" which really sounds like a mutating, modifying, operation. But it's declared to take a constant node reference!

Peeking at the code, what it does boils down to:

    if (!node->isDirty) {
      node->isDirty = true;
      ...
    }
So it really is modifying, there's no trickery involved (like having node be a handle or indirect reference). It then recurses upwards through the chain of parent nodes, like you'd expect.

This works since the "const" here doesn't apply to the pointed-at object (it's distinct from the un-typedef:ed version "const struct YGNode * node"), it applies to the reference.

So the code jumps through these hoops and adds a const to the external interface, which doesn't matter, all it does is say "yeah, this function won't re-assign the reference variable to point at something else". Which, in my opinion, is not very useful information, as opposed to "this function doesn't write to the object you pass in" which you'd get with the non-asterisked version.

Can anyone shed some light on why one would do this?



I'd guess that its because changing the value of the *Pointer would actually be a bug, and const args are used to catch these kinds of bugs...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: