EntropySink

Technical & Scientific => Programming => Topic started by: Mike on December 11, 2014, 11:11:35 PM

Title: Shit code that gets in when you don't have code reviews
Post by: Mike on December 11, 2014, 11:11:35 PM
I found this gem today (reduced down just a little):
Code: [Select]
<?php

switch (true) {
    case (
$perm->CanRead('permission1') || $perm->CanRead('permission2')):
       break;

    case (
$some_other_condition_expression):
        break;

    case (
$one_last_expression):
        break;
}

I was floored.  I was amazed that (and sickened) that first PHP allowed that and second that someone actually thought it was a good idea to use it.  And to top it off, we found that the second two cases were never going to be true and were completely useless.
Title: Re: Shit code that gets in when you don't have code reviews
Post by: hans on December 11, 2014, 11:34:38 PM
I've never seen that technique. Interesting.
Title: Re: Shit code that gets in when you don't have code reviews
Post by: Mike on December 12, 2014, 12:30:56 AM
I hope to never see it again.
Title: Re: Shit code that gets in when you don't have code reviews
Post by: micah on December 12, 2014, 12:49:02 AM
I mean, it kinda makes sense if you're just trying to test which conditions true first in a given order.
Title: Re: Shit code that gets in when you don't have code reviews
Post by: Mike on December 12, 2014, 12:55:51 AM
Been looking around and it appears to be common(ish).

My issue is that it isn't immediately and intuitively obvious what the behavior will be.  I'd go with the if..elseif..else chain, especially in cases where you aren't using case fall through.
Title: Re: Shit code that gets in when you don't have code reviews
Post by: micah on December 12, 2014, 08:37:30 AM
Yeah, it does seem weird.  Was there a default case or is it impossible for everything to be false?
Title: Re: Shit code that gets in when you don't have code reviews
Post by: Mike on December 12, 2014, 09:24:17 AM
No default, cases could all be false.  The one that was found was adding additional where clasuses to the query to restrict it further.