Naming: Why should named constants be all uppercase in C++/Java?

前端 未结 15 1658
情歌与酒
情歌与酒 2020-12-15 16:13

I know, that for C++ and Java it is a well established naming convention, that constants should be written all uppercase, with underscores to separate words. Like this (Java

相关标签:
15条回答
  • 2020-12-15 16:26

    In C (and then C++), symbols that were defined with the preprocessor #define directive were written all in caps as a loud signal to developers that the code was not as it seemed and to not treat as a normal symbol. Constants did not initially exist in K&R C (although const was brought back from C++ later) so developers used #define instead, hence the caps.

    For some reason, this got twisted into Java as "constants are caps", hence the current misguided (IMO) convention.

    0 讨论(0)
  • 2020-12-15 16:29

    I believe in C++ it's a convention carried over from the days of using the preprocessor to #define constant values. Back then, it was done to avoid having the preprocessor trample all over your source code, as the usual conventions for C function and variable names would make them mixed case or lower case.

    From a C++ point of view, I would say that it's a bad idea to make your constants all-uppercase. I've had to debug more than one build problem because of this - remember that the C++ preprocessor does know nothing about namespaces and naming scope and will happily substitute what it thinks is appropriate even though it is rather inappropriate.

    0 讨论(0)
  • 2020-12-15 16:29

    I think uppercase constants are a bad heritage from C. The logic behind is the same as when using underscores as prefixes for private members. This is technical stuff which is already expressed by Java keywords like private or, in the case of constants, static final.

    0 讨论(0)
  • 2020-12-15 16:33

    Basically, back when people were in love with C, and C++ & Java were either new or not yet created, people used all caps for preprocessor constant names, to indicate that they weren't actually variables.

    #define INT_CONST 4
    #define STRING_CONST "Yello."
    #define FLOAT_CONST 3.6122448
    

    Considering that this was the only real way to define a true constant in C (it's still possible to change const variables if you know how), preprocessor constants like that were just seen as constants, and as such the meaning of all-caps names shifted from preprocessor constants to simply constants in general. This then carried over into later languages, becoming the standard "consts = capitals" practice.

    Other languages have their own preferred style, however (for example, C# & .NET prefer PascalCase), so it's generally best to use the preferred style if there is one.

    0 讨论(0)
  • 2020-12-15 16:36

    This is purely to help the programmer understand what he or she is looking at from a glance. It helps to know that a constant is being used so that the value won't change no matter how you refer to it.

    There isn't really a reason regarding the compiler side of things. Naming conventions are purely that, conventions, not really technical restrictions.

    0 讨论(0)
  • 2020-12-15 16:37

    It's a workaround for your development tools not being able to spot the properties of an identifier in a convenient way.

    Much like Hungarian notation.

    When your IDE gets better, you won't need any naming convention but the one that dictates that a name is comprehensive information on what an identifier means.

    Even that may evolve: why not create a programming system where you just create identifiers, and add properties to it like "brief description", "type", ... When the tool arrives that can do this in a convenient way, I'm in. "Intentional Programming" is a hint.

    0 讨论(0)
提交回复
热议问题