Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the dead code come from?

I've got a problem, I'm getting a "Dead Code" warning in Eclipse and I really don't know why. The code is from my Connect Four project, to be more precise it's from the Class that checks if somebody has won. This method checks all the horizontal winning possibilities for red. The code is the following:

/**
 * Method to check the horizontal winning possibilities for red
 * @return true if red won or false if not
 */
public boolean checkHorRed(){
    for(int line = 0; line < 6; line++) {
        for(int column = 0; column < 4; column++) { //column++ is underlined and causes the "dead Code" warning
            if(gw.buttons[line][column].getIcon().equals(gw.red));
                if(gw.buttons[line][column+1].getIcon().equals(gw.red));
                    if(gw.buttons[line][column+2].getIcon().equals(gw.red));
                        if(gw.buttons[line][column+3].getIcon().equals(gw.red));
                            return true;
        }
    }
    return false;
}    

The game is even caused to be immediately won because of this method. What's strange about this is that all the other methods in the class that look almost the same don't cause any problems. Here's the method that checks the vertical winning possibilities for yellow, to have a comparison:

/**
 * Method to check the vertical winning possibilities for yellow
 * @return true or false
 */
public boolean checkVertYel(){
    for(int line = 3; line < 6; line++) {
        for(int column = 0; column < 7; column++) {
            if(gw.buttons[line][column].getIcon().equals(gw.yellow))
                if(gw.buttons[line-1][column].getIcon().equals(gw.yellow))
                    if(gw.buttons[line-2][column].getIcon().equals(gw.yellow))
                        if(gw.buttons[line-3][column].getIcon().equals(gw.yellow))
                            return true;
        }
    }
    return false;
}    

This one does not cause any problems. Can somebody tell me where the warning comes from? If you need additional information please tell me.

like image 462
Lunaetic Avatar asked Nov 09 '22 23:11

Lunaetic


1 Answers

The dead code in your function is the increment statement of your inner for loop (column++). The return true statement will always be executed (if the loop is executed), so the loop increment is never going to happen.

That is your code, but properly formatted:

// ...

for(int column = 0; column < 4; column++) {
    //column++ is underlined and causes the "dead Code" warning
    if(gw.buttons[line][column].getIcon().equals(gw.red));

    if(gw.buttons[line][column+1].getIcon().equals(gw.red));

    if(gw.buttons[line][column+2].getIcon().equals(gw.red));

    if(gw.buttons[line][column+3].getIcon().equals(gw.red));

    return true;
}

// ...

You can easily spot the error: return true will always be executed, so the increment statement of the inner loop will not be executed.

That is how you code should look like:

public boolean checkHorRed() {
    for(int line = 0; line < 6; line++) {
        for(int column = 0; column < 4; column++) {
            //column++ is underlined and causes the "dead Code" warning
            if(gw.buttons[line][column].getIcon().equals(gw.red)
                    && gw.buttons[line][column+1].getIcon().equals(gw.red)
                    && gw.buttons[line][column+2].getIcon().equals(gw.red)
                    && gw.buttons[line][column+3].getIcon().equals(gw.red) {
                return true;
            }
        }
    }

    return false;
}
like image 82
ifloop Avatar answered Nov 15 '22 13:11

ifloop