Victor Rentea calls himself a ‘Clean Code Evangelist’ and so his session on clean code at Devoxx was an interesting one. It was about how we need to reorganise code to make it easier to read and simpler to maintain. He also gave the three basic principles for helping to achieve this goal.
The old KISS principle (Keep It Simple, Stupid) is certainly still valid today. We assume that ‘over-engineering’ is not recommended because it leads to considerable loss of time (and so also budget). What already works in production can in principle be reused, so that more effort can be focused on important aspects such as new functionalities. Moreover, this makes the code less complex, which improves readability.
Single Responsibility Principle
The Single Responsibility Principle (SRP) is sometimes also called the Separation of Concerns principle. Basically, it means you make every code class or module in a program as short and as function-related as possible. Each class is then responsible for only a single part of the functionality. If code is largely the same, you can also split it off into a generic method in a generic class, for example a ‘util class’. The classes can then invoke this generic method. That does require some more programming work. As a result, it is sometimes difficult to apply the SRP principle because of deadlines. In addition, it is advisable to regularly refactor the code and do a code review.
Don’t Repeat Yourself Principle
The third basic principle for developers is ‘Don’t Repeat Yourself’. With DRY programming you ensure that the same code is not repeated. This way you get ‘clean code’, with a reduced risk of bugs. Victor Rentea also had other recommendations for the public at Devoxx. For example, he recommends using Loosely Coupled Classes as much as possible. This means the extent to which the classes depend on each other is kept to a minimum, so that they can work autonomously. Another tip is not to make classes longer than 200 lines and to extract the class as it grows. That too is mainly to increase the readability and ease of maintenance.
View the full session of Victor Rentea here.