Friday 27 January 2017

Should constants be uppercase?

In a recent blog someone pointed out to me that boolean values true and false should be uppercase because they are constants. This made me think, usually I put constants in uppercase, however when I code true and false they are always lowercase, so it raises a question about my code style, should all constants be uppercase? If so then true, false and nil should also always be uppercase, but for me this does not look correct. Should constants not be uppercase? One reason why constants are uppercase is so they stand out from variables, but is this the only reason?

When I develop in C#, I follow the recommendation for constants not to be uppercase, so I am starting to feel that maybe this should be the same in Delphi and constants should be camel case for local constants and pascal case for public ones.

9 comments:

  1. IT_SHOULD_BE_LIKE_THIS

    ReplyDelete
  2. True and False are not real constant. They are predefined constants.

    ReplyDelete
  3. User defined constants, yes, SHOULD_BE_LIKE_THIS. But not True or False.

    ReplyDelete
    Replies
    1. Thanks for your comment. This is how I wold usually do constants, but recently some developers have said because true and false are constants they should be uppercase. I think when it comes to defining my Delphi coding standards I will stick with what I currently do, which is uppercase for user defined constants.

      Delete
  4. I don't see why there should be an exception for TRUE/FALSE ? I find it adds readability to my code, it gets so much easier to spot them.

    ReplyDelete
    Replies
    1. Do you also think NIL should be uppercase? I can understand the argument to treat all constants the same for consistency, but I do not agree it adds to the readability of code.

      Delete
  5. True/False are enumerations, not constants. And we aren't C devs, so we shouldn't use C conventions anyway.

    ReplyDelete
    Replies
    1. Hi Gerry, looking at this article:
      http://docwiki.embarcadero.com/Libraries/Seattle/en/System.Boolean
      it does appear they are enums, however looking at this one:
      http://docwiki.embarcadero.com/RADStudio/Berlin/en/Simple_Types_(Delphi)
      they appear to be 'predefined constants'. 

      Also, most developers are not just Delphi developers, the majority cross over into other languages like C#, Java, PLSQL, SQL. The reason why I refer to C# is as a comparison, if something works well in another language and is recommended for the modern IDE, then we should ask the question why do we do things differently.

      Delete
  6. IMO it does not matter and does not add anything to the comprehensibility of the code if you write constants in a special way (like all uppercase with underscores for example).

    As for how to write already existing symbols in my code: the same way as they are declared, so in this particular case: True and False

    ReplyDelete