Work is guaranteed: the logical chains of the expressions AND and OR are evaluated from left to right, and if the first subexpression satisfies the condition, further subexpressions are not evaluated.
In your case, if currentNode is null, it will never be dereferenced by the second subexpression, so the code will be safe.
As @jdv pointed out, this is called a short circuit rating, not a lazy rating. The latter is a programming method when you, transparently to the client, calculate the required value only for the first time when it is specifically needed. The simplest example:
class Example { SomeClass *theObject = null; public: SomeClass *getTheObject() { if (!theObject) { theObject = doResourceConsumingCalculation(); } return theObject; } };
Note that the Example client is not aware of the implementation details that theObject evaluates lazily, so you can freely switch back and forth between impatient and lazy evaluation without affecting the public interface of the class.
(Of course, in real production code, getTheObject should be implemented in a separate cpp file, and it should probably include synchronization, error handling code, etc. This is just a simplified example getTheObject
Péter Török
source share