Earlier quoted context omitted.
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.
> 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;
`three = 1` in the Linux sourcecode (2014)
91–100 of 105 posts
Re: `three = 1` in the Linux sourcecode (2014)
#92705 /* 706 * Iterate through the groups which hold BACKUP superblock/GDT copies in an 707 * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before 708 * calling this for the first time. In a sparse filesystem it will be the 709 * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ... 710 * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ... 711 */ 712 static unsigned…
Yeah, maybe calling them pow3, pow5, pow7 would be a bit clearer? But a 10s search for a comment cleared it up, so I don’t really see the problem...
Re: `three = 1` in the Linux sourcecode (2014)
#93But then, you should probably do something such as "#define CLOCK_DIVISOR_3 0x1" instead of simply "int three = 1".
Re: `three = 1` in the Linux sourcecode (2014)
#94Many years ago in an age of klocs and flowcharts, at a large three-letter computer company, there were iron-clad coding rules that must be obeyed, no questions allowed. In general you could see their reasoning, but such bureaucratic reasoning doesn't pay off. One example of this was that all numeric values used in a program must be factored out as symbolic constants. The reason for doing this is obvious, but it faile…
#define zero 0
is just bad programming, barring some philosophical code which cares about "zero-ness". Names need to reflect what they represent, not their precise values. Are we looking up the first index? Then how about #define first_index 0
? Are we summing up values which are coerced to zero when empty? Then how about #define empty_value_integer 0
or #define additive_identity 0
?Yes, that means there might be more than one variable with a value of zero, and that's completely fine since we now have several megabytes available for both source code and for compilation.
To be clear, I'm not endorsing the rule, just saying that there are more productive ways of dealing with it.
Re: `three = 1` in the Linux sourcecode (2014)
#95Earlier quoted context omitted.
'zero' and '0' are the same label, for the digit 0. There is no advantage to re-defining it, in fact, it does the opposite. It's a good practice to define labels for magic numbers to promote more maintainable code through the DRY / single source of truth principle, document the magic number through the label name, and improve code archeology by enabling searching by label. These principles hold when the label given a…
Heh, try for #define 0 0
> #define 0 0
That actually works on some implementations (the preprocessor grabs a token without checking that it's a identifier). I've seen: #define $ SOME_LINE_NOISE
#define && HACKY_GARBAGE
x = &&(y $ z);
as well.Re: `three = 1` in the Linux sourcecode (2014)
#96Earlier quoted context omitted.
I see a problem: https://github.com/torvalds/linux/blob/master/fs/ext4/resize... Upon a quick code review, these lines look buggy: unsigned three = 1; unsigned five = 5; unsigned seven = 7; Without digging deeper, the reader of this code thinks "The first line surely must be a bug, right???"
unsigned three = 1; To me, that is a 20 foot tall neon orange flashing Chesterton's fence. I think, "surely there is a very strong reason for this." Maybe not a good reason, but definitely a compelling one.
It's not a Chesterton's fence, though. The whole point of Chesterton's fence is that whoever put up the fence that you want to take down couldn't be bothered to add signage explaining why the fence was present, thus either it isn't important to keep the fence up long term, or they're a idiot, in which case there's no reason to belive they were correct in deciding that having a fence there was useful in the first place.
With this fence, if you walk a few meters to the side (read, "select 'ext4_list_backups' and hit ^F and ^G a few times); et viola, there's your signs.
Re: `three = 1` in the Linux sourcecode (2014)
#97Many years ago in an age of klocs and flowcharts, at a large three-letter computer company, there were iron-clad coding rules that must be obeyed, no questions allowed. In general you could see their reasoning, but such bureaucratic reasoning doesn't pay off. One example of this was that all numeric values used in a program must be factored out as symbolic constants. The reason for doing this is obvious, but it faile…
An application I touched a few weeks ago has a DB column named 'type' with values 1,2,3. So on to the source code we go: enum RecordType {TypeOne(1),TypeTwo(2),TypeThree(3); RecordType(int dbValue) ...} As it happens, I know an end user of this particular beast, so I show her some record IDs of each type and ask in what way they differ. She looks a few seconds, then says: 'This is clearly a type one record, the next…
You don't get screwed because you called something apple to start with, but it now represents a race car
Re: `three = 1` in the Linux sourcecode (2014)
#98Earlier quoted context omitted.
> 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
Fortytwo: a defensive fortification with a nickname. See also Boaty McBoatface.
Re: `three = 1` in the Linux sourcecode (2014)
#99Re: `three = 1` in the Linux sourcecode (2014)
#100Many years ago in an age of klocs and flowcharts, at a large three-letter computer company, there were iron-clad coding rules that must be obeyed, no questions allowed. In general you could see their reasoning, but such bureaucratic reasoning doesn't pay off. One example of this was that all numeric values used in a program must be factored out as symbolic constants. The reason for doing this is obvious, but it faile…
#define zero 0 is just bad programming, barring some philosophical code which cares about "zero-ness". Names need to reflect what they represent , not their precise values. Are we looking up the first index? Then how about #define first_index 0 ? Are we summing up values which are coerced to zero when empty? Then how about #define empty_value_integer 0 or #define additive_identity 0 ? Yes, that means there might be m…
But that is the intrinsic meaning OP was talking about. Plenty of mathmatics deals with 0 or 1 as important constants. If you have a function that mods by a parameter, you need error handling for zero. Calling it value_that_causes_undefined_behaviour isn't going to improve code clarity. Beyond that;
for (i = 0; i , or some variation thereof, is very idiomatic C.