5 ms·
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 whatev
by hannasanarion 6y ago
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.
- yoz-y 6y agoIt'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.
- loa_in_ 6y ago0 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, [...]}
- cratermoon 6y agoLet 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
- function_seven 6y agoWhen 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)
- deleted 6y ago[deleted]
- inetknght 6y ago> 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;
- sandworm101 6y ago"What do you get if you multiply six by nine?" = 42
- bluesmoon 6y agountil someone runs a spellcheck on this code and changes all locations to fortytwo
- inetknght 6y agoFourtytwo: a number equaling four amounts of ten plus two singular amounts. Fortytwo: a defensive fortification with a nickname. See also Boaty McBoatface.
- deleted 6y ago[deleted]
- jiveturkey 6y agoThe 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.