Reasons and Ways to Improve Code Quality — Venkat Subramaniam
Given below is the summary of the talk given by Mr Venkat Subramaniam at Devternity 2022.
What is a Good Quality Code?
- Code quality is inversely proportional to the effort it takes to understand it.
Why focus on Quality?
- First Law of Programming
Lowering the code quality lengthens development time.
Negotiate and Slow down the pace initially, to gain speed and improve quality eventually.
- Agile Development
We can’t be agile if our code stinks.
Code which is not adaptive or agile is not sustainable for development.
- Humanity
Good code is about empathy and humanity.
I am empathising with the person who is doing the next pull.
Before you push the code, make sure it doesn’t slam on the person behind you.
It is essentially how we convey the way we feel about our colleagues
- Economic Impact
The cost of maintenance is more as quality decreases.
We write code once but make sure to read it many times.
Optimize for reading rather than writing.
A good code reads like a story, not a puzzle.
Make the code work and make it better real soon.
Write code, and before you move forward, think about how others feel reading it.
Look for the flow in the code.
Does it logically move the reader forward from the unknowns to the knowns that they can gather along the way?
It makes it easier to understand what is happening in the code.
Knowing the need to write quality code, doesn’t necessarily translate to doing it. We need to motivate ourselves and others to keep improving.
Ways to Improve Code Quality
- Focus on the relevant
Create relevant working software — Is it serving the needs of the customer and providing value?
Code what is only necessary, aim for a Minimalist code and design
Be mindful of what edge cases need to be handled and what level of extensibility the code should have rather than accounting for all of it in places it seems unwanted.
Single Level of Abstraction Pattern > Single Responsibility Principle
The length of the functions doesn’t matter if the abstractions are at the same level, thereby reducing the cognitive load on the readers.
Usually, a compose method pattern is feasible.
- Mind the extensibility trap
Extensibility is important but when misused ends up in unnecessary complexity.
Don’t arbitrarily design towards extensibility without the end goal in mind.
Using coding principles and design patterns without context leads to unnecessary complexity.
- Reduce dependencies
Low coupling and loose coupling
Fewer is better than a lot when it comes to dependencies.
Focus on stability and frequency of change.
Automated tests are early signs of design quality.
Move dependencies as much as possible to higher levels so that interactions are coarse-grained rather than fine-grained.
- Write clear code rather than clever code — Create clear solutions
- Don’t comment to cover up bad code
Try to refactor rather than cover up bad code with comments.
Write comments not to tell what the code is doing but to know why it was written so.
The code should be self-documenting. Another form of documentation is tests which tell what the code is doing and how it behaves.
- Refactor Continuously
- Make visible your technical debts
List technical debts in order of diminishing impact.
Publish them in a common space.
Schedule time to pay them off.
- Do tactical code reviews
Start by reviewing tests, then review the code.
When you find a bug, don’t write about it, write a test for it.