• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checkstyle - avoiding using 'magic numbers'  RSS feed

 
K DeLucia
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • 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!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • 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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!