The key to clean code

August 08 · 2 mins read

In the introduction videos of the Nand2Tetris Part 1 course, Noam Nisan and Shimon Schocken said that the main secret of computer science is this:

Don’t worry about the “how”, only about the “what”.

In other words, don’t worry about the “implementation”, only about the “abstraction”.

The Main Secret of Computer Science

(Please don’t misinterpret their usage of the phrase “don’t worry”. To better understand what they mean, You have to watch the entire introduction video, or visit their site.)


Steve Freeman and Nat Pryce, the authors of the GOOS book, said something similar in page 252 of the book:

All code should emphasize “what” it does over “how”, including test code.


Jonathan Boccara also said that it is “the principle to rule them all”:.

…respecting levels of abstraction… , [focusing on] what a particular piece of code intends to do as opposed to how it is implemented… is the one principle to rule them all because it automatically applies all the best practices… When you follow it, you’ll find yourself naturally writing code with a high-quality design.

But of course, knowledge is only potential power, so… sad life… but at least now we can have a direction…

An example:

When computing the sum of a list of numbers, it is easier to read this

sum = listOfNumbers.computeSum();
// use sum here

or this

sum = computeSumOf(listOfNumbers);
// use sum here

than this

sum = 0 
index = 0
while index < listOfNumbers.length {
    n = listOfNumbers[index]
    sum += n
    index++
}
// use sum here

The first two code snippets focuses on what the code does instead of how it does it. They are shorter compared to the third one. They have intention-revealing names, which make them easier to read.

Buy Me A Coffee