The best code you can write
Code is read many more times than it is written, and its ultimate cost is often very high and paid by someone else.
While it’s difficult to get exact figures for value and cost, asking the following questions will give you insight into the potential expense of a bit of code:
- How difficult was it to write?
- How hard is it to understand?
- How expensive will it be to change?
When is the code easy to understand?
Code is easy to understand when it clearly reflects the problem it’s solving, and thus openly exposes that problem’s domain.
Programmers love clever code. You must resist being clever for its own sake. If you are capable of conceiving and implementing a complex solution, you should accept the harder task and write simpler code.
How can we do code easy to understand
Inconsistent styling makes code harder for humans to parse, it raises costs without providing benefits.
Duplication of logic suggests that there are concepts hidden in the code that are not yet visible because they haven’t been isolated and named. When you remove duplication of your code, you do it more DRY, and to do it you need to introduce some abstractions.
Concepts in the code should be extracted and named. You should name methods not after what they do, but after what they mean, what they represent in the context of your domain. Naming the method at this slightly higher level of abstraction isolates the code from changes in the implementation details.
- This concept was originally found at the book 99 Bottles of OOP by Sandi Metz & Katrina Owen.