Remember: languages automatically evaluate const expressions
Posted by jpluimers on 2026/04/16
The first tweet below reminded me that few people seem to realise that const expressions are evaluated by the compiler/interpreter into the actual const value. Often this is called constant folding (though that can happen outside constant definitions too!)
Truckloads of source code I have come across in all kinds of languages where people put the calculated values in the expression like described here:
[Wayback/Archive] Kevlin Henney on Twitter: “For example:
const int secondsInDay = 24 * 60 * 60;There is no need to calculate it yourself:const int secondsInDay = 86400;Or, related to what I’ve just seen:const int secondsInDay = 86400; // 24 * 60 * 60“.
In languages that support rich enough types, you can even pass a typed constant like timespan, duration or period around:
[Wayback/Archive] David Kerr on Twitter: “@KevlinHenney The general point is well made of choir course. In
java, etc you can pass aDurationobject around, no need to interpret an in. Type safety, self documenting.”
My recommendation is to use an expression like the first and maybe document the calculated value (for instance for ease of bug hunting) like here
const int secondsInDay = 24 * 60 * 60; // 86400
This makes the code much more intuitive to read, and as negligible influence on speed: it is either in the (one time) compile pass, or in the (often also one time) interpretation pass. In fact, many popular scripting languages gradually compile to intermediate (byte) code while executing the script:
[Wayback/Archive] Barney Laurance on Twitter: “@KevlinHenney I’m pretty sure is also true for a language like PHP which people think of as not compiled but actually just has a very fast compiler that doesn’t do much work beyond translating source to byte code.”
That time is much faster than fixing bugs because of wrong constant values: [Wayback/Archive] Jeroen Wiert Pluimers @wiert@mastodon.social on Twitter: “@KevlinHenney The first in any programming language all the time since I could program. The (often perceived) overhead is far less than the time to fix mistakes. Especially for me: since I have a hard time remembering constants over 3-4 digits in length I hardly can verify long consts at all.”
Now you say “I don’t make these mistakes”. You might not, but others do and likely you will be maintaining this code:
- [Wayback/Archive] Nat Pryce on Twitter: “@KevlinHenney I’ve seen code like:
// 15 * 1000ms * 60 secs = 15 minslong endTime = currentTime + 15 * 6000; If only they’d let the compiler do the calculation!” - [Wayback/Archive] Kevlin Henney on Twitter: “@natpryce Ah yes, the right comment for the wrong code, the less well-known cousin of the more popular wrong comment for the right code.”
Remember:
- often “others” are past incarnations of yourself
- like off-by-one errors, off-by-factor-10 errors are common logic-errors too
- fewer people than you think realise the compiler/interpreter actually does constant folding
One of the responses indicated a learning opportunity like [Wayback/Archive] Matthias 💉💉💉 on Twitter: “@KevlinHenney 🤯TIL! “ learned that the constant value 3600 is both evaluated to by the [Wayback/Archive] javac – Java programming language compiler compiled constant expressions 3600 and 60 * 60 when dumped with [Wayback/Archive] javap – The Java Class File Disassembler:
--jeroen






Leave a comment