Note: Most of these blogs are for my personal reference and at a given time, some of those might just be unpolished drafts.
On Code Readability
Almost everyone starts off coding in a very imperative fashion. You figure out the rules, syntax of the language and you figure out the logical solution to the problem and start off coding. As you start doing so you discover some idiosyncracies of the language and you start focusing on making the code more and more ‘concise’. If unchecked this can lead to obfuscation where you just increase the cognitive load of the program. You write a very smart-ass and clever piece of code which can take hours to decipher. The code is concise and does what you want but think in terms of the maintainer or even you yourself, fiew years/months down the line. May be you switched to another programming language and are probably thinking ‘who the fuck wrote that?’ until you ‘git blame’ it and find that it’s you after all.
“Programs must be written for people to read, and only incidentally for machines to execute.”— sudip bhandari (@sudip_post) January 18, 2020
― Harold Abelson, Structure and Interpretation of Computer Programs #code #readability #programming #goodcodingpractice#codingisfun
After a series of discussions that ensued with this tweet I had the following insights:
Every programmer aims to writes concise and unambiguous code. (fair enough)
To do so you have to be familiar with the nitty gritty details of the language you are writing in. (You can’t expect anyone to write code that is ‘readable’ to a code-illiterate person) (fair enough)
You start over-emphasizing on 2 and start becoming more esoteric, using the hacks of the language to do some operation. Following is a lame js example:
Array [ "ram", "sita" ] b[Number(4>5)] "ram" b[Number(4<5)] "sita" //This works but why would anyone do it?
- Readable code is not short code. Readable code is not extra verbose code either. Being programmers we should be fairly literate in this regard and we shouldn’t load our cognition with unnecessary details. Likewise we shouldn’t load it with esoteric expressions either