• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Checkstyle - avoiding using 'magic numbers'

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm pretty new to java and have just discovered CheckStyle. One of the messages I'm getting a lot of is # 'is a magic number'.

In one case I worked around it like this:


But what should I do in a case where I have a lot of numbers like this:




Is there some other way I should be doing this? Thanks for any input!
 
author and iconoclast
Posts: 24207
46
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
Well, in this particular case, it's much nicer to use column names rather than column numbers; this makes your code far more robust to database schema changes.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K DeLucia wrote:But what should I do in a case where I have a lot of numbers like this:


If you still want to use the numbers, you can always use a counter:

Added bonus is that you can add a field in between without having to re-index. A downside is that the order is still important.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about cases where the numbers are not related?
I recently had to use iText to generate some Pdfs. There were so many numbers that I finally had to convince people to ignore those warnings.
 
author & internet detective
Posts: 41860
908
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
I have my static analysis utility configured to ignore magic numbers above a certain threshold. (I think it is 10 or 15) to avoid the JDBC code being triggered. I don't like using the column names because if they change in the SQL, I have to remember to change them in the get/set methods. For the rare occasions where my JDBC code uses a ton of columns, I configure the static analysis tool to suppress the errors.

In your first example with the zip code, the tool is right. You do have a "magic number" in the code. You didn't work around it - you fixed a problem. It's traditional to use a real constant for this like:
private static final int ZIP_LENGTH = 11;
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
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

Sridhar Santhanakrishnan wrote:What about cases where the numbers are not related?
I recently had to use iText to generate some Pdfs. There were so many numbers that I finally had to convince people to ignore those warnings.


Why do yo have so many numbers in iText? I have mainly column widths in my iText code - which are all constants anyway. What other types of numbers do you have?
 
reply
    Bookmark Topic Watch Topic
  • New Topic