Where does the dead code come from?

I have a problem, I get a "Dead Code" warning in Eclipse, and I really don't know why. The code from my Connect Four project, more precisely, from a class that checks to see if anyone has won. This method checks all the possibilities of horizontal gain for red. The code is as follows:

/** * 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 by the fact that it was immediately won because of this method. What is strange is that all the other methods in the class that look almost the same do not pose any problems. Here's a method that tests the vertical win opportunities for yellow, 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 does not cause any problems. Can someone tell me where the warning comes from? If you need more information, please let me know.

+5
source share
3 answers

The dead code in your function is the increment operator of your inner for ( column++ ) loop. The return true will always execute (if the loop is executing), so the loop increment will never happen.

This is your code, but formatted correctly:

 // ... 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 detect an error: return true will always be executed, so the increment instruction of the inner loop will not be executed.

Here's how you should look like this:

 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; } 
+1
source

In the above method you have; after each if statement, where when your second method is correct, which is the correct way.

  if(gw.buttons[line][column].getIcon().equals(gw.red)); <-- 

This completes if he himself. Your equivalent line of code

  if(condition) { } 

This means that the code after the if condition is dead.

0
source

This is your code after formatting it:

 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; //this will always happen } } return false; } 

And this is different:

 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; } 

Basically, you really shouldn't end your if with semicolons.

0
source

Source: https://habr.com/ru/post/1205847/


All Articles