Hacker News new | past | comments | ask | show | jobs | submit login

Why is the reason for doing this obvious? I can't see any benefit.

I get why you would prefer to name constants for their use, like `numIterations=3` or whatever, but renaming every integer seems senseless.




When formulating the rule, they didn't understand the point of it. They think "naked constants === bad" is equivalent to "no numbers in source".

Which, of course, is wrong. They're not the same. Same bastardization happened to Hungarian Notation, IIRC. It was meant to add some information to variables, like `iterN` for a counter, or `lenX` for a length, but someone somewhere decided that you were meant to prefix with the most primitive of type info, so you get stupid shit like `intN` and `intX`, and aren't much more informed. (Especially with an IDE showing the type info)


It's "obvious" if you take into account the corporate culture I guess. The rule of 'no magic numbers' is generally good, but then one has to remember that some numbers (like 0 and 1) are not magic.


0 and 1 can be magic if the context depends on them being arbitrarily 0 or 1. eg.

// this byte is unused and always 0

#define UNUSED_CONSTANT_FIELD_VALUE 0

bytes = {HEADER, UNUSED_CONSTANT_FIELD_VALUE, [...]}


Let me tell you about the system where null and 0 are treated as equal because in some programming languages will coerce null to 0. It just so happens that for an important business type in this system:

the range of the type is the non-negative integers up to (2^32)-1

semantically, the value cannot be null

when the value is 0, the code treats it as a special case

null is treated as 0


The use case I'm familiar with is when the fn you're calling requires a pointer. Common for ioctl(..., &one) calls.

If you look later in the code that's exactly what is happening here.


> but renaming every integer seems senseless.

When the integer itself is self-explainatory but you can't just assign the integer as a name.

For example:

   constexpr int 42 = 42; // compiler error
So instead:

   constexpr int fourtytwo = 42; // yay!

However the obviousness of what 42 means is debatable.

   int universe = fourtytwo; // why?
   int sum = fourtytwo; // sum... of what?
   int magic = fourtytwo / 7;


until someone runs a spellcheck on this code and changes all locations to fortytwo


Fourtytwo: a number equaling four amounts of ten plus two singular amounts.

Fortytwo: a defensive fortification with a nickname. See also Boaty McBoatface.


"What do you get if you multiply six by nine?" = 42




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

Search: