Encapsulate Loop Conditions

Sometimes the termination condition of a loop is independent of the data being manipulated in the loop. One example occurs when you are looking for an external trigger such as an event, signal, or sensor reading to stop. Imagine you were writing the preheat controller for a commercial oven. Simplified pseudo-code for the initial heating cycle might look something like Listing 8-3.

Listing 8-3: Simple pseudo-code for externally triggered loop termination

while(underSetTemperature()) {  keepHeating();}

We have changed the problem domain slightly, but this example closely resembles the example from the previous section. The underSetTemperature() method encapsulates the logic around continuing ...

Get Quality Code: Software Testing Principles, Practices, and Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.