Always use explicit, well-defined representations for undefined, uninitialized, or invalid states instead of magic values or allowing undefined behavior to propagate silently. Create named constants or enum values for undefined states, and make conscious decisions about whether to crash, return defaults, or handle undefined values gracefully.
Always use explicit, well-defined representations for undefined, uninitialized, or invalid states instead of magic values or allowing undefined behavior to propagate silently. Create named constants or enum values for undefined states, and make conscious decisions about whether to crash, return defaults, or handle undefined values gracefully.
Key practices:
0.0f
, reference defined constants like YGDefaultFlexGrow
to prevent bugs and improve clarityYGValueUndefined
rather than raw undefined values or magic numbers-1
or other magic numbers to represent invalid states, consider explicit enum values, though weigh API design implicationsExample of good practice:
// Good: Named constant for undefined state
static const YGValue YGValueUndefined = {YGUndefined, YGUnitUndefined};
node->style.flexBasis = YGValueUndefined;
// Good: Explicit handling decision
if (YGFloatIsUndefined(baseline)) {
// Crash fast - undefined baseline indicates a bug
YGAssert(false, "Baseline function must not return undefined");
}
// Avoid: Magic values
node->style.flexBasis.value = YGUndefined; // Raw undefined
layout->cached_layout.width_measure_mode = (css_measure_mode_t)-1; // Magic -1
This approach prevents bugs by making undefined states visible and intentional, while forcing explicit decisions about how to handle edge cases.
Enter the URL of a public GitHub repository