0

I have this bit of code and eclipse is telling me that the boolean expressions can be simplified.

    knightTurn == true && occupied ==true

and

    knightTurn == true

both boolean knightTurn and boolean occupied are set to false earlier in the code.

    boolean knightTurn=false

    boolean occupied=false

    @Tags({"passed"})
public void passed () 
{
    final int x = 50;
    if (knightTurn == true && occupied == true) {
        for (int i=0; i<x; i++) {
            standKnight.moveAvatar(i, 0);
            avatarEditor.refresh();
            i++;
        }
        occupied = false;
    }

}

@Tags({"failed"})
public void failed () {
    final int x = 35;
    if (knightTurn == true) {
        for (int i=0; i<x; i++) {
            standKnight.moveAvatar(i, i/2);
            avatarEditor.refresh();
            i++;
        }
        occupied =  false;
    }

The code still runs as it should, but how do I tidy up this bit and make it simpler?

2 Answers2

3

For a boolean, you can use it directly in the expression without explicitly evaluating it to true.

 if (someBooleanVariable && someOtherBoolean){
    // run condition

 }
ergonaut
  • 6,929
  • 1
  • 17
  • 47
3
 if (knightTurn == true && occupied == true)

Can be written as

 if (knightTurn && occupied)

This also reduces the possibility of making mistakes like these

if (knightTurn = true)
Ankit Deshpande
  • 3,476
  • 1
  • 29
  • 42