The world is a stage where all of us are artists and constant learning is the foundation of success. So, to improve ourselves here we are going to know more about coding style in which we will look deep into Tools in Java. We will see why it is important and how to achieve that.
As in the previous blog, we saw clean coding basic practices and SOLID principles. Here we are going to look at other things as well To get the context Clean code and the previous blog please go through this link: Functional Java CodeStyle [Part: 1] and Functional Java CodeStyle [Part: 2].
Tools for Help
Writing clean code is not just a matter of principles and practices, but it’s a personal habit. We tend to grow as better developers as we learn and adapt.
However, to maintain consistency across a large team, we’ve also to practice some enforcement. Code reviews have always been a great tool to maintain consistency and help the developers grow through constructive feedback.
There are also several tools available in the Java ecosystem, which take at least some of the responsibilities away from code reviewers:
- Code Formatters: Most of the popular Java code editors, including Eclipse and IntelliJ, allows for automatic code formatting. We can use the default formatting rules, customize them, or replace them with custom formatting rules.
- Static Analysis Tools: There are several static code analysis tools for Java, including SonarQube, Checkstyle, PMD and SpotBugs. They have a rich set of rules which can be used as-is or customized for a specific project.
Static Analysis Tools:
- SonarQube: It is an open-source platform for ensuring code quality. because to ensure quality, reliability, and maintainability over the life-span of the project; a poorly written codebase is always more expensive to maintain.
- Checkstyle: Checkstyle is an open source tool that checks code against a configurable set of rules.
- PMD: It is a source code analyzer to find common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth.
- SpotBugs: SpotBugs is a program which uses static analysis to look for bugs in Java code. It is free software, distributed under the terms of the GNU Lesser General Public License.
So these were Tools which we use. Now let’s see about dry and kiss
DRY & KISS
DRY stands for “Don’s Repeat Yourself”. This principle states that a piece of code should not be repeated across the software.
However. note that we should be careful in adopting this rather too literally. Some duplication can actually improve code readability and maintainability.
KISS stands for “Keep It Simple, Stupid”. This principle states that we should try to keep the code as simple as possible.
This means if we keep our classes and methods focussed and small, this will lead to simpler code.
TDD stands for “Test Driven Development”. This is a programming practice that asks us to write any code only if an automated test is failing.
Hence, we’ve to start with the design development of automated tests. In Java, there are several frameworks to write automated unit tests like JUnit and TestNG.
We only add code if the new or any of the old tests fail. Which means that it leads to reusability as well.
Also, This leads to software that always works as expected. As we always start with tests, we incrementally add working code in small chunks.
To sum up, it’s important to note that all of these principles and practices are there to make our code cleaner. This is a more subjective term and hence, must be evaluated contextually.
While there are numerous sets of rules available to adopt, we must be conscious of our maturity, culture, and requirements. We may have to customize or for that matter, devise a new set of rules altogether. But, whatever may be the case, it’s important to remain consistent across the organization to reap the benefits.
That’s pretty much it from the article. We will come with 2nd Part in which we will include solid Principles and so.
If you have any feedback or queries, please do let me know in the comments. Also, if you liked the article, please give me a thumbs up and I will keep writing blogs like this for you in the future as well. Keep reading and Keep coding.