• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about the need of using a constant

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your clear explanation.
may i call that defensive code?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34859
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic