r/ProgrammerTIL • u/Thijmenn • Jan 02 '23
Other Magic Numbers Are Problematic (Use Explanatory Constants Instead)
Hi everyone,
In one of my recent programming seminars we had a discussion about so-called "magic numbers", which refers to the anti-pattern of using numbers directly in source code. My professor demonstrated that this habit, although subtle, can have a noticeable negative impact on the readability of your code, in addition to making it harder to refactor and detect errors while programming. Instead he proposed the use of "explanatory constants", which basically means that you assign (most) numeric literals to an adequately named constant that conveys the number's semantic meaning.
I find the topic particularly interesting because I value readable and well thought-out code (like most of us do) and thus decided to make a video on the topic:
Hopefully the presented information is useful to someone on this subreddit.
25
u/dreamer_ Jan 02 '23
Yup, magic numbers/strings/values in general are bad. However, try to avoid opposite problem as well: just because you're using a number or a string literal in your code it does not mean that you need to give it name. It's always a balance when it comes to readability.
For example:
3600orSECONDS_IN_HOUR, I would probably choose named variable (constexpr, if possible)0xfffforMASK_16BIT- I would probably go with a literal value instead.It depends on context, always.
3
u/npsimons Jan 02 '23
This is a very good idea, and a pretty low effort "win" for making code more maintainable and readable, especially these days with IDEs and editors that will complete things for you (and emacs has had
dabbrev-expandsince forever).As another commenter said, this should go for strings as well. Basically, any time you see something that could have been named semantically, it should be. And then you get into moving those to configuration files, next databases, and you can start to see the real power of this pattern . . .
Just beware that naming things is hard, hence why you get dark/anti-patterns of bad names.
9
u/CrackerBarrelJoke Jan 02 '23
Also magic strings
1
u/jha666 Jan 02 '23
Please elaborate on the concept of "adequately named constant"
4
u/chicksOut Jan 02 '23
Yup, also strongly recommend having a constants file that will serve as a reduction in duplication and ease in refactoring.
4
u/yottalogical Jan 02 '23
I think I've got the hang of this.
2
u/eterevsky Jan 02 '23
I think it depends. It makes sense to create constants for "magic numbers" in some discoverable place like a dedicated configuration file. This is especially important if the constant is reused in several places.
On the other hand, it doesn't really help to just create a constant for the sake of creating a constant. If the number is used in just one place and there is no natural place for the constant definition, it might be better to just inline it and write a comment to describe its meaning.
1
u/nikkocpp Jan 03 '23
advice, don't put all your constants in the same kitchen sink file.
-3
u/StickyCarpet Jan 02 '23
Why not use a macro definition to substitute the readable names with the hard-coded values prior to compiling, saving a few cycles on execution?