3 ms·
#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 pre
by l0b0 6y ago
#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.
- thrwyoilarticle 6y ago>barring some philosophical code which cares about "zero-ness" 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 < IMPORTANT_QUANTITY; i++) , or some variation thereof, is very idiomatic C.
- l0b0 6y agoMy point is, if you had to pull `0` in that `for` loop into a variable, would you call it `zero` or `first_index` (or something else actually relevant to the loop)? I'd certainly prefer something relevant to the loop rather than `zero`.