Is a constant name related to its current value an anti-pattern? [closed] The Next CEO of Stack OverflowUsage of magic strings/numbersThe difference between best practices and common sense?Finding a definition for this anti-patternWhat is this (anti?)pattern called? (or how to describe it)Is there a programming pattern, design pattern, or naming convention for moving data between related schemas?Constructor-only subclasses: Is this an anti-pattern?Way to organize repetitive text in JavaSQL Query and Java Constant Abuse?Should I rename variables that are already constants in my own library?Does this anti-pattern have a name?Should I avoid creating a variable with shorter name for a constant?
Term for the "extreme-extension" version of a straw man fallacy?
How long to clear the 'suck zone' of a turbofan after start is initiated?
Why did we only see the N-1 starfighters in one film?
Inappropriate reference requests from Journal reviewers
The King's new dress
Was a professor correct to chastise me for writing "Prof. X" rather than "Professor X"?
Rotate a column
Customer Requests (Sometimes) Drive Me Bonkers!
Why Were Madagascar and New Zealand Discovered So Late?
Why does GHC infer a monomorphic type here, even with MonomorphismRestriction disabled?
What makes a siege story/plot interesting?
Explicit solution of a Hamiltonian system
MAZDA 3 2006 (UK) - poor acceleration then takes off at 3250 revs
Why do remote companies require working in the US?
How do I get the green key off the shelf in the Dobby level of Lego Harry Potter 2?
How to write papers efficiently when English isn't my first language?
Visit to the USA with ESTA approved before trip to Iran
Why does standard notation not preserve intervals (visually)
Removing read access from a file
How to be diplomatic in refusing to write code that breaches the privacy of our users
What does "Its cash flow is deeply negative" mean?
Return the Closest Prime Number
If I blow insulation everywhere in my attic except the door trap, will heat escape through it?
How to Reset Passwords on Multiple Websites Easily?
Is a constant name related to its current value an anti-pattern? [closed]
The Next CEO of Stack OverflowUsage of magic strings/numbersThe difference between best practices and common sense?Finding a definition for this anti-patternWhat is this (anti?)pattern called? (or how to describe it)Is there a programming pattern, design pattern, or naming convention for moving data between related schemas?Constructor-only subclasses: Is this an anti-pattern?Way to organize repetitive text in JavaSQL Query and Java Constant Abuse?Should I rename variables that are already constants in my own library?Does this anti-pattern have a name?Should I avoid creating a variable with shorter name for a constant?
For example, suppose I have a string constant like this:
const TITLEBAR_MESSAGE="Welcome back, %USERNAME%!";
I think it is more readable when it is named as
const WELCOME_BACK_USERNAME="Welcome back, %USERNAME%!";
because I don't need to move to the definition of TITLEBAR_MESSAGE to see what is the string context, also I don't need to remember that "TITLEBAR_MESSAGE" is mapped to "Welcome back,xxx" currently.
I know it would less maintainable when the string is changed because I also need to change its name. But code is read more than write, right? I think the latter naming is more straightforward so that other team members don't need to know what "TITLEBAR" is.
Similar cases also appears for colour constants:
const COLOR_SOME_UI=[32,0,0];
Which I think:
const COLOR_DEEP_RED=[32,0,0];
is more straightforward (when there is just a few color only) as other team member doesn't need to know what "SOME_UI" is. So my question is, is a constant name related to its current value an anti-pattern?
coding-style naming anti-patterns constants
closed as too broad by gnat, jwenting, Laiv, Andy, Blrfl Mar 18 at 10:27
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
For example, suppose I have a string constant like this:
const TITLEBAR_MESSAGE="Welcome back, %USERNAME%!";
I think it is more readable when it is named as
const WELCOME_BACK_USERNAME="Welcome back, %USERNAME%!";
because I don't need to move to the definition of TITLEBAR_MESSAGE to see what is the string context, also I don't need to remember that "TITLEBAR_MESSAGE" is mapped to "Welcome back,xxx" currently.
I know it would less maintainable when the string is changed because I also need to change its name. But code is read more than write, right? I think the latter naming is more straightforward so that other team members don't need to know what "TITLEBAR" is.
Similar cases also appears for colour constants:
const COLOR_SOME_UI=[32,0,0];
Which I think:
const COLOR_DEEP_RED=[32,0,0];
is more straightforward (when there is just a few color only) as other team member doesn't need to know what "SOME_UI" is. So my question is, is a constant name related to its current value an anti-pattern?
coding-style naming anti-patterns constants
closed as too broad by gnat, jwenting, Laiv, Andy, Blrfl Mar 18 at 10:27
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
6
Way too broad. Depends on context. In a list of colours, calling a constant DEEP_RED makes perfect sense e.g., in a list of colour values used in a user interface, you'd name them for the things being coloured, like SCROLLBAR_BACKGROUND_COLOUR = Colours.DEEP_RED;
– jwenting
Mar 18 at 7:30
That is an anti-pattern. Two problems, first: Rate of change. The name of the constant within the code will change less frequently then the contents of it. That is the whole point of abstracting it, to allow simple and global change. Second: level of abstraction. As a benefit of putting the constant behind a name, the name can mean something. Maybe Title_On_Error vs Title_On_Success. Try and make the name say something important about how/when it is used.
– Kain0_0
Mar 18 at 7:31
Hmm... why would team members who don't need to know about the title bar ever see that string constant in their code? If you useTITLEBAR_MESSAGEfor strings that are not actually title bar messages, then it's not wonder that the practice feels wrong to you.
– Christian Hackl
Mar 18 at 7:45
Both are readable. Which one do you think is more meaningful for the reader in each case? I would dare to say that it depends on the place and the moment you use one or another. Anyways, you could use both altogether as it was already suggested.
– Laiv
Mar 18 at 7:46
Possible duplicate of Usage of magic strings/numbers
– Blrfl
Mar 18 at 10:27
add a comment |
For example, suppose I have a string constant like this:
const TITLEBAR_MESSAGE="Welcome back, %USERNAME%!";
I think it is more readable when it is named as
const WELCOME_BACK_USERNAME="Welcome back, %USERNAME%!";
because I don't need to move to the definition of TITLEBAR_MESSAGE to see what is the string context, also I don't need to remember that "TITLEBAR_MESSAGE" is mapped to "Welcome back,xxx" currently.
I know it would less maintainable when the string is changed because I also need to change its name. But code is read more than write, right? I think the latter naming is more straightforward so that other team members don't need to know what "TITLEBAR" is.
Similar cases also appears for colour constants:
const COLOR_SOME_UI=[32,0,0];
Which I think:
const COLOR_DEEP_RED=[32,0,0];
is more straightforward (when there is just a few color only) as other team member doesn't need to know what "SOME_UI" is. So my question is, is a constant name related to its current value an anti-pattern?
coding-style naming anti-patterns constants
For example, suppose I have a string constant like this:
const TITLEBAR_MESSAGE="Welcome back, %USERNAME%!";
I think it is more readable when it is named as
const WELCOME_BACK_USERNAME="Welcome back, %USERNAME%!";
because I don't need to move to the definition of TITLEBAR_MESSAGE to see what is the string context, also I don't need to remember that "TITLEBAR_MESSAGE" is mapped to "Welcome back,xxx" currently.
I know it would less maintainable when the string is changed because I also need to change its name. But code is read more than write, right? I think the latter naming is more straightforward so that other team members don't need to know what "TITLEBAR" is.
Similar cases also appears for colour constants:
const COLOR_SOME_UI=[32,0,0];
Which I think:
const COLOR_DEEP_RED=[32,0,0];
is more straightforward (when there is just a few color only) as other team member doesn't need to know what "SOME_UI" is. So my question is, is a constant name related to its current value an anti-pattern?
coding-style naming anti-patterns constants
coding-style naming anti-patterns constants
asked Mar 18 at 7:17
mmmaaammmaaa
2,86841926
2,86841926
closed as too broad by gnat, jwenting, Laiv, Andy, Blrfl Mar 18 at 10:27
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by gnat, jwenting, Laiv, Andy, Blrfl Mar 18 at 10:27
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
6
Way too broad. Depends on context. In a list of colours, calling a constant DEEP_RED makes perfect sense e.g., in a list of colour values used in a user interface, you'd name them for the things being coloured, like SCROLLBAR_BACKGROUND_COLOUR = Colours.DEEP_RED;
– jwenting
Mar 18 at 7:30
That is an anti-pattern. Two problems, first: Rate of change. The name of the constant within the code will change less frequently then the contents of it. That is the whole point of abstracting it, to allow simple and global change. Second: level of abstraction. As a benefit of putting the constant behind a name, the name can mean something. Maybe Title_On_Error vs Title_On_Success. Try and make the name say something important about how/when it is used.
– Kain0_0
Mar 18 at 7:31
Hmm... why would team members who don't need to know about the title bar ever see that string constant in their code? If you useTITLEBAR_MESSAGEfor strings that are not actually title bar messages, then it's not wonder that the practice feels wrong to you.
– Christian Hackl
Mar 18 at 7:45
Both are readable. Which one do you think is more meaningful for the reader in each case? I would dare to say that it depends on the place and the moment you use one or another. Anyways, you could use both altogether as it was already suggested.
– Laiv
Mar 18 at 7:46
Possible duplicate of Usage of magic strings/numbers
– Blrfl
Mar 18 at 10:27
add a comment |
6
Way too broad. Depends on context. In a list of colours, calling a constant DEEP_RED makes perfect sense e.g., in a list of colour values used in a user interface, you'd name them for the things being coloured, like SCROLLBAR_BACKGROUND_COLOUR = Colours.DEEP_RED;
– jwenting
Mar 18 at 7:30
That is an anti-pattern. Two problems, first: Rate of change. The name of the constant within the code will change less frequently then the contents of it. That is the whole point of abstracting it, to allow simple and global change. Second: level of abstraction. As a benefit of putting the constant behind a name, the name can mean something. Maybe Title_On_Error vs Title_On_Success. Try and make the name say something important about how/when it is used.
– Kain0_0
Mar 18 at 7:31
Hmm... why would team members who don't need to know about the title bar ever see that string constant in their code? If you useTITLEBAR_MESSAGEfor strings that are not actually title bar messages, then it's not wonder that the practice feels wrong to you.
– Christian Hackl
Mar 18 at 7:45
Both are readable. Which one do you think is more meaningful for the reader in each case? I would dare to say that it depends on the place and the moment you use one or another. Anyways, you could use both altogether as it was already suggested.
– Laiv
Mar 18 at 7:46
Possible duplicate of Usage of magic strings/numbers
– Blrfl
Mar 18 at 10:27
6
6
Way too broad. Depends on context. In a list of colours, calling a constant DEEP_RED makes perfect sense e.g., in a list of colour values used in a user interface, you'd name them for the things being coloured, like SCROLLBAR_BACKGROUND_COLOUR = Colours.DEEP_RED;
– jwenting
Mar 18 at 7:30
Way too broad. Depends on context. In a list of colours, calling a constant DEEP_RED makes perfect sense e.g., in a list of colour values used in a user interface, you'd name them for the things being coloured, like SCROLLBAR_BACKGROUND_COLOUR = Colours.DEEP_RED;
– jwenting
Mar 18 at 7:30
That is an anti-pattern. Two problems, first: Rate of change. The name of the constant within the code will change less frequently then the contents of it. That is the whole point of abstracting it, to allow simple and global change. Second: level of abstraction. As a benefit of putting the constant behind a name, the name can mean something. Maybe Title_On_Error vs Title_On_Success. Try and make the name say something important about how/when it is used.
– Kain0_0
Mar 18 at 7:31
That is an anti-pattern. Two problems, first: Rate of change. The name of the constant within the code will change less frequently then the contents of it. That is the whole point of abstracting it, to allow simple and global change. Second: level of abstraction. As a benefit of putting the constant behind a name, the name can mean something. Maybe Title_On_Error vs Title_On_Success. Try and make the name say something important about how/when it is used.
– Kain0_0
Mar 18 at 7:31
Hmm... why would team members who don't need to know about the title bar ever see that string constant in their code? If you use
TITLEBAR_MESSAGE for strings that are not actually title bar messages, then it's not wonder that the practice feels wrong to you.– Christian Hackl
Mar 18 at 7:45
Hmm... why would team members who don't need to know about the title bar ever see that string constant in their code? If you use
TITLEBAR_MESSAGE for strings that are not actually title bar messages, then it's not wonder that the practice feels wrong to you.– Christian Hackl
Mar 18 at 7:45
Both are readable. Which one do you think is more meaningful for the reader in each case? I would dare to say that it depends on the place and the moment you use one or another. Anyways, you could use both altogether as it was already suggested.
– Laiv
Mar 18 at 7:46
Both are readable. Which one do you think is more meaningful for the reader in each case? I would dare to say that it depends on the place and the moment you use one or another. Anyways, you could use both altogether as it was already suggested.
– Laiv
Mar 18 at 7:46
Possible duplicate of Usage of magic strings/numbers
– Blrfl
Mar 18 at 10:27
Possible duplicate of Usage of magic strings/numbers
– Blrfl
Mar 18 at 10:27
add a comment |
4 Answers
4
active
oldest
votes
It entirely depends on whether it is a constant and won't likely change or if it is a constant which is symbolic or representative of something else and therefore may change.
An example of a constant which won't likely change is:
const PI=3.1415926
const RED=[255,0,0]
In this case, call it as it is. It represents the value of PI, not some arbitrary float.
An example of a constant which is symbolic or representative of something else is:
const WELCOME_MESSAGE="welcome.user"
const CONNECT_URL="jdbc:sqlserver://server:1433;databaseName=AdventureWorks"
This is not the message itself, but rather a key for looking up the message. If later you should decide that the key would be better written "user.welcome", you can change the constant in your program, and WELCOME_MESSAGE is equally valid and therefore doesn't need to be renamed as it is always used in the same way.
Same can be said for the connect URL. It will be the same throughout your program, but the value is also very likely to change in the future. So calling it CONNECT_ADVENTUREWORKS would be wrong. The name of the database may not change, but it is still not best practice since the name is a reflection of its value, which again, would be okay if the value doesn't change. Otherwise the name should be more towards how it will be used in your program. In this case it is your connection URL to the database (whatever it may be), and should be called thusly.
It's always a constant, just a less constant constant. :)
add a comment |
The idea of defining constants is that you give a value a certain semantic meaning other than the value itself (the color of headers, a circumference-square ratio, etc.) Thus, in case the value is later changed, you only have to change it in one place, and the whole application or library reflects this change.
With this in mind, it's counterproductive to bind the constant name to its value instead of its meaning for the same reason you shouldn't tell how a function does something instead of what it does: if you change the contents enough, you need to rename the constant. Furthermore, if I see COLOR_DEEP_RED in the source code, it tells little about what it is, but COLOR_PRIMARY tells me that it's the primary color of the style palette.
I don't know if it's actually considered an antipattern itself, but I clearly see reasons not to name constants by their value, but by their meaning.
add a comment |
Name your string constants for their content.
const WELCOME_MESSAGE="Welcome!";
const WELCOME_BACK_MESSAGE="Welcome back!";
That will come in handy when you're looking at a localized string.
const WELCOME_MESSAGE="Wilkommen!';
const WELCOME_MESSAGE="¡Bienvenida!';
Name your colors for their content, as well:
const COLOR_DEEP_RED=[32,0,0];
const COLOR_DEEP_BLUE=[0,0,32];
Then, reference those constants in other constants named for their use:
const TITLE_BAR_BACKGROUND_COLOR=COLOR_DEEP_RED;
const WINDOW_BORDER_COLOR=COLOR_DEEP_BLUE;
This way, all your color definitions and usages are defined in one place and can be manipulated without having to traipse all over your codebase.
That's what's worked well for me. YMMV.
add a comment |
I work mostly in SQL server which doesn't have constants at all ( you can define a function which always returns the same value ).
I would say that your naming granularity is likely too fine here. I would expect there to only be one place in your application where you welcome a user back, and therefore a named constant is not needed at all. Instead I would expect a named procedure, something like DisplayWelcomeBackDialog or similar.
Constants ( and other things such as functions, procedures, variables ) should usually be named for their meaning. In some cases a mathematical constant may be so well known that it an abbreviation can be used, say Pi = 3.1459.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
It entirely depends on whether it is a constant and won't likely change or if it is a constant which is symbolic or representative of something else and therefore may change.
An example of a constant which won't likely change is:
const PI=3.1415926
const RED=[255,0,0]
In this case, call it as it is. It represents the value of PI, not some arbitrary float.
An example of a constant which is symbolic or representative of something else is:
const WELCOME_MESSAGE="welcome.user"
const CONNECT_URL="jdbc:sqlserver://server:1433;databaseName=AdventureWorks"
This is not the message itself, but rather a key for looking up the message. If later you should decide that the key would be better written "user.welcome", you can change the constant in your program, and WELCOME_MESSAGE is equally valid and therefore doesn't need to be renamed as it is always used in the same way.
Same can be said for the connect URL. It will be the same throughout your program, but the value is also very likely to change in the future. So calling it CONNECT_ADVENTUREWORKS would be wrong. The name of the database may not change, but it is still not best practice since the name is a reflection of its value, which again, would be okay if the value doesn't change. Otherwise the name should be more towards how it will be used in your program. In this case it is your connection URL to the database (whatever it may be), and should be called thusly.
It's always a constant, just a less constant constant. :)
add a comment |
It entirely depends on whether it is a constant and won't likely change or if it is a constant which is symbolic or representative of something else and therefore may change.
An example of a constant which won't likely change is:
const PI=3.1415926
const RED=[255,0,0]
In this case, call it as it is. It represents the value of PI, not some arbitrary float.
An example of a constant which is symbolic or representative of something else is:
const WELCOME_MESSAGE="welcome.user"
const CONNECT_URL="jdbc:sqlserver://server:1433;databaseName=AdventureWorks"
This is not the message itself, but rather a key for looking up the message. If later you should decide that the key would be better written "user.welcome", you can change the constant in your program, and WELCOME_MESSAGE is equally valid and therefore doesn't need to be renamed as it is always used in the same way.
Same can be said for the connect URL. It will be the same throughout your program, but the value is also very likely to change in the future. So calling it CONNECT_ADVENTUREWORKS would be wrong. The name of the database may not change, but it is still not best practice since the name is a reflection of its value, which again, would be okay if the value doesn't change. Otherwise the name should be more towards how it will be used in your program. In this case it is your connection URL to the database (whatever it may be), and should be called thusly.
It's always a constant, just a less constant constant. :)
add a comment |
It entirely depends on whether it is a constant and won't likely change or if it is a constant which is symbolic or representative of something else and therefore may change.
An example of a constant which won't likely change is:
const PI=3.1415926
const RED=[255,0,0]
In this case, call it as it is. It represents the value of PI, not some arbitrary float.
An example of a constant which is symbolic or representative of something else is:
const WELCOME_MESSAGE="welcome.user"
const CONNECT_URL="jdbc:sqlserver://server:1433;databaseName=AdventureWorks"
This is not the message itself, but rather a key for looking up the message. If later you should decide that the key would be better written "user.welcome", you can change the constant in your program, and WELCOME_MESSAGE is equally valid and therefore doesn't need to be renamed as it is always used in the same way.
Same can be said for the connect URL. It will be the same throughout your program, but the value is also very likely to change in the future. So calling it CONNECT_ADVENTUREWORKS would be wrong. The name of the database may not change, but it is still not best practice since the name is a reflection of its value, which again, would be okay if the value doesn't change. Otherwise the name should be more towards how it will be used in your program. In this case it is your connection URL to the database (whatever it may be), and should be called thusly.
It's always a constant, just a less constant constant. :)
It entirely depends on whether it is a constant and won't likely change or if it is a constant which is symbolic or representative of something else and therefore may change.
An example of a constant which won't likely change is:
const PI=3.1415926
const RED=[255,0,0]
In this case, call it as it is. It represents the value of PI, not some arbitrary float.
An example of a constant which is symbolic or representative of something else is:
const WELCOME_MESSAGE="welcome.user"
const CONNECT_URL="jdbc:sqlserver://server:1433;databaseName=AdventureWorks"
This is not the message itself, but rather a key for looking up the message. If later you should decide that the key would be better written "user.welcome", you can change the constant in your program, and WELCOME_MESSAGE is equally valid and therefore doesn't need to be renamed as it is always used in the same way.
Same can be said for the connect URL. It will be the same throughout your program, but the value is also very likely to change in the future. So calling it CONNECT_ADVENTUREWORKS would be wrong. The name of the database may not change, but it is still not best practice since the name is a reflection of its value, which again, would be okay if the value doesn't change. Otherwise the name should be more towards how it will be used in your program. In this case it is your connection URL to the database (whatever it may be), and should be called thusly.
It's always a constant, just a less constant constant. :)
edited Mar 19 at 7:13
answered Mar 18 at 8:01
NeilNeil
20.6k3770
20.6k3770
add a comment |
add a comment |
The idea of defining constants is that you give a value a certain semantic meaning other than the value itself (the color of headers, a circumference-square ratio, etc.) Thus, in case the value is later changed, you only have to change it in one place, and the whole application or library reflects this change.
With this in mind, it's counterproductive to bind the constant name to its value instead of its meaning for the same reason you shouldn't tell how a function does something instead of what it does: if you change the contents enough, you need to rename the constant. Furthermore, if I see COLOR_DEEP_RED in the source code, it tells little about what it is, but COLOR_PRIMARY tells me that it's the primary color of the style palette.
I don't know if it's actually considered an antipattern itself, but I clearly see reasons not to name constants by their value, but by their meaning.
add a comment |
The idea of defining constants is that you give a value a certain semantic meaning other than the value itself (the color of headers, a circumference-square ratio, etc.) Thus, in case the value is later changed, you only have to change it in one place, and the whole application or library reflects this change.
With this in mind, it's counterproductive to bind the constant name to its value instead of its meaning for the same reason you shouldn't tell how a function does something instead of what it does: if you change the contents enough, you need to rename the constant. Furthermore, if I see COLOR_DEEP_RED in the source code, it tells little about what it is, but COLOR_PRIMARY tells me that it's the primary color of the style palette.
I don't know if it's actually considered an antipattern itself, but I clearly see reasons not to name constants by their value, but by their meaning.
add a comment |
The idea of defining constants is that you give a value a certain semantic meaning other than the value itself (the color of headers, a circumference-square ratio, etc.) Thus, in case the value is later changed, you only have to change it in one place, and the whole application or library reflects this change.
With this in mind, it's counterproductive to bind the constant name to its value instead of its meaning for the same reason you shouldn't tell how a function does something instead of what it does: if you change the contents enough, you need to rename the constant. Furthermore, if I see COLOR_DEEP_RED in the source code, it tells little about what it is, but COLOR_PRIMARY tells me that it's the primary color of the style palette.
I don't know if it's actually considered an antipattern itself, but I clearly see reasons not to name constants by their value, but by their meaning.
The idea of defining constants is that you give a value a certain semantic meaning other than the value itself (the color of headers, a circumference-square ratio, etc.) Thus, in case the value is later changed, you only have to change it in one place, and the whole application or library reflects this change.
With this in mind, it's counterproductive to bind the constant name to its value instead of its meaning for the same reason you shouldn't tell how a function does something instead of what it does: if you change the contents enough, you need to rename the constant. Furthermore, if I see COLOR_DEEP_RED in the source code, it tells little about what it is, but COLOR_PRIMARY tells me that it's the primary color of the style palette.
I don't know if it's actually considered an antipattern itself, but I clearly see reasons not to name constants by their value, but by their meaning.
answered Mar 18 at 7:39
Jesus Alonso AbadJesus Alonso Abad
66836
66836
add a comment |
add a comment |
Name your string constants for their content.
const WELCOME_MESSAGE="Welcome!";
const WELCOME_BACK_MESSAGE="Welcome back!";
That will come in handy when you're looking at a localized string.
const WELCOME_MESSAGE="Wilkommen!';
const WELCOME_MESSAGE="¡Bienvenida!';
Name your colors for their content, as well:
const COLOR_DEEP_RED=[32,0,0];
const COLOR_DEEP_BLUE=[0,0,32];
Then, reference those constants in other constants named for their use:
const TITLE_BAR_BACKGROUND_COLOR=COLOR_DEEP_RED;
const WINDOW_BORDER_COLOR=COLOR_DEEP_BLUE;
This way, all your color definitions and usages are defined in one place and can be manipulated without having to traipse all over your codebase.
That's what's worked well for me. YMMV.
add a comment |
Name your string constants for their content.
const WELCOME_MESSAGE="Welcome!";
const WELCOME_BACK_MESSAGE="Welcome back!";
That will come in handy when you're looking at a localized string.
const WELCOME_MESSAGE="Wilkommen!';
const WELCOME_MESSAGE="¡Bienvenida!';
Name your colors for their content, as well:
const COLOR_DEEP_RED=[32,0,0];
const COLOR_DEEP_BLUE=[0,0,32];
Then, reference those constants in other constants named for their use:
const TITLE_BAR_BACKGROUND_COLOR=COLOR_DEEP_RED;
const WINDOW_BORDER_COLOR=COLOR_DEEP_BLUE;
This way, all your color definitions and usages are defined in one place and can be manipulated without having to traipse all over your codebase.
That's what's worked well for me. YMMV.
add a comment |
Name your string constants for their content.
const WELCOME_MESSAGE="Welcome!";
const WELCOME_BACK_MESSAGE="Welcome back!";
That will come in handy when you're looking at a localized string.
const WELCOME_MESSAGE="Wilkommen!';
const WELCOME_MESSAGE="¡Bienvenida!';
Name your colors for their content, as well:
const COLOR_DEEP_RED=[32,0,0];
const COLOR_DEEP_BLUE=[0,0,32];
Then, reference those constants in other constants named for their use:
const TITLE_BAR_BACKGROUND_COLOR=COLOR_DEEP_RED;
const WINDOW_BORDER_COLOR=COLOR_DEEP_BLUE;
This way, all your color definitions and usages are defined in one place and can be manipulated without having to traipse all over your codebase.
That's what's worked well for me. YMMV.
Name your string constants for their content.
const WELCOME_MESSAGE="Welcome!";
const WELCOME_BACK_MESSAGE="Welcome back!";
That will come in handy when you're looking at a localized string.
const WELCOME_MESSAGE="Wilkommen!';
const WELCOME_MESSAGE="¡Bienvenida!';
Name your colors for their content, as well:
const COLOR_DEEP_RED=[32,0,0];
const COLOR_DEEP_BLUE=[0,0,32];
Then, reference those constants in other constants named for their use:
const TITLE_BAR_BACKGROUND_COLOR=COLOR_DEEP_RED;
const WINDOW_BORDER_COLOR=COLOR_DEEP_BLUE;
This way, all your color definitions and usages are defined in one place and can be manipulated without having to traipse all over your codebase.
That's what's worked well for me. YMMV.
answered Mar 18 at 7:39
aridlehooveraridlehoover
251210
251210
add a comment |
add a comment |
I work mostly in SQL server which doesn't have constants at all ( you can define a function which always returns the same value ).
I would say that your naming granularity is likely too fine here. I would expect there to only be one place in your application where you welcome a user back, and therefore a named constant is not needed at all. Instead I would expect a named procedure, something like DisplayWelcomeBackDialog or similar.
Constants ( and other things such as functions, procedures, variables ) should usually be named for their meaning. In some cases a mathematical constant may be so well known that it an abbreviation can be used, say Pi = 3.1459.
add a comment |
I work mostly in SQL server which doesn't have constants at all ( you can define a function which always returns the same value ).
I would say that your naming granularity is likely too fine here. I would expect there to only be one place in your application where you welcome a user back, and therefore a named constant is not needed at all. Instead I would expect a named procedure, something like DisplayWelcomeBackDialog or similar.
Constants ( and other things such as functions, procedures, variables ) should usually be named for their meaning. In some cases a mathematical constant may be so well known that it an abbreviation can be used, say Pi = 3.1459.
add a comment |
I work mostly in SQL server which doesn't have constants at all ( you can define a function which always returns the same value ).
I would say that your naming granularity is likely too fine here. I would expect there to only be one place in your application where you welcome a user back, and therefore a named constant is not needed at all. Instead I would expect a named procedure, something like DisplayWelcomeBackDialog or similar.
Constants ( and other things such as functions, procedures, variables ) should usually be named for their meaning. In some cases a mathematical constant may be so well known that it an abbreviation can be used, say Pi = 3.1459.
I work mostly in SQL server which doesn't have constants at all ( you can define a function which always returns the same value ).
I would say that your naming granularity is likely too fine here. I would expect there to only be one place in your application where you welcome a user back, and therefore a named constant is not needed at all. Instead I would expect a named procedure, something like DisplayWelcomeBackDialog or similar.
Constants ( and other things such as functions, procedures, variables ) should usually be named for their meaning. In some cases a mathematical constant may be so well known that it an abbreviation can be used, say Pi = 3.1459.
answered Mar 18 at 7:56
George BarwoodGeorge Barwood
1076
1076
add a comment |
add a comment |
6
Way too broad. Depends on context. In a list of colours, calling a constant DEEP_RED makes perfect sense e.g., in a list of colour values used in a user interface, you'd name them for the things being coloured, like SCROLLBAR_BACKGROUND_COLOUR = Colours.DEEP_RED;
– jwenting
Mar 18 at 7:30
That is an anti-pattern. Two problems, first: Rate of change. The name of the constant within the code will change less frequently then the contents of it. That is the whole point of abstracting it, to allow simple and global change. Second: level of abstraction. As a benefit of putting the constant behind a name, the name can mean something. Maybe Title_On_Error vs Title_On_Success. Try and make the name say something important about how/when it is used.
– Kain0_0
Mar 18 at 7:31
Hmm... why would team members who don't need to know about the title bar ever see that string constant in their code? If you use
TITLEBAR_MESSAGEfor strings that are not actually title bar messages, then it's not wonder that the practice feels wrong to you.– Christian Hackl
Mar 18 at 7:45
Both are readable. Which one do you think is more meaningful for the reader in each case? I would dare to say that it depends on the place and the moment you use one or another. Anyways, you could use both altogether as it was already suggested.
– Laiv
Mar 18 at 7:46
Possible duplicate of Usage of magic strings/numbers
– Blrfl
Mar 18 at 10:27