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.
View all comments
Show parent comment
5
u/dreamer_ Jan 02 '23 edited Jan 02 '23
And I agree with them - my example was not perfect, as introduction of constants for time conversions "encourages" people to add even more named constants ("if we have
DAY_IN_MILLISthen why notDAY_IN_SandDAY_IN_MICROS? Then maybeWEEK_IN_S,WEEK_IN_MILLIS,WEEK_IN_MICROS", etc, etc - that would work against code readability). Also, with24 * 60 * 60 * 1000you really give a lot of context already. At least enough for casual reader to understand what's being computed here.But if, for whatever reason, at least one of those constants does not refer to time then I would fall back on named variable again.
In fact, here's some of my old code where I mixed both styles trying to maximise readability of my code:
https://github.com/dosbox-staging/dosbox-staging/commit/74b678a92fc1de0d9fc4e888d28fa152282d9724#diff-393a03403908ba121aeea0bb78f78cd01ebfbbb17c5dd812c5b5633d76cfacb5R1403-R1408