A Nested If Statement Checker

by krishnamoorthyb


The book ‘Beautiful Code’ edited by Andy Oram and Greg Wilson is a wonderful collection of articles in programming.  I recently attended a student presentation on an article from the book titled ‘Code in Motion’ by Laura Weingerd and Christopher Seiwald.  The author discusses what in his opinion are the seven pillars of beautiful code.  One of them is to avoid deep nested if statements. Deep nested if statements can confuse the reader about the list of conditions under which sections of code execute. I got to thinking about this problem and i feel there is a solution that can be provided by augmenting the language compiler or by adding ‘code reading intelligence’ to a code editor. The basic idea is to have a decode function that would take a list of conditions as a parameter and return a binary code where each bit is one or zero depending on whether the corresponding condition evaluates to true or false. The following example should help clarify my point

Let us consider the following snippet of C code

if (a<b) {

if (c>d){

if (e == f){

action 1

} else {

action 2

}

}

action 3

}

the above code could neatly expressed as

bitmapCode  = decode(“a<b”, “c>d”, “e==f”)

the following will be the valid values of code

The position of the parameters in the decode function call will have a bearing on the code returned.

In this example condition 1 is “is a < b? “, condition 2 is “is c greater than d?” and condition 3 is “if e equal to f?”

code = 000 if none of the conditions are true

code = 111 if all conditions are tur

code = 100 of condition 1 is true and conditions 2 and 3 are false

code = 110 if conditions 1 and 2 are true and 3 is false

code = 101 if conditions 1 and 3 are true and 2 is false and so on….

We can have a switch statment to process the code. This would make the code much more easier to read. This approach, however, has a drawback.  Function calls impose runtime context switch overhead. One way to overcome this problem is via inline functions.  The other radical approach is to have an editor that can provide multiple ‘intelligent’ view of a program. I will write about this in a seperate article.

I have a version 1 rough implementation of this scheme in VB.NET.  The implementation consists of two classes. The condition class implements an abstraction of a relational expression. The class maintains an LHS (left hand side), a RHS (right hand side) and a relational operator (<,==,>,<> etc…) The class also implements a method to test if the condtion is true. The second class is a decode class which is a container class for a list of conditions. The main responsibility of the decode class is to maintain a list of condtions and return a code reflecting the conditions.  I am uploading a pdf file of the source code along with this post. The condtion class right now stores the LHS and RHSas objects. I am planning to use generics and overload the relational operators in a future version.

The code has been tested to work on numeric and string data. The code can be downloaded here

Advertisements