Complete Communications Engineering

Logically dead code are branches of software that cannot be reached given the logical conditions.  Finding logically dead code is important because it can indicate the software was not written as originally intended.  So simply, a code change made the branch no longer necessary. 

The code below is an example of logically dead code.  The only potential output values for the function are 0 and 1.

 

int gain_adjust(int input)

{

    int output;

   

    output = 0;

    if (input > 10) {

        output = 1;

    }

    else if (input > 20) {

        output = 2;

    }

    return output;

}

 

 

The question is, under what conditions should the output be 2?  If this is legacy code with no documentation or code comments, it is difficult to determine.  The images below show two potential corrections.  For the function on the left, the output will return 2 when input is between 10 and 19, inclusive.  For the function on the right, the output will return 2 when the input is greater than 20.

int gain_adjust(int input)

{

    int output;

   

    output = 0;

    if (input < 10) {

        output = 1;

    }

    else if (input < 20) {

        output = 2;

    }

    return output;

}

 

int gain_adjust(int input)

{

    int output;

   

    output = 0;

    if (input > 20) {

        output = 2;

    }

    else if (input > 10) {

        output = 1;

    }

    return output;

}

 

 

This is a simple example, but it shows why it is important to document code with a functional description and the requirements.  Utilizing white box testing methods such as, static analysis security testing (SAST) software, will help find logically dead code that code reviews missed. 

At VOCAL we utilize strong coding practices to minimize common coding issues.