• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Doubt about the need of using a constant

 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your clear explanation.
may i call that defensive code?
 
author & internet detective
Posts: 41415
854
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic