6 ms·
Obviously not. So since you found a counter example we should discount the original advice and feel free to use numeric constants all over our code without any
by jameshart 7y ago
Obviously not. So since you found a counter example we should discount the original advice and feel free to use numeric constants all over our code without any explanation.
- happytoexplain 7y agoThis isn't a helpful response. I have the same question as the parent.
- ztjio 7y agohttps://en.wikipedia.org/wiki/Magic_number_%28programming%29 https://en.wikipedia.org/wiki/Magic_number_%28programming%29
- wool_gather 7y agoAccording to the thread, there was a size of something, and that size was used in two places. The size was specified as a literal number in both those places. The bug was caused by changing the literal in one place but not the other. One way that could have been avoided is to use a named constant for the size, so that when the literal number was changed, both uses of the name would see the new value.
- regularfry 7y agoThe reverse is also dangerous: when you have the same numeric value doing different jobs in different parts of the code. If they're literals, you've got no clue whether they should both be changed or not.
- necovek 7y agoThe idea is not to replace your constant 2048 with A_2048_NUMBER. But eg. MAX_BLOCK_SIZE in one place, and STACK_SIZE in another, even if they are the same value today. Of course, if you are repurposing the code to do something new, you need to be extra careful not to use the constant just because "it's the right number". And sure, it sometimes gets tricky, but using literals will not make it any less so.
- klyrs 7y agoDoes my balanced ternary expansion obviate the need for documentation? No. Drop a reference to the paper/website describing the algorithm you're doing. If it's, say, trig that you're doing, use clear variable names and explain how you derived the formula.
- SlowRobotAhead 7y agoOr you could go the Amazon AWS for C route and give make a define with the name of MilicsecondsPerSecond, and a separate define that makes that at 1000. If you want to go full spectrum the other way.
- necovek 7y agoLet's call that a "natural constant": while not strictly coming from nature, it is coming from a very familiar and widespread convention. There are other numbers like that (12, 24, 60 come to mind; 365 has a similar familiarity but has lots of gotchas; I am sure many developers will immediately understand 1024 too). But that's also mostly due to how they will be used. Contrast and compare: — a = b * 24 — a = b * HOURS_PER_DAY — uptime_in_hours = 24 * uptime_in_days I like the last one best, but if given a choice between the first or the second, I'd prefer to be reading the second. FWIW, I am not advocating for use of units in variable names at all times — but if you are converting between units, put them in either your variable or constant names. Basically, I interpret the rule of no-literals as a reminder to think of the readibility of any statement involving them.
- klyrs 7y ago> Basically, I interpret the rule of no-literals as a reminder to think of the readibility of any statement involving them. Now that is an excellent rule, if slightly too specific.