2

I have the next code snippet, where the sonarlint tool says that my boolean variable sentinel always evaluates to true, and the sentinel = true it's an useless asingment.

import static java.lang.System.*;

public class Sentinel {

    private static final int[] array = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    public static void main(String[] args) {
        boolean sentinel = false;
        int counter = 0;
        while (!sentinel) {
            out.println( "Counter: " + counter);
            out.println( "Array index: " + array[counter] );
            counter++;
            if ( counter == array.length - 1 ) {
                out.println( "End of the array reached" );
                sentinel = true;
                out.println( "Breaking..." );
                break;
            }
        }
    }
}

What could be the issue with the sonarlint analisys? Code compiles perfectly and runs as intended.

Regards.

Update:

@kkk have provided two valuable answers. See below, but I let here the one that i liked more:

import java.util.concurrent.atomic.AtomicBoolean;

import static java.lang.System.*;

public class Sentinel {

    private static final int[] array = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    public static void main(String[] args) {
        AtomicBoolean sentinel = new AtomicBoolean(false);
        int counter = 0;
        while ( !sentinel.get() ) {
            out.println( "Counter: " + counter);
            out.println( "Array index: " + array[counter] );
            counter++;
            if ( counter == array.length - 1 ) {
                out.println( "Counter limit reached" );
                sentinel.set( true );
                out.println( "Breaking..." );
                break;
            }
        }
    }
}
Alex Vergara
  • 1,766
  • 1
  • 10
  • 29
  • 1
    Because of the break statement causing the loop to be exited, `sentinel` goes out of scope before it is read. – tgdavies Dec 22 '21 at 07:50
  • And what about the times that `sentinel` does not goes out of scope 'cause the loop break condition isn't true? That's seems to me a big arbitrary condition on the analisys, and does not reflects the reality – Alex Vergara Dec 22 '21 at 08:26
  • `sentinel` is unnecessary. Having it does not change what your code does. – tgdavies Dec 22 '21 at 10:30

2 Answers2

1

The change made by sentinel = true; is never seen by while (!sentinel). That's because of the break statement.

With that said, your code is much simpler with a for loop. Your while simply makes it complex.

for(int counter = 0; counter < array.length; counter++) {
    out.println("Counter: " + counter);
    out.println("Array index: " + array[counter]);
    if (counter == array.length - 1) {
        out.println("End of the array reached");
        out.println("Breaking...");
    }
}

Or, even better, do the counter == array.length - 1 actions after the loop

for(int counter = 0; counter < array.length; counter++) {
    out.println("Counter: " + counter);
    out.println("Array index: " + array[counter]);
}
out.println("End of the array reached");
out.println("Breaking...");
ernest_k
  • 44,416
  • 5
  • 53
  • 99
-1

make sentinel static or user AtomicBoolean,it's visibility problem

kkk
  • 22
  • 1
  • I don't like first one, have an static class level member on too large code bases can be awful, an dI w'd like to have it inside the method, so AtomicBoolean works perfectly fine and removes the sonarlint analisys error. – Alex Vergara Dec 22 '21 at 08:30
  • 1
    There is no need for it to be `static` or an `AtomicBoolean`. The reason it is saying the value is never used is that, after setting it, the value is never used. The break statement takes you out of the `while`. – user904963 Dec 22 '21 at 08:54
  • The simpler answer is just to remove sentinel all together. – tgdavies Dec 22 '21 at 10:31
  • That code snippet it's resuming another very big piece of code where there is a need to have multiple breaks, but sonarlint complains about not to have multiple break statements in the same while loop, so a sentinel value seems a proper option. So replace the break with the condition evaluating to true and checking the value after the if, and after looping again if the sentinel is not true. – Alex Vergara Dec 22 '21 at 10:37