简体   繁体   中英

Since I'm not supposed to have magic numbers in my code, should I #define ZERO 0

I want to optimize readability of my code. I don't want people reading my code to be confused when they see 0 . What 0 means could be extremely ambiguous. For instance, if I had a statement like if (myVector.size() > 0) , that might be confusing because, after all, what is zero supposed to mean in this context? I'm wondering if I should put

#define ZERO 0

at the top of my code.

You miss the point here. Magic is not about using a number - it's about being able to understand why this particular number is used. When you compare size() with 0, your intent is pretty obvious. Have you compared it with any other number - like 42 , for example - you'd have to explain why this particular number has been chosen.

As for this particular change ( 0 => ZERO ), it'll just introduce tautology in your code.

0 used in comparisons or initialization of counters, indexes, accumulators, arithmetic expressions... is perfectly understandable as such.

It makes sense to create an identifier in situations where the exact value is irrelevant and could be changed without doing any harm, and having a interpretation specific to the context, like FREE_CLUSTER or NO_ERROR.

I'm wondering if I should put
#define ZERO 0
at the top of my code.

As long you're asking just for the 0 magic number : NO, Don't do this! You should simply use

if (myVector.size()) // ...

which reproduces behavior of your statement equivalently.

It is guaranteed in and language, that 0 evaluates to false and any other value evaluates to true for evaluation of conditional expressions.

No, don't do that. It's a bad choice and utterly unnecessary. In this specific case, your code is pretty obvious:

if (myVector.size() > 0)

No one can get confused with that. But if it was something like:

if (myVector.size() > 23*66)

Then that would be confusing, because someone reading your code would be left wondering what the hell 23*66 means. This is a magic number. In a context like this, a #define could help, accompanied by a comment, for example:

/* We have at most 23 files with 66 records each */
#define MAX_RECORDS_NO 23*66

And then you'd have:

if (myVector.size() > MAX_RECORDS_NO)

That is the kind of situation where you can and surely want to avoid magic numbers. Note that the #define is a description of what that number means, if I were to follow your logic, I would have chosen this instead:

#define TWENTY_THREE_TIMES_SIXTY_SIX 23*66

Now, that's not very helpful, is it? #define ZERO 0 totally misses the point: it adds nothing to someone reading your code, it is excessively verbose, and it's useless because it is not very likely that the value of 0 will change. What if all of a sudden you wanted to compare myVector.size() to another number? You'd have to replace the constant name because defining ZERO to something else other than 0 is asking for trouble. So, after all, you gained nothing.

You don't need to do so.

The original code is already straightforward, as you can see.

Also, just think about it. If you define zero as 0, are you going to change zero to some other value other than 0? The answer is no, of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM