The Art of Readable Code - by Dustin Boswell and Trevor Foucher

Hello,

In this post, I will continue to share my understanding on the Part-2 of the book "The Art of Readable Code" by Dustin Boswell and Trevor Foucher.

#1. Simplifying Loops and Logics

The first chapter of this amazing book dealt with general topic such as variable name, function name and comments. In the second chapter, the authors explain about making the actual logic easy to understand.

The authors first start with a simple if condition. They suggest having the variable in question always on the left-hand-side of the condition as it more colloquial and readable. During a comparison between two values like a range, the developers have to go with having the variable in middle. Some examples are as

if (taxAmt > 999)
if (1 <= taxPerc <= 15)

Also, about the order of your condition, start with the positive or easier criteria in your logic and proceed further. Also, the authors warn about using the ternary operator. They suggest using it only if the condition is simple and normal. Although there are differences in opinion about this operator in general, it is better to use it on simple occasions.

name_Pref = (str_Gender == 'MALE')? 'Mr' : 'Ms'

Regarding a normal while loop and a do-while statement, it is always better to use the former. It is easier, useful and very natural to have the condition in the first like all other loop logic. Also, when the reader goes through your code, in case of the do-while they have to read the code twice.

- The Goto
Like the ternary operator, it good if you use this feature very little and only needed. Evidently, over usage of goto will make your program much harder to read and understand. But when used at correct places, it is very effective feature.

if (tax_Percent == 0) goto print;
...
print:
	PrintInvoice();

- Nested loops
Sometimes the program cannot be done without using such complex features, but generally the use of nested loops can be minimized if the requirement is understood well. According to the authors, it wise to use other ways and depend on nested only if there is no other way.

# Breaking Down Giant Expressions

- Summary Variables
Just because it’s possible, we don’t have to write a complex single line code or function. Even the same developer coming back to debug his code might find it very hard after a period of time. So when you initially develop your login, it is better to break it into meaningful chunks.

ReadInput();
ValidateInput();
LogicComputation();
PrintOutput();

The authors suggest the use of summary variables to break down a complex equation and store the intermediate values in a temporary place for easier understanding. When using complex logics in a single line or function, it ads extra burden to the reader. The developer may be confident about its readability at beginning, but as mentioned earlier, after to move on to other functions or phases of project and come back for a fix or analysis, it might be hard for the same person.

When breaking down your logic to smaller pieces, it good for the developer to maintain, find bugs, typing errors, useful conditions and cases to be handled in particular.

# Variables and Readability

The use of temporary variables must handle with great care. Only used when needed and at correct places. When used at unwanted places, they tend to increase the complexity of the program and increase resources also. The variable name, importance in other aspects of the variable should depend on its scope.

Just like goto statements, the break and continue features of the loop can be very useful when used correctly and aptly. Also, when over used or poorly coded they will increase the complexity of your code in many degrees.

Regarding the scope of your variable, it is always better to limit or minimize it as much as possible, in other words it is good practice to limit the usage of global variables. The authors suggest to rethink the use of global variable in any language and to use local variables as much as possible. Also, write once variables as constants helps in easy understanding and maintenance of your program.