5

I always seem to come back to this question and cannot settle on a convention that I'm comfortable with or that I can concretely define: when naming a constant, how narrowly or broadly should it be named?

For instance, let's say I've written a workflow application that has workflow items (instances of the workflow) and those have, among other properties, a "stage name". When a workflow item is being initiated, it will get the initial stage name, which should be a constant. Should I name the constant, e.g., InitialStageName or should I name it LoanApplicationApproverStageName (assuming the first stage is the loan application approver" stage). In both cases, the value might be something like "LoanAppApprvr".

I guess it boils down to this: is it more important to name the constant based on how it's used (e.g. as the initial stage name) or on what it means (e.g. the stage where the loan approver performs their work)? Or should there be some marriage between the two -- possibly by having two separate constants that share the same value?

  • 4
    What language? What's the context? – Lightness Races in Orbit Nov 16 '15 at 17:45
  • Narrow? Broad? or Long, Short? – Santosh Kumar Nov 16 '15 at 19:02
  • @LightnessRacesinOrbit -- it happens to be VB.NET, but it could be any language, right? What do you mean by "what's the context", specifically? – rory.ap Nov 16 '15 at 21:18
  • @SantoshKumar -- no I'm not talking about the length of the name in characters, at all. I'm talking about the scope and breadth of the constant's definition. – rory.ap Nov 16 '15 at 21:21
17

The place where your constants are defined is also important as that information enhances the constant, without making the name longer.

Compare a class Constants that holds all the constants and is in the namespace MyApp.Global. You can have constants in there called Width, Height and Radius that really don't mean a lot being in there. These constants benefit from having clearer names, such as DefaultSquareWidth, DefaultRectangleHeight and DefaultCircleRadius.

Compare this to a constant defined in the MyApp.Shapes namespace on the Circle object. The meaning of a constant named DefaultRadius would be very obvious because of where you can find it.

Ask yourself the question if typing the constant name in a certain place will make it immediately obvious what it means. Typing MyApp.Global.Constants.Radius is a lot less obvious than MyApp.Shapes.Circle.DefaultRadius or MyApp.Global.Constants.DefaultCircleShapeRadius.

  • All good points for enhancing the answer but for me the main takeaway was "Will the constant name...make it immediately obvious what it means". That "is" the answer to the OP's question. – Dunk Nov 18 '15 at 22:35
5

The broader the scope the longer the name. This is done because the further you get from a definition the less clear its intent becomes.

An example of the absurd narrow end is a method-level constant (or variable). It could be a single character, although I tend to stick with the type name as a minimim personally.

On the far end you have a global constant. It should be very detailed in its name.

Names of constants (and variables in general) should always be about what they are (noun or adjective-noun phrases) rather than what they do.

2

I would use the LoanApplicationApproverStageName; it conveys more meaning when used:

setInitialState(LoanApplicationApproverStageName);

as opposed to:

setInitialState(InitialStageName);

Looking at the second example, you have no understanding of what the initial state is being set as.

Additionally, You might be tempted to use this value ("LoanAppApprvr") in other locations where you intend to refer to LoanApplicationApproverStageName - the stage, not it's place as the initial stage.

  • 1
    The question is about constants specifically, not arguments. – JDT Nov 16 '15 at 16:30
  • 2
    @JDT: How do you use constants if not as arguments? – Lightness Races in Orbit Nov 16 '15 at 17:46
  • You declare a constant somewhere and give it a name at that point. You can use the constant at some point in a method call as an argument, or to assign to a variable, but I believe the question is about the name of the constant, not the name of the variables you assign the constant to. – JDT Nov 16 '15 at 18:08
  • @JDT I believe the question to be about the name of the constant variable. - In both cases, the value might be something like "LoanAppApprvr" – tylerwal Nov 16 '15 at 18:21
  • 1
    The name setInitialState is terrible. It should probably be something like setStageName. – David Conrad Nov 16 '15 at 18:25
0

Your application will always have an initial stage, but will not necessarily always have a loan application approver stage or, more likely, it may not always remain the initial stage.

For this reason, I would name the constant to reflect what it represents (the name of the initial stage) rather than on what it currently means

  • 2
    Off topic questions happen a lot here. Trying to answer them only encourages the behavior. Recommended reading: Should one advise on off-topic questions?. – Adam Zuckerman Nov 17 '15 at 1:58
  • At the risk of losing all of my reputation points, what a load of rubbish. Honestly, you've wasted more time downvoting everyone who has answered and explaining why you don't want to waste time than you would have spent just answering the question. Here's a thought. If you think a question about design and naming is "off topic" despite that there are explicit meta-tags for this just don't answer it. – Michael J. Nov 17 '15 at 19:18
  • 2
    I didn't downvote your answer. While we did (at one time several years ago) allow the questions related to naming, we (this community) no longer do. Here are the current reasons we don't do this: On the troubles of naming and terminology. If you have further concerns, please feel free to discuss this with a moderator or post a question on our meta site. – Adam Zuckerman Nov 17 '15 at 19:33
  • Yet the site offers a reversal badge which blatantly encourages answering bad questions. Most questions don't get a negative vote unless they're off-topic, too broad, etc. Perhaps more questions should be downvoted for just being uninteresting or something like that, since it's rare to find a question with negative votes which isn't asking to be closed or flagged. To me the notion of a good answer should be decoupled from a good question. There can be a great answer to a bad question, or vice versa. An answer should only be downvoted for being a bad answer. – user204677 Nov 19 '15 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.