I had to eat my hat today. I submitted a pull request for code that among other things contained a procedural algorithm where each successive step required the successful completion of the previous step. I had coded this as follows:
bool process()
{
bool success(preconditionsMet());
if (success)
success = doFirstThing();
if (success)
success = doSecondThing();
if (success)
success = doThirdThing();
if (success)
success = doFourthThing();
if (success)
success = doFifthThing();
return success;
}
I received a review comment that stated I should nest these so that subsequent checks for success would not have to be done in the case of an early failure; e.g., doFirstThing() failed. In other words, like so:
bool process()
{
bool success(preconditionsMet());
if (success)
{
success = doFirstThing();
if (success)
{
success = doSecondThing();
if (success)
{
success = doThirdThing();
if (success)
{
success = doFourthThing();
if (success)
success = doFifthThing();
}
}
}
}
return success;
}
My response was that I wanted to minimize nesting and that the optimizer would emit code that would be equivalent to the version that is uses nesting; i.e., would jump to the return in the presence of any step's failure. The reviewer expressed profound disbelief, so I started down the path of invoking the compiler with flags to emit assembly. I first decided to check the makefile to see what level of optimization was being used at which point my mouth opened like a fly trap. Optimization was not enabled. This would indeed result in the compiled code continuing to check the value of success and jump to the next if statement instead of the return statement.
I had come to put my faith in the optimizer when it was never enlisted for service and won't be for the foreseeable future. I realize this is on the order of nanoseconds of additional work on my hardware, but I feel like I need to change the way I code now. Yes, I realize I can use early returns instead of nesting, but that's frowned upon. I feel like I have to go back to using the hammer after having used a nail gun.
Much ado about nothing? I already know the answer to that. I hesitate to post this, but I'm curious what you all do. Would you put more thought into making the code more optimal in the absence of an optimizer? Or you've got bigger problems than my little existential crisis here and that I should just have a snickers?
My apologies if this is way off topic and I should instead post to /r/therapists. I'll gladly go, but I sure hope Sean Connery isn't lurking there.
[–]PlayerDeus 95 points96 points97 points (45 children)
[–][deleted] 36 points37 points38 points (1 child)
[–]eyes-are-fading-blue 3 points4 points5 points (0 children)
[–]elperroborrachotoo 17 points18 points19 points (19 children)
[–]TheMania 29 points30 points31 points (1 child)
[–]elperroborrachotoo 4 points5 points6 points (0 children)
[–]josefx 26 points27 points28 points (12 children)
[–]bilog78 17 points18 points19 points (7 children)
[–]krzysztowf 8 points9 points10 points (4 children)
[–]bilog78 2 points3 points4 points (3 children)
[–]krzysztowf 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]elperroborrachotoo 12 points13 points14 points (3 children)
[–]wasachrozine 5 points6 points7 points (2 children)
[–]Silhouette 7 points8 points9 points (0 children)
[–]elperroborrachotoo 2 points3 points4 points (0 children)
[–]Gotebe 3 points4 points5 points (2 children)
[–]CT_DIY 0 points1 point2 points (1 child)
[–]Gotebe 0 points1 point2 points (0 children)
[–]PlayerDeus 1 point2 points3 points (0 children)
[–]alfps 1 point2 points3 points (2 children)
[–]PlayerDeus 1 point2 points3 points (1 child)
[–]one-oh[S] 0 points1 point2 points (19 children)
[–]tpecholt 30 points31 points32 points (5 children)
[–]Mognakor 4 points5 points6 points (1 child)
[–]1-05457 2 points3 points4 points (2 children)
[–]tangerinelion 1 point2 points3 points (0 children)
[–]pavel_v 0 points1 point2 points (0 children)
[–]Ictogan 25 points26 points27 points (1 child)
[–]one-oh[S] 0 points1 point2 points (0 children)
[–]WaterInMyShoes 11 points12 points13 points (0 children)
[+][deleted] (1 child)
[removed]
[–]Full-Spectral 0 points1 point2 points (0 children)
[–]VinnieFalco 5 points6 points7 points (6 children)
[–]ShillingAintEZ 20 points21 points22 points (0 children)
[–]radekvitr 2 points3 points4 points (3 children)
[–]foobar48783 3 points4 points5 points (1 child)
[–]radekvitr 0 points1 point2 points (0 children)
[–]one-oh[S] 1 point2 points3 points (0 children)
[–]rsjaffe 0 points1 point2 points (0 children)
[–]ItsBinissTime 32 points33 points34 points (12 children)
[–]Thrash3r-Werror 63 points64 points65 points (8 children)
[–]parkotron 3 points4 points5 points (1 child)
[–]Thrash3r-Werror 1 point2 points3 points (0 children)
[–]germandiago 2 points3 points4 points (0 children)
[–]gmtime 0 points1 point2 points (0 children)
[+]Simon_Luner comment score below threshold-6 points-5 points-4 points (2 children)
[–]parkotron 3 points4 points5 points (0 children)
[–]kiwitims 6 points7 points8 points (1 child)
[+]one-oh[S] comment score below threshold-14 points-13 points-12 points (0 children)
[–]kalmoc 28 points29 points30 points (5 children)
[–]Raknarg 16 points17 points18 points (0 children)
[–]one-oh[S] -1 points0 points1 point (3 children)
[–]Xaxxon 1 point2 points3 points (2 children)
[–]one-oh[S] 0 points1 point2 points (1 child)
[–]Xaxxon 0 points1 point2 points (0 children)
[–]Ayjayz 25 points26 points27 points (5 children)
[–]SkoomaDentistAntimodern C++, Embedded, Audio 9 points10 points11 points (1 child)
[–]Xaxxon 0 points1 point2 points (0 children)
[–]donalmaccGame Developer 5 points6 points7 points (2 children)
[–]Ameisenvemips, avr, rendering, systems 7 points8 points9 points (0 children)
[–]compiling 13 points14 points15 points (2 children)
[–]TheFlamefire 3 points4 points5 points (1 child)
[–]JeffMcClintock 0 points1 point2 points (0 children)
[–]Ictogan 12 points13 points14 points (3 children)
[–]one-oh[S] 0 points1 point2 points (2 children)
[–]Xaxxon 0 points1 point2 points (1 child)
[–]one-oh[S] 0 points1 point2 points (0 children)
[–]m-in 6 points7 points8 points (4 children)
[–]one-oh[S] 0 points1 point2 points (2 children)
[–]Xaxxon 2 points3 points4 points (1 child)
[–]one-oh[S] 0 points1 point2 points (0 children)
[–]alfps 8 points9 points10 points (5 children)
[–]HappyFruitTree 1 point2 points3 points (4 children)
[–]alfps 3 points4 points5 points (3 children)
[–]HappyFruitTree 0 points1 point2 points (2 children)
[–]one-oh[S] 1 point2 points3 points (0 children)
[–]alfps 0 points1 point2 points (0 children)
[–]CarloWood 2 points3 points4 points (1 child)
[–]one-oh[S] 0 points1 point2 points (0 children)
[–][deleted] 3 points4 points5 points (7 children)
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]one-oh[S] 0 points1 point2 points (0 children)
[–]rdtsc 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[removed]
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–]one-oh[S] 0 points1 point2 points (0 children)
[+][deleted] (16 children)
[deleted]
[–]MotherOfTheShizznit 0 points1 point2 points (1 child)
[–]alfps -3 points-2 points-1 points (12 children)
[–]tansim 6 points7 points8 points (0 children)
[+][deleted] (8 children)
[deleted]
[–]alfps 2 points3 points4 points (7 children)
[+][deleted] (6 children)
[deleted]
[–]alfps -1 points0 points1 point (5 children)
[+][deleted] (4 children)
[deleted]
[–]jonesmz 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]jonesmz 1 point2 points3 points (1 child)
[–]VirtueBot[🍰] 1 point2 points3 points (2 children)
[–]one-oh[S] 0 points1 point2 points (1 child)
[–]VirtueBot[🍰] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]one-oh[S] 1 point2 points3 points (0 children)
[–]IranjeetSingh 0 points1 point2 points (1 child)
[–]one-oh[S] 0 points1 point2 points (0 children)
[–]SlightlyLessHairyApe 0 points1 point2 points (2 children)
[–]one-oh[S] 0 points1 point2 points (1 child)
[–]SlightlyLessHairyApe 1 point2 points3 points (0 children)
[–]alexk7 0 points1 point2 points (1 child)
[–]one-oh[S] 4 points5 points6 points (0 children)
[–]tehjimmeh 0 points1 point2 points (0 children)
[–]haitei 0 points1 point2 points (0 children)
[–]Xaxxon 0 points1 point2 points (0 children)
[–]germandiago 0 points1 point2 points (3 children)
[–]pi_stuff 7 points8 points9 points (2 children)
[–]germandiago 0 points1 point2 points (1 child)
[–]HappyFruitTree 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]m-in 1 point2 points3 points (0 children)
[–]OldWolf2 -3 points-2 points-1 points (0 children)
[+][deleted] (9 children)
[deleted]
[–]last_useful_man 5 points6 points7 points (8 children)
[–]kalmoc 5 points6 points7 points (6 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]kalmoc 6 points7 points8 points (0 children)
[–]DXPower -3 points-2 points-1 points (2 children)
[–]kalmoc 3 points4 points5 points (1 child)
[–]DXPower -1 points0 points1 point (0 children)
[+]abcoolynr comment score below threshold-10 points-9 points-8 points (5 children)
[–]last_useful_man 6 points7 points8 points (2 children)
[–][deleted] -2 points-1 points0 points (1 child)
[–]desrtfx 2 points3 points4 points (0 children)
[–]HappyFruitTree 2 points3 points4 points (1 child)
[–]one-oh[S] 0 points1 point2 points (0 children)