Today I want to talk about code quality. It's one of the most important parts of OOP, which is underestimated nowadays, mostly because of current working style and "focusing on results". So, how is it in Java? What good practices will a clean code give us?
![]() |
| The clean code is like the clean sky, doesn't happen too often. Image from: http://all4desktop.com/ |
It's not a secret that Java is an extremely huge language, mostly because of its object oriented structure, but that's not the only reason. Without overloading operators even a simple behavior could be much longer in a source code than expected. It's why Java 8 appears with more functional programming style than any other version before.
In this article series I want to show a few practices which in my opinion can improve quality of code. Let's begin from basics about code formatting and code documentation.
1. Code formatting
It's a controversial topic, which causes a lot of never ending discussions. In fact does not matter how you format your code, as long as it is readable for others. Because of current displays resolution I believe that longer lines allow saving a lot of space - what I personally prefer.
Popular myth across the internet is that a good developer should see a lot of code on his screen. In practice I met very good developers who prefer to focus on just a few lines at one time. Both methods have theirs advantages and disadvantages.
2. Code documentation
The second myth is about code documentation, and it says that a good code comments itself. Probably every one of you has heard that before. It's almost as ridiculous as lack of documentation because of Agile.
- I need to change some old functionality, have we got any documentation?
- Nope.
- Why not?
- Come on! We are working in agile, we are preparing functional requirements on an ongoing basis!
The source code is a natural and the final documentation, but none of methodologies say that's the only one. I believe that every type and method written in Java should have at least a javadoc. That rule does not apply to private, standard (like getters and setters) methods and just the obvious ones.
Of course it provides a responsibility for keeping that kind of documentation up to date. But it's obvious, isn't it?
3. Inline comments
In my career I never had a reason to write inline comments. I believe that if you need a comment inside a method, it probably means that the method is too long. It's doing too much and has too much responsibility. Information about an algorithm or a solution should be included into a javadoc.
4. Shorter != better
If you ask me how long the single method should be, I would say: as short as possible. Not longer than few lines. However that's not always the truth. Not so long ago I delegated a task for one of the junior developers to refactor an old UI in SWT created by some templates, to be visible by Eclipse WindowBuilder tool. He was upset, because that solution causes some duplication in code (as you already know generated code is always worse than the one written manually).
I answered that it gives more advantages than disadvantages: possibility to create a UI with drag & drop, possibility to replace a user interface part with different libraries, upgrade currently used frameworks to the newest one and few more resulting of our implementation.
5. Code quality tools
Every Java IDE gives a possibility to use some internal tools to increase quality of your code. The most popular are PMD, FindBugs and Checkstyle (available for Eclipse, NetBeans and IntelliJ). In most cases their prompts are useful, however some of the rules might not fit to your project. That could happen when you are using external libraries, or when you have some specific convention in your project. Because of that configuration of the code quality tools should be prepared wisely and carefully.
Conclusion
In fact that's only the beginning of the code quality guide. I'll prepare another article about this topic in the future - I'll write something more about white spaces, design patterns and SOLID principles. However, keeping in mind the few rules above will improve code quality in your project. As always, I encourage you to comment and I'm curious how you take care of the code quality in your projects.
1. Code formatting
It's a controversial topic, which causes a lot of never ending discussions. In fact does not matter how you format your code, as long as it is readable for others. Because of current displays resolution I believe that longer lines allow saving a lot of space - what I personally prefer.
Popular myth across the internet is that a good developer should see a lot of code on his screen. In practice I met very good developers who prefer to focus on just a few lines at one time. Both methods have theirs advantages and disadvantages.
2. Code documentation
The second myth is about code documentation, and it says that a good code comments itself. Probably every one of you has heard that before. It's almost as ridiculous as lack of documentation because of Agile.
- I need to change some old functionality, have we got any documentation?
- Nope.
- Why not?
- Come on! We are working in agile, we are preparing functional requirements on an ongoing basis!
The source code is a natural and the final documentation, but none of methodologies say that's the only one. I believe that every type and method written in Java should have at least a javadoc. That rule does not apply to private, standard (like getters and setters) methods and just the obvious ones.
Of course it provides a responsibility for keeping that kind of documentation up to date. But it's obvious, isn't it?
3. Inline comments
In my career I never had a reason to write inline comments. I believe that if you need a comment inside a method, it probably means that the method is too long. It's doing too much and has too much responsibility. Information about an algorithm or a solution should be included into a javadoc.
4. Shorter != better
If you ask me how long the single method should be, I would say: as short as possible. Not longer than few lines. However that's not always the truth. Not so long ago I delegated a task for one of the junior developers to refactor an old UI in SWT created by some templates, to be visible by Eclipse WindowBuilder tool. He was upset, because that solution causes some duplication in code (as you already know generated code is always worse than the one written manually).
I answered that it gives more advantages than disadvantages: possibility to create a UI with drag & drop, possibility to replace a user interface part with different libraries, upgrade currently used frameworks to the newest one and few more resulting of our implementation.
5. Code quality tools
Every Java IDE gives a possibility to use some internal tools to increase quality of your code. The most popular are PMD, FindBugs and Checkstyle (available for Eclipse, NetBeans and IntelliJ). In most cases their prompts are useful, however some of the rules might not fit to your project. That could happen when you are using external libraries, or when you have some specific convention in your project. Because of that configuration of the code quality tools should be prepared wisely and carefully.
Conclusion
In fact that's only the beginning of the code quality guide. I'll prepare another article about this topic in the future - I'll write something more about white spaces, design patterns and SOLID principles. However, keeping in mind the few rules above will improve code quality in your project. As always, I encourage you to comment and I'm curious how you take care of the code quality in your projects.







Nice article, but it's rather Table of Contents. Now I'm waiting for some explication of each :)
ReplyDelete