Debatable.
Marginally
maybe, and irrelevant anyway.
maybe a bit more performing too! |
Nope.
Can you tell me why shouldn't I use GoTo for a function like that? |
Do you know for a fact that the function will stay like that forever? How much do I need to change it before erroneous and/or unintuitive behavior arises? In particular, how does goto behave when used close to objects with non-trivial destructors?
Personally, I have the policy that if a goto
can be replaced with a different construct at no technical cost (if there's a technical cost it must be weighted it against competing concerns) then it
must be replaced. This prevents goto from proliferating in codebases.
Is there any good reason not to rewrite that code like so?
1 2 3 4 5 6 7 8 9 10
|
int something;
while (true){
cout << "Enter something: ";
cin >> something;
if (cin)
break;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "invalid answer!\n\n";
}
|
I can think of a few reasons to change it:
1. People don't immediately reject while loops, even if while (true) is often cause for pause.
2. This code behaves reasonably around common program-structure-sensitive constructs, such as RAII.