Hacker News new | ask | show | jobs
by Stratoscope 3110 days ago
> #2 is referred to as an unsafe macro because of the multiple evaluation of its arguments. In some cases this is fine but it may need to be documented that the macro is unsafe for future users.

In the early days of C programming, a naming convention was adopted to warn about this. If you wrote a macro that might have multiple evaluation of its arguments, you named it in UPPERCASE as a warning:

    #define MIN( a, b )  ( (a) < (b) ? (a) : (b) )
This practice spread into pretty much all macro definitions. If you defined a macro of any sort, you gave it an uppercase name, even if the macro didn't take any arguments at all and therefore warning about multiple evaluation was moot:

    #define MAX_SIGNED_SHORT 32767
From there the notion spread into other languages that all constants ought to have UPPER_CASE_NAMES. So this is why you see JavaScript code like:

    const TEMP_ITEM_LIMIT = 100;
instead of something easier on the eyes like:

    const tempItemLimit = 100;
2 comments

Although I get that the original intent was different. I actually find making all constants uppercase a useful practice. When reading the code, it's easy to see what is a local variable and what is not, or at least not supposed to be.
I really find TEMP_ITEM_LIMIT easier to read than tempItemLimit.