Keeping code quality is utmost important to any developer / architect. However, being on top of it always is a greater challenge.So is there any easier way?
As per my experience setting up right process and following simple practices really helps. Here are my most common observations and very minimum must have for maintaining healthy code.
Follow coding standards and use code quality tools
Following coding standards is another important factor. For example, consistent naming convention for controllers, services etc helps in understanding and navigating code base quickly.
Code quality tools like jshint, checkstyle, pmd, jacoco, findbugs, jdepend also plays really important rule in maintaining code quality. Personally, I prefer tools like jshint etc. which can tell the developer a lot of things about the code before even developer checks in the code. It really helps in fixing the issues right in IDE. Other good pattern which works really well is integrating these tools in your developer’s IDE’s in some way so that IDE can flag the errors right away.
Follow code review process diligently
Last but not the least. Code reviews really helps in maintaining consistency and ensuring best practices are followed.
I have not covered some of the other practices like continous integration/delivery and having centralized code quality tools like SONAR. I will leave it for some other day.
Overall, It takes some time to set the overall development process and practices. Once done, it really gives peace of mind and confidence to each develop in the team.
Promote test driven development and have good test automation infrastructure
Writing good and valid tests are really important. I have seen lot of projects without any kind of test infrastructure. It has lot of side effects. One of the prominent ones is maintaining and onboarding new developers becomes really hard. Whenever I work on a new project, running unit tests, integration tests are first steps I do which help me become more comfortable and confident about the code I am working on.
Also, having right amount and valid unit, integration, functional and smoke tests are really important for a healthy code.
Minimize the cyclomatic complexity
We should also strive to minimize the cyclomatic complexity of the code. Cyclomatic complexity is the number of independent paths through a given piece of code. Very simple method without any conditions will have cyclomatic complexity of 1. If you introduced an if condition method’s cyclomatic complexity would now be two, since there are two different code paths to traverse the method.
As the cyclomatic complexity in a unit of code grows, it becomes really hard to understand different code paths. There are various techniques and patterns we can use to reduce cyclomatic complexity of the code.
Promote loose coupling and high cohesion
Following loose coupling and other SOLID principles is another very important point. I have seen lot of code bases where business logic was written in front end tier or front end context was passed to business tier etc. Segregating your code by appropriate tier really helps. Eventually, these applications tends to become monolithic applications.
Other Important thing I believe in is in practice of avoiding premature optimization.
Promote good logging practices
Log based analytics plays really important role for auditing and troubleshooting the issues. I have seen a lot of code bases where troubleshooting of issues became really hard due to inadequate logging. Having right granularity for logging becomes very important. Taking some amount of time and thinking about what we want to log for auditing saves lots of headache later. In case of distributed systems, having strategy for distributed tracing and passing common requestId, SessionId across systems helps in creating complete traversal flow.
In a nutshell
having code review process, coding standards and promoting practices like test driven development, loose coupling really helps.