Be careful what you wish for...



  • cause you might just gonna get it...

     

     I want to share this piece of PHP that I just found with Google Codesearch. Nothing too fancy, no major WTF but I really liked it.

     

     // This is code for the thedailywtf.com, but it works fast:
    switch ($accreditation['level']) {
    case 0: $menu_tree[$accreditation_id] = $accreditation; break;
    case 1: $menu_tree[$last_accreditation_id[0]]['mentry'][$accreditation_id] = $accreditation; break;
    case 2: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$accreditation_id] = $accreditation; break;
    case 3: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$accreditation_id] = $accreditation; break;
    case 4: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$accreditation_id] = $accreditation; break;
    case 5: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$accreditation_id] = $accreditation; break;
    case 6: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$accreditation_id] = $accreditation; break;
    case 7: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$last_accreditation_id[6]]['mentry'][$accreditation_id] = $accreditation; break;
    case 8: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$last_accreditation_id[6]]['mentry'][$last_accreditation_id[7]]['mentry'][$accreditation_id] = $accreditation; break;
    case 9: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$last_accreditation_id[6]]['mentry'][$last_accreditation_id[7]]['mentry'][$last_accreditation_id[8]]['mentry'][$accreditation_id] = $accreditation; break;
    case 10: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$last_accreditation_id[6]]['mentry'][$last_accreditation_id[7]]['mentry'][$last_accreditation_id[8]]['mentry'][$last_accreditation_id[9]]['mentry'][$accreditation_id] = $accreditation; break;
    }

     

     Maybe he wanted to take part in a "coding some stairs" contest.
     



  • [quote user="Nachoo"]

    cause you might just gonna get it...

     

     I want to share this piece of PHP that I just found with Google Codesearch. Nothing too fancy, no major WTF but I really liked it.

     

     // This is code for the thedailywtf.com, but it works fast:
    switch ($accreditation['level']) {
    case 0: $menu_tree[$accreditation_id] = $accreditation; break;
    case 1: $menu_tree[$last_accreditation_id[0]]['mentry'][$accreditation_id] = $accreditation; break;
    case 2: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$accreditation_id] = $accreditation; break;
    case 3: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$accreditation_id] = $accreditation; break;
    case 4: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$accreditation_id] = $accreditation; break;
    case 5: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$accreditation_id] = $accreditation; break;
    case 6: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$accreditation_id] = $accreditation; break;
    case 7: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$last_accreditation_id[6]]['mentry'][$accreditation_id] = $accreditation; break;
    case 8: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$last_accreditation_id[6]]['mentry'][$last_accreditation_id[7]]['mentry'][$accreditation_id] = $accreditation; break;
    case 9: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$last_accreditation_id[6]]['mentry'][$last_accreditation_id[7]]['mentry'][$last_accreditation_id[8]]['mentry'][$accreditation_id] = $accreditation; break;
    case 10: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$last_accreditation_id[6]]['mentry'][$last_accreditation_id[7]]['mentry'][$last_accreditation_id[8]]['mentry'][$last_accreditation_id[9]]['mentry'][$accreditation_id] = $accreditation; break;
    }

     

     Maybe he wanted to take part in a "coding some stairs" contest.
     

    [/quote]

    If I remember my "switch case" functionality correctly, couldn't he have just done this:

                switch ($accreditation['level']) {
                    case  10: $menu_tree[$last_accreditation_id[9]]['mentry'][$accreditation_id] = $accreditation;
                    case  9: $menu_tree[$last_accreditation_id[8]]['mentry'][$accreditation_id] = $accreditation;
                    case  8: $menu_tree[$last_accreditation_id[7]]['mentry'][$accreditation_id] = $accreditation;
                    case  7: $menu_tree[$last_accreditation_id[6]]['mentry'][$accreditation_id] = $accreditation;
                    case  6: $menu_tree[$last_accreditation_id[5]]['mentry'][$accreditation_id] = $accreditation;
                    case  5: $menu_tree[$last_accreditation_id[4]]['mentry'][$accreditation_id] = $accreditation;
                    case  4: $menu_tree[$last_accreditation_id[3]]['mentry'][$accreditation_id] = $accreditation;
                    case  3: $menu_tree[$last_accreditation_id[2]]['mentry'][$accreditation_id] = $accreditation;
                    case  2: $menu_tree[$last_accreditation_id[1]]['mentry'][$accreditation_id] = $accreditation;
                    case  1: $menu_tree[$last_accreditation_id[0]]['mentry'][$accreditation_id] = $accreditation;
                    case  0: $menu_tree[$accreditation_id] = $accreditation; break;
                }
    

     Which, thanks to the wonders of "Fall Through" would jump in at the correct point and execute all stages from there down...

    Okay, maybe that's not completely right (PHP isn't my thing), but something very much like that. 



  • [quote user="Anaerin"][quote user="Nachoo"]

    cause you might just gonna get it...

     

     I want to share this piece of PHP that I just found with Google Codesearch. Nothing too fancy, no major WTF but I really liked it.

     

     // This is code for the thedailywtf.com, but it works fast:

    switch ($accreditation['level']) {

    case 0: $menu_tree[$accreditation_id] = $accreditation; break;

    case 1: $menu_tree[$last_accreditation_id[0]]['mentry'][$accreditation_id] = $accreditation; break;

    case 2: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$accreditation_id] = $accreditation; break;

    case 3: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$accreditation_id] = $accreditation; break;

    case 4: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$accreditation_id] = $accreditation; break;

    case 5: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$accreditation_id] = $accreditation; break;

    case 6: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$accreditation_id] = $accreditation; break;

    case 7: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$last_accreditation_id[6]]['mentry'][$accreditation_id] = $accreditation; break;

    case 8: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$last_accreditation_id[6]]['mentry'][$last_accreditation_id[7]]['mentry'][$accreditation_id] = $accreditation; break;

    case 9: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$last_accreditation_id[6]]['mentry'][$last_accreditation_id[7]]['mentry'][$last_accreditation_id[8]]['mentry'][$accreditation_id] = $accreditation; break;

    case 10: $menu_tree[$last_accreditation_id[0]]['mentry'][$last_accreditation_id[1]]['mentry'][$last_accreditation_id[2]]['mentry'][$last_accreditation_id[3]]['mentry'][$last_accreditation_id[4]]['mentry'][$last_accreditation_id[5]]['mentry'][$last_accreditation_id[6]]['mentry'][$last_accreditation_id[7]]['mentry'][$last_accreditation_id[8]]['mentry'][$last_accreditation_id[9]]['mentry'][$accreditation_id] = $accreditation; break;

    }

     

     Maybe he wanted to take part in a "coding some stairs" contest.
     

    [/quote]

    If I remember my "switch case" functionality correctly, couldn't he have just done this:

                switch ($accreditation['level']) {
    case 10: $menu_tree[$last_accreditation_id[9]]['mentry'][$accreditation_id] = $accreditation;
    case 9: $menu_tree[$last_accreditation_id[8]]['mentry'][$accreditation_id] = $accreditation;
    case 8: $menu_tree[$last_accreditation_id[7]]['mentry'][$accreditation_id] = $accreditation;
    case 7: $menu_tree[$last_accreditation_id[6]]['mentry'][$accreditation_id] = $accreditation;
    case 6: $menu_tree[$last_accreditation_id[5]]['mentry'][$accreditation_id] = $accreditation;
    case 5: $menu_tree[$last_accreditation_id[4]]['mentry'][$accreditation_id] = $accreditation;
    case 4: $menu_tree[$last_accreditation_id[3]]['mentry'][$accreditation_id] = $accreditation;
    case 3: $menu_tree[$last_accreditation_id[2]]['mentry'][$accreditation_id] = $accreditation;
    case 2: $menu_tree[$last_accreditation_id[1]]['mentry'][$accreditation_id] = $accreditation;
    case 1: $menu_tree[$last_accreditation_id[0]]['mentry'][$accreditation_id] = $accreditation;
    case 0: $menu_tree[$accreditation_id] = $accreditation; break;
    }

     Which, thanks to the wonders of "Fall Through" would jump in at the correct point and execute all stages from there down...

    Okay, maybe that's not completely right (PHP isn't my thing), but something very much like that. 

    [/quote]

    Disclaimer: I know absolutely nothing about PHP, so I could be completely off base here:

    The fall-through version of the switch looks good, at least according to C/Java switch conventions.  But why wouldn't you just use a for loop?  I'd much rather have to maintain that, at least if there's a possibility of adding more iterations.
     



  • It's a stair case!



  • I could be wrong, I mean I'm not a doctor or anything...

    if($accreditation['level'] >= 1)
       $menu_tree[$last_accreditation_id[$accreditation['level'] - 1]]['mentry'][$accreditation_id] = $accreditation;
    else
       $menu_tree[$accreditation_id] = $accreditation;

     



  • The WTF isn't poor use of a loop or switch statement. 

    The whole code snippet is crap.  You can paint it a pretty color, and add a little perfume, but it is still crap.



  • [quote user="bullseye"]

    The WTF isn't poor use of a loop or switch statement. 

    The whole code snippet is crap.  You can paint it a pretty color, and add a little perfume, but it is still crap.

    [/quote]

    Or even try to polish it! 



  • [quote user="Nachoo"]

     Maybe he wanted to take part in a "coding some stairs" contest.
     

    [/quote]

    I
    once saw a Java file (actually several) whose import statements (about
    20 of them) were sorted by length.  The longest import statement
    was at the bottom.

    I called it "ski slope code."

     




  • $accreditation['level'] is also more or less the number of dimensions in the array, so I don't think the suggestions given above would work. The loop you would need would be something like this:

     

    $current_node = &$menu_tree;
    for ($i = 0; $i < $accreditation['level']; $i++){
        $current_node = &$current_node[$last_accreditation_id[$i]]['mentry'];
    }
    $current_node[$accreditation_id] = $accreditation;
    

     

    Of course, storing data like this is probably a WTF itself. 



  • [quote user="VGR"]I
    once saw a Java file (actually several) whose import statements (about
    20 of them) were sorted by length.  The longest import statement
    was at the bottom.[/quote]

     I do that (and with C# too), though not with that many imports. It looks nicer, and there are few enough lines you can read it anyway :).
     


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.