Use constants rather than numbers - declaring variables with FINAL statement


Suppose the government hired you to create an application that, among other things, examines whether a citizen is adulthood or not.
How would you do this conditional test?

if (age> = 21) or who knows ... if (age > 20)?

Both correct, but absolutely not recommended.
In this article we will teach an important lesson in Java: Do not use numbers, but constant.




Fleeing, rather, the scope of the "Object Oriented" section, we will introduce a best practice in Java programming, which is the use of constants.
This section aims to introduce concepts to the next article, about enum.


Why use constants instead of numbers?


Imagine that you have made the application. A real application like that takes, easy, thousands of lines of code.
Throughout your program you used that magic number, 21.
It is clear that he will appear a lot, because the Java application is about adulthood.

Ok, so far so good.
As you study the Progressive Java course, your program is working like a charm.

Now imagine the situation: elections.
New government, new laws ... adulthood age is now 18.
What do they do?

'Call the developer!' and there you go, picking out the code number 21 and substituting it to the 18 number.
Imagine the work ... thousand of lines...classes...objects...methods...

A smarter can say 'use regex or ctrl + f' or any other shortcut to find and replace 21 by 18.
But what if in your application number 18 appears and has nothing to do with full age?
You'll have a hell of a bug.

Or instead of 18, you used the numbers 0 or 1 in your application, and now needs to change.
You can not replace every 1 or 0, which is picking out the code and manually changing!!


The advantage of using constants is this: once you define, you must use the NAME, EVER!
The right would be:

int adulthood = 21;
if (age > = 21)
    is adult


Ready. The variable 'adulthood' could appear 1 million times in your code.
On the day that the government change it, change only the declaration of the value of 21 to 18 and it will be spread throughout your code.

Another advantage is the ability to read your code for you (after months without seeing the code, we forget what it means that heap of Java code) or to someone else who is reading.
Note:
if (age > 20)
You can not know exactly what it is, just looking. Got to see some documentation to be sure.

But if you come across with:
if (age > adulthood)
... Well, adulthood is adulthood! I know what it is! That 'if' checks if the person is an adult!

Another example:
A = 3.14 * r * r
What's this? Do not know?

And now ...
A = PI * r * r
Now it is obvious that the number pi, then it is something related to geometry. In this case, the area of ​​a circle.
The most appropriate would be to define constants and use their names.
It is more beautiful, elegant and professional.

That is, set the value of your Java code variables and use constants.


Java final keyword - declaring variables as constants

The Java reserved a very interesting gimmick for you to use its constants, which is the keyword 'final.

When you define a variable with the 'final', it becomes constant.
If you try to change it, get an error.

The statement is as follows:
final type variable_name;

Eg
final int YES = 1;
final int NO = 0;

Ready. Now you can use 'YES' and 'NO', instead of using numbers.
Use an 'OK' or 'ERROR', rather than numbers strange and bizarre.

In classes would be:
private final int STATUS_ON = 1;
private final int STATUS_OFF = 0;

From now on, we will use some numbers.
Even if your applications are simple and short - and there is no apparent need for constant - use them.
It is a custom that takes when turning professional and create real applications.


final statement in Classes and Objects

In a class, you can declare an attribute as final in two possible ways:


  1. To initialize the variable, start with its value. Only do this if you want all objects of that class have this variable with the same value.
  2. You can also initialize your variables via a constructor method. Thus, each initialized object can have a different value.

But remember: After initialized in its declaration or in the constructor, can no longer be changed.

Make sure you really want to have a constant in your application. If yes, always use the keyword final, as this will prevent the variable has its value changed by accident or bad programming.

No comments: