Please share your opinion

What do you think?


I have read several times that so called magic numbers are from the dark side. But in this case you can see it in the picture that it is difficult to be confused by the digits. Let’s look at one of the lines:
I have a panel component which preferred size property should be… the horizontal parameter: 356 and the vertical parameter is 356 pixels.

Is it hard to understand? Does it take a long time to see what are the magic numbers for?

Your opinion?

If you have to use more than once the same parameter, then it is ok to use constants. But a preferred size of a widget is used only once usually.

As you can see above, the names of constants should be self explaining. In usual it means long names (a lot of characters). Too long and too much text is less readable IMHO.

Let’s see an example from the web:

magicnumbersexampleAnswer says that using constant in this case makes the code more readable. I think it is not. Password length must be greater than seven is absolutely readable for me. Much more than seeing a constant’s name. But to maintain the code is easier when you use constants.

And what about this solution?

magicNIn the 18th line. Do you think it is easy to read that line or not?

We have discussed this in an other forum in Hungary with Gábor and he wrote a quite good essay about the magic numbers and usage of constants, properties, parameters. He said the followings:

– When constants (let’s say parameters) are for the whole project, then they can go into a so called constants’ file. This is a pure java class which has constants and nothing else inside. It is easy to maintain our constants, but they are a little bit far from the place of their usage.

– Second case is when the parameters are for a little part of the code and/or they are for configuration which can be modified by the customer. In this case it is good to use property files

– And finally, place your constants as close to the place where they are used as you can.

Thank you Gábor!

This entry was posted in java, opinion, programming and tagged , , , . Bookmark the permalink.

3 Responses to Please share your opinion

  1. Zsolt János says:

    I think the point there really is that you don’t know what is 7 in that password checking method. (well, in this case, of course you know it, but maybe in a bigger system, it can help). I think it kinda’ acts like an explanation. It is 7, because it is MAX_PASSWORD_SIZE.
    It explains the if statement as well.

    This is how i read it:

    If password length is more than 7, throw an exception.
    If password length is more than the max size, throw an exception.

    I would choose the first one anytime. And that is why i would use a constant variable with a nice name rather than a magic number.


  2. PuZZleDucK says:

    The problems with magic numbers is also evident when the project is big or involves many people.
    Taking the password example, the password length is probably checked in many places so refactoring the magic number is harder.
    Also I might be making unrelated changes, and I want them to occur before the length check… with magic numbers I have to know that password magic number before I can find the correct statement, however the more verbose version makes the original developers _intention_ clearer.


  3. cleanc0der says:

    Thank you for your comments! The are brilliant and extremely important for me!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s