The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,854 other subscribers

Archive for April 16th, 2026

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 a Duration object 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

Read the rest of this entry »

Posted in Conference Topics, Conferences, Development, Event, Scripting, Software Development | Leave a Comment »