On Friday, 28 December 2012 11:05:06 UTC+2, Juha Nieminen wrote:
> Being forced to construct such a
> complicated object can in some cases be completely unnecessary, for
> example in situations like
>
> SomeType foo()
> {
> if(something)
> someCodeHere;
>
> throw("this should never happen!");
> }
That is the optimistic "if things are good then we rock" style that
leaves bad things to end. Typical branch prediction in processors is
however also optimistic and hopes that branch won't be taken. So
better might be pessimistic style "eventually all things go wrong":
SomeType foo()
{
if (!someThing)
throw "there must be always something!";
switch (someOtherThing)
{
default: // always have default
case BadValue: // problems first
throw "bad or unhandled case

";
case GoodValue1:
// ... react to GoodValue1
break;
case GoodValue2:
// ... work with GoodValue2
break;
// ^ so maintainer can't forget to add break
// <- when he adds new case here
}
// ... rest of the code here
// very last thing is return of good result
}
As bonus that style also suppresses most warnings presence (or lack of)
whose this thread is about.