write well

enjoy writing code | 20 January 2022

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated.

Sometimes the principles are conflicting: explicit names can be long and make code complicated, and ugly looking. So we each have to decide on the right balance.

Write well (consistently) to avoid mistakes

It is important to be consistent. There are many ways to name objects. There are no right or wrong ways. But being consistent has many benefits.

One mistake that I sometimes make is confusing myself by names for index and parameters, I decide to name all indices in the code a single letter in lower case and stick with it. Therefore, in any arithmetic operations involving indices, all terms must be a single lower case letter.

The consistency allows easy checking and make writing code more enjoyable (who likes to live/work in a very messy place).

What Expression Example
index single lower case letter i, l, r, m
array or list single upper case letter A
function name words with first letter in lower and rest proper case bSearch