14

In my code,it shows the warning with message '100' is a magic number.See the below code,

int randomNo = generator.nextInt(100);

I read it here What is a magic number, and why is it bad? but my doubt is declaring 100 by creating variable with static will occupy more space,since i am using this in a single place.Is this a correct way to solve this?

  public static final int HUNDRED= 100;

Any Suggestion?

Community
  • 1
  • 1
moh
  • 1,426
  • 2
  • 16
  • 43
  • it is better to use it like this : `public static final int MAX_RAND= 100;` – Lrrr May 05 '15 at 05:21
  • I need it in a single place still its better to declare in top? – moh May 05 '15 at 05:25
  • I came across a related ERROR, whilst doing a maven build using the checkstyle plugin: `(coding) MagicNumber: '4' is a magic number.` I found that I could avoid these errors using the method described in the accepted answer (of course adding a comment to the field you've just added). – Bill Naylor Jul 12 '19 at 11:25

5 Answers5

24

well HUNDRED really is kinda silly, but why did you choose 100 and what is its meaning?

something like:

public static final int RANDOM_UPPER_LIMIT=100;

or something even more informative, depending on what you use the value for:

public static final int MAX_NUMBER_OF_COLORS=100;

would make more sense and improve readability.

Space saving should not be a consideration in this case, the space overhead of declaring a variable, if there is such, is completely negligible.

yurib
  • 8,043
  • 3
  • 30
  • 55
  • hi yurib thanks for your suggestion, i agree that selecting HUNDRED var name is not correct way,but here my question which i asked is creating static var will occupy more space, – moh May 05 '15 at 16:41
  • @mohammed space saving should not be a consideration in such a case, the space overhead of declaring a variable, if there is such, is completely negligible – yurib May 05 '15 at 16:44
8

It doesn't really have to do with storage, it's readability. If you want to change some number it can be difficult to find in code, if it's up at the top then it's better (if it's in a config file, better yet in many cases).

Yes, that's a good solution.

If you don't need it outside that file, you should make it "private", and you might want to be even more readable and use a name that indicates what it really means, like:

MAX_RANDOM_NUMBER=100

better yet include what it's used for

MAX_RANDOM_FOR_CARD_SELECTION

or something like that.

In this way when you go to look into that file 5 months from now because you added 20 new cards, it's completely obvious what you have to change without even glancing at the code.

Bill K
  • 62,186
  • 18
  • 105
  • 157
4

It's good to write not the shortest code, but code which is easy to understand and maintain. Storing this 100 to constant you may add a good name explaining why it's really 100. For example if you want to generate random score and your maximal possible score is 100, then you can define

static final int MAX_SCORE = 100;

After that you can use it in other places as well. Everybody will understand why it's 100 and nothing else. And if someday you will need to change it, say, to 200, you will have to replace it in only one place without searching through the code.

Also it's possible that in some other part of your program you will have 100 which has different meaning (say, MAX_PERCENT). If you want to change MAX_SCORE to 200, but leave MAX_PERCENT as is, it would be much easier if you have separate constants.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
  • yup..i understood but i need it in a single place,I have 50 other magic number in my application.will it occupy extra storage?wht ab performance? – moh May 05 '15 at 05:28
  • 1
    In runtime there will be no overhead as java compiler will directly substitute there numbers in your code where they are used (provided that you declare them as `static final`). Your compiled application size will grow a little, but that's ok. – Tagir Valeev May 05 '15 at 05:30
2

Check out Robert Martin's (Uncle Bob)

Clean Code

book for a thorough explanaition (or any other guide on coding style). Basically a hard coded '100' doesn't mean anything to the reader of your code. It won't mean anything to you in 6 months either, after you are done with your app. It is a magic number since it appears in the code - in 99 out of 100 cases - as almost out of nowhere. Why is it 100 and not 99 or 101? Why should your app limit the random number generation to 100 and not above (or below)?

To wrap up, it's a thing of readability of your code for yourself and for present or future readers of your code, it's a matter of coding style.

ttarczynski
  • 949
  • 1
  • 10
  • 19
0

Most compilers, including JIT compilers, will inline constant primitives, including statics. IE, at compile time it will remove the variable and expand your code back to

generator.nextInt(100);

So in practice there won't be space trade-offs, and you're improving readablity.

Be careful about expanding the idea out to more complicated code though. There are compiler and language dependent rules on when it is able to do certain optimisations.

Kindread
  • 926
  • 4
  • 12