Code is not just to tell the computer what to do. It is also to tell the human what the computer is doing.
The computer doesn't care about quality of communication. The human does.
If you throw horrible code at the computer, the computer will execute the code. If you throw horrible code at the human, the human will execute you.
- Me
We have always been able to tell the computer what to do in assembly language itself. But we ended up inventing and using nice languages, with higher layers of abstraction, to make sure we humans can easily understand what the code is doing.
When we make new languages, we mostly tell ourselves - "we're making this new language so we can tell the computer to do things more easily/safely/performantly".
In reality, most of the time, we are actually building them so that "the human can more easily communicate to the computer to do things more easily/safely/performantly".
An easy example is - We can write memory safe code in C - it is hard, but it can be done. But, rust is better in this regard because it makes writing memory safe code easier for the human.
It is always for the human.
Look back at all the programming talks you've watched from this perspective. They rarely talk about how to communicate well to the computer. Most of the time, they talk about how to communicate well to the humans reading the code.
So, write code that communicates well. Do whatever it takes to make sure the human understands the code without headaches.
- Refactor it
- Simplify it
- Add in comments that tell the "why"
- Read books
- Give clear names to variables, that clearly tell what they store
- Give clear names to functions, that clearly tell what they do
- Watch this amazing channel CodeAesthetic - youtube
- Read books like The pragmatic programmer etc
Optimize for communication.
- Notice the code written by veteran programmers- it all feels so easy and simple. That's because the veteran's code is communicating fluently to you.
- Notice the complex & tricky one-liner code in codebases. It is usually written by people trying to look clever by flaunting their programming.
When you come across a situation where you have to choose between readability vs performance, choose readability. If that code is extremely critical, and you must choose performance - put in comments to improve readability.
Good code communicates well. It tells you clearly what it is doing. It looks borderline stupid. It allows you to stay brainless.
Code is communication.