The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Pascal Menuet (6) [Avatar] Offline
#1
In the code snippet for the implementation of operator==, we could avoid testing left.is_sentinel() possibly twice, by nesting the ifs:
if (left.is_sentinel()) {
    if (right.is_sentinel()) {
        return true;
    } else {
        // ...
    }
} else if (right.is_sentinel()) {
        // ...
} else {
        // ...
}
Ivan Cukic (104) [Avatar] Offline
#2
Yes, if this was real code, that change would be sensible. Though, most of the time, things like is_sentinel are inlined (in template classes) by the compiler, and it can optimize the branching.

For the book, the snippet server just to demonstrate the point that there are 4 options for each call of the operator== and this layout makes it more readable than having nested if statements.