wood burning stoves 2.0*
The moose likes Agile and Other Processes and the fly likes constants vs hard coding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » Agile and Other Processes
Bookmark "constants vs hard coding" Watch "constants vs hard coding" New topic
Author

constants vs hard coding

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30361
    
150

One page 300 of "Clean Code", constant names vs hardcoding them is discussed. I suspect I err more on the side of making things constants than the author.

One quote on the page is, "and in the ____________ case, the number 5280 is so very well known and so unique a constant that readers would recognize it even if it stood alone on a page with no context surrounding it."

I find this interesting and wonder if people here know what number means off the top of their heads. And if you would recognize a typo of that number like 5820 if it were in code? If you don't, how easy is to find the answer via a search if you don't know what it means?

Please don't post what the number means within 24 hours of this post as I want people to get a chance to think about it.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1358
    
  17
I remember reading that and thinking shoot - I would never have guessed.

But then again, where I've hung out over the past 15 years, that number wasn't in general use.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Well, I guess when knowing the context, it would be easier to find out what that number is about.

But, what would be the disadvantage of using a constant, anyway? I just don't see any reason to not use one.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3219
What is your view on defining the constants in one centralized location versus defining it where it is required or used? Which is cleaner and why?
[ September 24, 2008: Message edited by: arulk pillai ]

Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by arulk pillai:
What is your view on defining the constants in one centralized location versus defining it where it is required or used? Which is cleaner and why?


It seems obvious to me that constants should be placed coherently - that is, they should be put together if they belong together semantically, and they should be kept as close to the code they belong to semantically as possible.

What would be arguments against doing that?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30361
    
150

Ok. It's been a day. 5280 is the number of feet in a mile.

Ilja: The context is "double milesWalked = feetWalked/5280.0" I agree it is obvious from the context. (Although I still think it should be a constant.) What got me was the sentence below that that said 5280 is so well known and unique that readers would recognize it without context. I certainly didn't.

Katrina: I have always lived in the US and last used that number in elementary school. Needless to say I don't know if off the top of my head.
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3219
Originally posted by Ilja Preuss:


It seems obvious to me that constants should be placed coherently - that is, they should be put together if they belong together semantically, and they should be kept as close to the code they belong to semantically as possible.

What would be arguments against doing that?



Totally agree with you. Constants should be encapsulated. Some developers do have the rationale of defining a Constants class, so that everything is in one spot and can be quickly referred and maintained.



Ok. It's been a day. 5280 is the number of feet in a mile.



It wasn't obvious to me. This is like saying use 22/7 instead of declaring a constant know as PI.
[ September 24, 2008: Message edited by: arulk pillai ]
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1358
    
  17
Originally posted by arulk pillai:
This is like saying use 22/7 instead of declaring a constant know as PI.


I beg to differ. At least the number of feet in a mile is exactly 5280.

22/7 is not equal to pi, it is an approximation - and not a particularly good one, at that.

But that is tangential to the discussion of constants!
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3219
I beg to differ. At least the number of feet in a mile is exactly 5280.



In Australia we use KM as opposed to Miles. So, 5280 is not quite straight forward. Software development is becoming more and more international affair. I just had been involved in a project where code has been developed in India, Argentina, Italy, and Israel.
Alaa Nassef
Ranch Hand

Joined: Jan 28, 2008
Posts: 460
You reminded me of the mars climate orbiter crash that was caused by a mixup between metric and English units back in 1998.


Visit my blog: http://jnassef.blogspot.com/
Robert Martin
Author
Ranch Hand

Joined: Jul 02, 2003
Posts: 76
Originally posted by Jeanne Boyarsky:
One page 300 of "Clean Code", constant names vs hardcoding them is discussed. I suspect I err more on the side of making things constants than the author.

One quote on the page is, "and in the ____________ case, the number 5280 is so very well known and so unique a constant that readers would recognize it even if it stood alone on a page with no context surrounding it."

I find this interesting and wonder if people here know what number means off the top of their heads. And if you would recognize a typo of that number like 5820 if it were in code? If you don't, how easy is to find the answer via a search if you don't know what it means?

Please don't post what the number means within 24 hours of this post as I want people to get a chance to think about it.


It's true, I'm an American. The number 5280 has deep significance for me -- as it does for most Americans.

Perhaps a better example would be: int secs = mins / 60; Does that need to be int secs = min / SECS_PER_MIN;? I doubt it.


---<br />Uncle Bob.
Robert Martin
Author
Ranch Hand

Joined: Jul 02, 2003
Posts: 76
Originally posted by arulk pillai:
What is your view on defining the constants in one centralized location versus defining it where it is required or used? Which is cleaner and why?

[ September 24, 2008: Message edited by: arulk pillai ]


Files that gather all constants together are "Dependency Magnets". When you change these files (by adding a new constant for example) you must rebuild and redeploy every module who uses them. This is unfortunate.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30361
    
150

Originally posted by Robert Martin:
Perhaps a better example would be: int secs = mins / 60; Does that need to be int secs = min / SECS_PER_MIN;? I doubt it.

I do like that example better as everyone should recognize that. (I'm not sold on the feet per mile thing.)

I think the one with the constant looks a little nicer ascetically. What about code like the following two examples. Clearly "space" and "equal" are obvious from their constant values. But extracting just the escaped constants looks funny to me. I think it's the shifting between symbols and words in the same line that gets me. Does anyone have any thoughts on relative readability here?
String space = " ";
String wordChars = "\\w+";
String equal = "=";
String quote = "\"";
String regExp = space + wordChars + equal + quote + wordChars + quote;


String wordChars = "\\w+";
String quote = "\"";
String regExp = " " + wordChars + "=" + quote + wordChars + quote;
Kelahcim Kela
Greenhorn

Joined: Aug 17, 2007
Posts: 29
Originally posted by Robert Martin:

Perhaps a better example would be: int secs = mins / 60; Does that need to be int secs = min / SECS_PER_MIN;? I doubt it.


It depends what you want to express with the hard coded value. There are cultural differences between various countries and sometimes something obvious to one person may be a total mystery to somebody else (that's why IQ tests have to be adjusted).

Generally, philosophically speaking, you could use hard coding for constants referring to the items that are cultural universals
[ September 29, 2008: Message edited by: Kelahcim Kela ]
Kelahcim Kela
Greenhorn

Joined: Aug 17, 2007
Posts: 29

String space = " ";
String wordChars = "\\w+";
String equal = "=";
String quote = "\"";
String regExp = space + wordChars + equal + quote + wordChars + quote;


In my opinion this is slightly different situation. We are using regExp syntax here which is constant independently from the cultural context. Anybody using regExp must know the meaning of "space" and "equal" signs.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Kelahcim Kela:

In my opinion this is slightly different situation. We are using regExp syntax here which is constant independently from the cultural context. Anybody using regExp must know the meaning of "space" and "equal" signs.


I'd even argue that regular expressions are a programming language. Abstracting from it feels a bit to me like abstracting from Java.

What I *do* from time to time, though, is introducing expressive constants for regular subexpressions that have *domain* meaning:

regexp = areaCodeRegexp + telephonenumberRegexp;
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30361
    
150

Originally posted by Ilja Preuss:
I'd even argue that regular expressions are a programming language. Abstracting from it feels a bit to me like abstracting from Java.

Does that mean you would write it as
String regExp = " \\w+=\\"\\w+\\"";


What I *do* from time to time, though, is introducing expressive constants for regular subexpressions that have *domain* meaning

Same here. I use the "wordChars" type constants to build up those subexpressions first though unless the regexp is trivial.

There's no domain meaning here because it is an example with all business meaning extracted.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jeanne Boyarsky:

Does that mean you would write it as
String regExp = " \\w+=\\"\\w+\\"";


Well, besides trying to use a better name for the local variable that it gets assigned to, probably, yes. I can read it quite easily (although the double backslashes are kind of insane, in my view).
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30361
    
150

Originally posted by Ilja Preuss:
Well, besides trying to use a better name for the local variable that it gets assigned to

I assure you that the real code has a logical business domain name for this variable.


I can read it quite easily (although the double backslashes are kind of insane, in my view).

Wow! I wrote it and get a headache looking at it.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jeanne Boyarsky:

Wow! I wrote it and get a headache looking at it.


And you should - because it's actually syntactically wrong, it should be

String regExp = " \\w+=\"\\w+\"";

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constants vs hard coding