Free Java course. Sign Up for tracking progress →

Java: Magic Numbers

Recall one of the past lessons:


var euros = 1000;
var dollars = euros * 1.25; // => 1250
var rubles = dollars * 60; // => 75000

System.out.print(rubles);

From the point of view of professional development, this code "smells." This is how code that does not correspond to the so-called best practices is described. And the reason is this: right now, looking at the numbers 60 and 1.25, you are most likely wondering: "What are these numbers?". And imagine what will happen in a month! And how will a new programmer understand him, who has not seen the code before? In our example, the context is restored due to proper naming, but in real life the code is much more complicated, and therefore it is often impossible to guess the meaning of numbers.

This “smell” is called magic numbers. Numbers whose origin is impossible to understand without a deep knowledge of what is happening inside this part of the code.

The way out is simple: it is enough to create variables with the correct names, how everything will fall into place.


var dollarsInEuro = 1.25;
var roublesInDollar = 60;

var euros = 1000;
var dollars = euros * dollarsInEuro; // => 1250
var rubles = dollars * roublesInDollar; // => 75000

System.out.print(rubles);

Pay attention to the following details:

  • Naming lowerCamelCase
  • Two new variables are separated from subsequent calculations by a blank line. These variables make sense without computations, so this separation is appropriate, it improves readability.
  • It came out well-named and structured code, but it is longer than the previous version. This is often the case, and this is normal, because the code should be readable.

Instructions

You are faced with this code, which displays the total number of rooms in the possession of the current king:

var king = "King Balon the 6th";
System.out.print(king + "has" + (6 * 17) + "rooms.");

As you can see, these are magic numbers: it is not clear what 6 is and what 17. It is possible to guess if you know the history of the royal family: each new king inherits all the castles from their ancestors and builds a new castle - an exact copy of the parent.

This strange dynasty just breeds the same castles ...

Get rid of the magic numbers by creating new variables, and then display the text.

It will turn out like this:

King Balon the 6th has 102 rooms.

Variable names should convey the meaning of numbers, but at the same time they should remain sufficiently short and capacious for comfortable reading.

Remember: the code will work with any names, and our system always checks only the result on the screen, so the execution of this task is under your responsibility.

The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.