* Öö Tiib, on 22.08.2010 04:09:
That is unclear what is buggy there. exitCmd is not handled by
doCommand in original, instead it is handled sort of outside ...
globally. Crap had leaked out of its locality before me. That sort of
confused me. I even commented it with question in code.
"Exit" is a special command because it needs handling at the level that calls
'doCommand'. That level has to stop looping. In your code you're thinking about
'doCommand' perhaps asking the user to confirm, but you stop looping anyway...
So, you can either special-case the signature of 'doCommand' to support "Exit",
or you can just handle it directly. Special-casing is problematic because there
can be two or three such special cases.
It's the KISS principle, Keep It Simple.
You might alternatively consider the weaker example
double sum = 0.0;
cout << explanation << endl;
for( ;; )
{
double const number = numberFromUser( "Number? " );
if( number == 0.0 ) { break; }
sum += number;
}
But in a way it accentuates the issue because here it's a least plausible to put
both the input operation and checking as a 'while' loop condition.
To my eyes the 'while', with side-effect condition, obfuscates what the effect
of the code is, sort of compiling it down to a lower level language, needlessly
extends the scope of a variable, and makes it more difficult to introduce
changes, i.e. reduced maintainability.
Cheers & hth.,
- Alf