Nowadays Java is one of the most popular programming languages to create the enterprise applications. Because of that we often have to work in large groups, cooperating between many teams and so on. During the software development one of the most important things is the quality of code.
Probably the last thing which you - as a programmer or system architect - want to see during the code review is some freaky code which enchants the reality because of some business logic.
- it must be ready by the end of the day,
- it needs to be done by yesterday,
- you have 999 more important tasks for tomorrow,
etc..
Naturally programmer should protect himself, even if his code is not perfect at this moment (because maybe he will be the lucky one to clear that crap during next refactoring). One of the easiest way to do that is to keep the quality of code. Because of the importance of that topic, today I want to talk about one of the Java syntax words. To be exact, it’s
the magic 'final' word.
![]() |
| Image from: http://fc00.deviantart.net |
There are few different contexts, when the 'final' syntax can be used:
1. Final class
public final class MyFinalClass {
//some code here.
}
A final class cannot be subclassed (it makes a compilation error):
public class MyOtherClass extends MyFinalClass { //compilation error.
//...
}
2. Final method
public class MyNonFinalClass {
public final void someMethod() {
// ...
}
}
A final method cannot be overridden or hidden by subclasses.
public class MyOtherClass extends MyNonFinalClass { // No more error.
@Override
public void someMethod() { // Compilation error
//...
}
}
3. Final variables
public final class MyFinalClass {
private final Object finalObject;
public MyFinalClass() {
finalObject = new Object(); // final object must be initialized.
}
}
Final variables can be initialized only once, either via an initializer or an assignment statement.
4. Blank final
To understand the blank final field let's imagine a DataModel class for some table. There will be a list with data and method to get the value depending on selected cell. Here it is:
public Object getValueAt(final int rowIndex, final int columnIndex) {
final SomeBean bean = list.get(rowIndex);
final Object value;
switch (columnIndex) {
case 0:
value = bean.getDateFrom();
break;
case 1:
value = bean.getUser().getLogin();
break;
case 2:
value = bean.getNote();
break;
default:
value = bean;
break;
}
return value;
}
As you can see the final variable doesn't need to be initialized at the point of declaration. OK, but how the final variables could help us increase code quality? What is the reason to use the final keyword? Let me show you:
Final method arguments forced you not to change them inside the method, which is always a good practice. Nothing unexpected would happen with your 'clear' data (not so fast - nothing if your object is immutable) . Look at getValueAt(...) method.
Let's get back to our hard business logic. By using final keyword with the variables inside you can avoid some errors which are hard to catch . For example:
public Integer someOtherMethod() {
final Integer value;
if (/* some crappy logic */) {
if (/* some even more crappy logic */) {
value = 1;
} else if (/* to make it clear next crap */) {
value = 2;
}
} else {
value = 3;
}
return value; // Compilation error, value might not have been initialized.
}
As you can see in that example, I didn't cover all possible cases. Is there something more? Maybe final variables are faster? - Well, yes... and no. In some cases yes, but mostly, it doesn't matter.
Many people are asking me: "if it's not faster, why should I use that? it's always 5 more letters per variable" (then why shouldn't you, if you can?). That's right, but this practice makes your code clear and easy to understand for other developers. You inform them at the beginning: "this variable will not change". Additional advantage: it's not necessary to debug full method to check the values of the variables . One line is enough.
At the end, the most important advantage: you and your code will look much more professional, because of additional part of
blue/violet/other color in your IDE :) I hope you'll enjoy this post and feel free to leave me some comments.







0 komentarze:
Post a Comment