wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Doubt about the need of using a constant 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 » Java » Beginning Java
Bookmark "Doubt about the need of using a constant" Watch "Doubt about the need of using a constant" New topic
Author

Doubt about the need of using a constant

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
While working on this tutorial a member sugested:
I'm not sure why "CAD" is hard coded in the class. If it is the default, I would make it a constant with a name that explains the purpose.

Well, i introduced:

but would like help in explaining me the advantages of this aproach, since i didnt notice any improvement in my tests.

Thanks in advance


java amateur
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

If this string is used in two places in your code, then if you don't use a constant, then you might accidentally type it differently in those two places: "usd" and "USD" or "US-D" or "Dollars" or whatever. The risk of this happening grows exponentially as a program grows, and is greatest if the code is used in a library.

The thing is that if you don't use a constant, then you can mistype things and the compiler won't tell you about it -- the code will compile fine, but not work correctly. This kind of bug can be very hard to find! If you mistype the name of a constant, then the compiler will tell you about your mistake and make you fix it right away.


[Jess in Action][AskingGoodQuestions]
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks for your clear explanation.
may i call that defensive code?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30361
    
150

Miguel,
For that purpose, it is defensive coding. I can think of two other reasons to use a constant:

1) It's clearer. As a reader of the code, I may not know what "CAD" is. If you use a constant, you tell me that it is the canadian dollar. This makes the code much more readable.
2) If those Strings are used in multiple classes, you can refer to the same constant. This is useful if that constant value changes. Then you can change it in one place rather than have to hunt all over the code for it.

On a related note, here's some more unsolicited advice
It's a convention to make constants static and in all caps with underscores between words. This highlights the fact that they are constants and not specific to a specific instance of the class. When reading the code, you can say "so that's a constant" without having to look it up.


[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
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
It's a convention to make constants static and in all caps with underscores between words.

indeed i had checked HFJ and TIJ and both mentioned it - my fault; but they where inconlusive about my previous Q, which was answered here

thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt about the need of using a constant