Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason to avoid the sentinel pattern in Java?

I've heard people advise that one should always use the Iterator pattern to control loops rather than throwing an exception (which is how it's done in Python iterators) or using the Sentinel pattern, whereby a special, sentinel value (often null) is returned to indicate the end of the iteration.

Does best practice advise against the sentinel pattern? If so, why? (other than it not working with the foreach syntax in Java 1.5).

Edit: Code example 1 - Sentinel Pattern

Reader r = ...;
for( int val = r.read(); val != -1; val = r.read()) {
   doSomethingWith(val);
}

Code example 2 - Iterator Pattern

for(Iterator<Thing> it = getAnIterator() ; it.hasNext(); ) {
  Thing t = it.next();
  doSomethingWith(t);
}
like image 720
Jim Downing Avatar asked Nov 10 '09 17:11

Jim Downing


People also ask

Why is sentinel value is required?

The sentinel value is a form of in-band data that makes it possible to detect the end of the data when no out-of-band data (such as an explicit size indication) is provided.

What is the purpose of sentinel value in loop structure in Java program?

In a programming context, “sentinel” is a specific value used to terminate a condition in a recursive or looping algorithm. Sentinel value is used in many ways, such as dummy data, flag data, rouge value, or signal value.

What is sentinel and what is its use?

a person or thing that watches or stands as if watching. a soldier stationed as a guard to challenge all comers and prevent a surprise attack: to stand sentinel.

Which type of loop can use a sentinel value to know when to stop?

Today you will learn about while loops with sentinel values. A sentinel value denotes the end of a data set, but it is not part of the data. A loop that uses a sentinel value is called a sentinel-controlled loop.

What is sentinel pattern?

The Sentinel Object pattern is a standard Pythonic approach that's used both in the Standard Library and beyond. The pattern most often uses Python's built-in None object, but in situations where None might be a useful value, a unique sentinel object() can be used instead to indicate missing or unspecified data.

What is a sentinel controlled loop in Java?

Sentinel-controlled repetition is sometimes called indefinite repetition because it is not known in advance how many times the loop will be executed. It is a repetition procedure for solving a problem by using a sentinel value (also called a signal value, a dummy value or a flag value) to indicate "end of data entry".


2 Answers

The issue with the Sentinel pattern is that it explicitly excludes the sentinel value from the set of values that are valid elements. I.e., if you have a list of objects which can validly contain null as an element, using null as a sentinel value is a fail.

like image 199
Paul Sonier Avatar answered Oct 03 '22 07:10

Paul Sonier


Exceptions should only be used in "exceptional" situations. Concluding or breaking out of a loop is normal program flow, not an exceptional situation.

Additionally, when working in Java, (or any language for that matter), you want to use patterns and conventions that are common and well known in the community because other Java programmers may need to maintain your code. If they do, they will most likely expect to see Iterators, not the sentinel pattern.

like image 25
Asaph Avatar answered Oct 03 '22 09:10

Asaph