• Anon (unregistered)

    Well on embedded systems without a filesystem, etc., etc....

  • (cs)

    Maybe the developer didn't know about else-if, but apparently, Rob (the submitter) didn't know about the switch statement either.

  • Eli (unregistered)

    Yeah, this code sucks. He should have used do { ... } where (false) so he didn't have to break at the end!

    CAPTCHA: nulla - stereotypical Italian undefined value.

  • MycroftMkIV (unregistered)

    He also seems to have no clue about the C++ switch statement....

  • (cs)

    I'm not familiar with C++, but wouldn't a switch() be better in this case than an if-elseif, because you're testing the same variable?

    (3rd attempt... when will the human finally look into this?)

    Edit: I've cracked it: The errors are there to give the ninjas the time to do their thing!

  • (cs)

    What's wrong with a swtich/case construct?

  • (cs) in reply to Anon

    Wow. Just, wow.

  • (cs)

    Any procedural solution for what this code is attempting would be smelly. However, I must say this is probably one of the ugliest ways to do it.

  • Anonymous coward (unregistered)

    Don't know if it was mentioned already, but what about switch?

  • (cs)

    Else if and switch sound good, but what are you going to do when mainType == 7 AND mainType == 9, huh?

  • (cs)

    As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

    It could be written:

    if(mainType == 7)
        subType = 4;
    if(mainType == 9)
        subType = 6;
    if(mainType == 11)
        subType = 9;
    

    There's no potential overlap so no NEED for an else (even if it might make it more readable).

  • MainCoder (unregistered) in reply to steenbergh

    I tend to avoid switch constructs myself, unless each case ends with a return statement (e.g. the switch is the main part of some conversion function). The reason being that I dislike having breaks in each case, as well as avoiding potential logic bugs that can arise due to forgetting one.

  • Ed (unregistered) in reply to FriedDan
    FriedDan:
    As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

    It could be written:

    if(mainType == 7)
        subType = 4;
    if(mainType == 9)
        subType = 6;
    if(mainType == 11)
        subType = 9;
    

    There's no potential overlap so no NEED for an else (even if it might make it more readable).

    But what if mainType is 7, 9 AND 11? Then you're just screwed!

    CAPTCH: dignissim (missingID backwards, perhaps?)

  • sjakie (unregistered)

    Why can you mortals not accept this highly optimized piece of code? Everybody knows switch statements are a CPU killer, even worse than an if...else construct. This sub contractor really knew what he was doing, although I expect he would have rather written it in a proper language like assembly to begin with.

    CAPTCHA - Genitus - somebody with highly intellectual genitals?

  • (cs) in reply to halcyon1234
    halcyon1234:
    Else if and switch sound good, but what are you going to do when mainType == 7 AND mainType == 9, huh?
    That could actually be possible if it was a volatile variable that is tied to some piece of memory mapped hardware. But I'd rather not think about that.
  • (cs)
    while(subcontractor==nitiwt)
    {
      if (Paula == "Brillant")
        return true;
      if (Table == "wooden")
        return true;
      if (XML == true)
        return true;
      if (Post == "FIRST")
        return null;
      if (Post == "FRIST")
        return subcontractor;
    }
    
  • (cs) in reply to Ed
    Ed:
    FriedDan:
    As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

    It could be written:

    if(mainType == 7)
        subType = 4;
    if(mainType == 9)
        subType = 6;
    if(mainType == 11)
        subType = 9;
    

    There's no potential overlap so no NEED for an else (even if it might make it more readable).

    But what if mainType is 7, 9 AND 11? Then you're just screwed!

    CAPTCH: dignissim (missingID backwards, perhaps?)

    If mainType is 7, 9 AND 11 your screwed anyway because you either have major thread sync issues, or you have landed in an alternate universe.

  • Anonymous (unregistered)

    I think you're right, he probably had no idea about the 'else' clause. Seems unlikely but then it was only the other day that we had someone who didn't know about 'break'. The simple fact is that we're working in a profession where proper understanding is the exception, not the rule.

  • Lennart (unregistered)
    Ed:
    FriedDan:
    As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

    It could be written:

    if(mainType == 7)
        subType = 4;
    if(mainType == 9)
        subType = 6;
    if(mainType == 11)
        subType = 9;
    

    There's no potential overlap so no NEED for an else (even if it might make it more readable).

    But what if mainType is 7, 9 AND 11? Then you're just screwed!

    CAPTCH: dignissim (missingID backwards, perhaps?)

    if(mainType == 11) subType = 9; if(mainType == 9) subType = 6; if(mainType == 7) subType = 4;

    fixed

  • Ed (unregistered) in reply to frits
    frits:
    Ed:
    FriedDan:
    As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

    It could be written:

    if(mainType == 7)
        subType = 4;
    if(mainType == 9)
        subType = 6;
    if(mainType == 11)
        subType = 9;
    

    There's no potential overlap so no NEED for an else (even if it might make it more readable).

    But what if mainType is 7, 9 AND 11? Then you're just screwed!

    CAPTCH: dignissim (missingID backwards, perhaps?)

    If mainType is 7, 9 AND 11 your screwed anyway because you either have major thread sync issues, or you have landed in an alternate universe.

    Damn, thanks for clearing that up. My sarcasm chip must be on indecipherable today.

  • (cs) in reply to Ed
    Ed:
    frits:
    Ed:
    FriedDan:
    As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

    It could be written:

    if(mainType == 7)
        subType = 4;
    if(mainType == 9)
        subType = 6;
    if(mainType == 11)
        subType = 9;
    

    There's no potential overlap so no NEED for an else (even if it might make it more readable).

    But what if mainType is 7, 9 AND 11? Then you're just screwed!

    CAPTCH: dignissim (missingID backwards, perhaps?)

    If mainType is 7, 9 AND 11 your screwed anyway because you either have major thread sync issues, or you have landed in an alternate universe.

    Damn, thanks for clearing that up. My sarcasm chip must be on indecipherable today.

    Sorry, you threw me off by posting your captcha, which usually is an indicator the post will not be funny.

  • Ed (unregistered) in reply to frits
    frits:
    Ed:
    frits:
    Ed:
    FriedDan:
    As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

    It could be written:

    if(mainType == 7)
        subType = 4;
    if(mainType == 9)
        subType = 6;
    if(mainType == 11)
        subType = 9;
    

    There's no potential overlap so no NEED for an else (even if it might make it more readable).

    But what if mainType is 7, 9 AND 11? Then you're just screwed!

    CAPTCH: dignissim (missingID backwards, perhaps?)

    If mainType is 7, 9 AND 11 your screwed anyway because you either have major thread sync issues, or you have landed in an alternate universe.

    Damn, thanks for clearing that up. My sarcasm chip must be on indecipherable today.

    Sorry, you threw me off by posting your captcha, which usually is an indicator the post will not be funny.

    Point taken, sarcastic response retracted ;)

  • the beholder (unregistered) in reply to frits
    frits:
    If mainType is 7, 9 AND 11 your screwed anyway because you either have major thread sync issues, or you have landed in an alternate universe.
    No, he's just using Schrodinger's integers.
  • Drew (unregistered) in reply to ParkinT
    ParkinT:
    while(subcontractor==nitiwt)
    {
      if (Paula == "Brillant")
        return true;
      if (Table == "wooden")
        return true;
      if (XML == true)
        return true;
      if (Post == "FIRST")
        return null;
      if (Post == "FRIST")
        return subcontractor;
    }
    
    You for got
      if(fileSystem == "embedded")
        return "embedAllImages";
    
  • (cs)

    It's been a while since my Computer Engineering classes but from what I remember the reason for using NAND gates instead of OR/AND gates was because they had less transistors making them cheaper. This also meant their propagation delay was lower and thus faster than an equivalent AND/OR circuit.

    I vaguely recall exercises in converting AND/OR circuits into only NAND gates.

  • Consultuning (unregistered)

    Erm... my optimizing mind only can say... why is that nobody is not pointing that branching is mostly unnecessary? All that discussion about switch is puzzling, because the whole thing can be reduced to

    subType = mainType - 3 ;
    if( subType == 8 )
        ++subType;
    

    True, the original code does not alter the value of the subType if mainType is outside the 7,9,11 range. Let's add another twist, assuming that mainType is an integer

    if( mainType > 6 && mainType < 12) {
        subType = mainType - 3 ;
        if( subType == 8 )
            ++subType;
    }
    

    This is in my mind way more legible and does exactly the same. Or I'm completely confused?

    If the code was about mapping arbitrary mainType values to subType values, then a lookup table would be better, by the way.

  • BBT (unregistered)

    "so he cobbled together an 'else if' in the most ridiculous way possible."

    That sounds like a challenge. I'm sure someone here could do more ridiculous!

  • Nicolai Jørgensen (unregistered)

    Actually, the professor is right about the NAND gates (and the poster should have paid more attention in class).

    It IS the most basic 2-input-gate to use and all other gates are designed from NAND-gates (using only 4 transistors) and then the redundant transistors are removed for optimization.

    Even an AND-gate is just a NAND-gate with and inverter (2 transistors) in any processor design you'll find.

    Check this wiki for further info: http://en.wikipedia.org/wiki/NAND_logic

  • Douglas (unregistered) in reply to frits

    No need for threads or alternative universes:

    #include <iostream>

    #define P(_X) std::cerr << _X << std::endl;

    class MainType { public: bool operator==(int) { return true; } };

    int main() { MainType mainType;

    if(mainType == 7)
        P(4);
    if(mainType == 9)
        P(6);
    if(mainType == 11)
        P(9);
    

    }

  • Jonesey (unregistered)

    break;

    /* Wow, that's a sexy piece of code! It looks so beautiful I want to slip something in it's drink and take it back to mine, where it'll wake up in the morning confused and with a sore rear! */

    continue;

  • Skilldrick` (unregistered)
    so he cobbled together an 'else if' in the most ridiculous way possible

    I think without the knowledge of else if, this isn't the most ridiculous way to cobble the semantics of an else if statement together. I think it's pretty damn elegant actually...

  • Carl (unregistered) in reply to apaq11
    apaq11:
    I vaguely recall exercises in converting AND/OR circuits into only NAND gates.
    And you've gone on to use that in your Information Systems career how, exactly? Because at my University they were obsessed with making us slog thru this crap while never saying a word about SQL Injection or Cross-Site Scripting.

    "A human will eventually look at it." riiight.

  • Anon (unregistered) in reply to Ed
    Ed:
    But what if mainType is 7, 9 AND 11? Then you're just screwed!

    People who think this is a comment on thread synchronization are TRWTF. Clearly we're talking about quantum computing here.

  • Anon (unregistered) in reply to Severity One
    Severity One:
    Maybe the developer didn't know about else-if, but apparently, Rob (the submitter) didn't know about the switch statement either.

    Yes, a switch makes more sense, but, given that the original programmer knows about "if" how is it conceivable that they don't know about "else" and "else if"? You could (almost) forgive that they just plain didn't know about switch, but to know "if" and not "else"/"else if" is just mind blowing.

  • Bub (unregistered)

    Haven't you ever seen .Net-optimized code before?

  • Manos (unregistered)

    It's the infinite loop that always breaks after the first execution. That should get patented immediately.

  • (cs) in reply to MainCoder
    MainCoder:
    I tend to avoid switch constructs myself, unless each case ends with a return statement (e.g. the switch is the main part of some conversion function). The reason being that I dislike having breaks in each case, as well as avoiding potential logic bugs that can arise due to forgetting one.

    As an ex-VB programmer, this is the one time I miss VB. Select Case ... Case ... End Select was a much better construct than switch.

    I'd like C# to include a switch statement where each case had to be followed by a block, so it would look like:

    select (mainType) { case (7) { subType = 4; } case (9) { subType = 6; } case (11) { subType = 9; } }

    I don't have a problem with fall-through switch, but can we have a non-fall-through version where you can't mess yourself up by forgetting a break?

    I've used the select keyword in my example because that's what VB uses - if C# can steal good ideas from non-Microsoft languages, why not one from their own VB?

  • (cs) in reply to Consultuning
    Consultuning:
    Erm... my optimizing mind only can say... why is that nobody is not pointing that branching is mostly unnecessary? All that discussion about switch is puzzling, because the whole thing can be reduced to
    subType = mainType - 3 ;
    if( subType == 8 )
        ++subType;
    
    There's a special place in Maintenance Hell for coders like you.
  • (cs)

    Everyone here who's talking about "not knowing about switch statements" is missings the bigger picture. Look at that code again. That's very close to a switch statement. Clearly he knows about switch statements -- I'm guessing from VB --- But doesn't realize that C/C++ has them also.

    I'll bet he's thinking that he's created a way to "do select/case in C++", and is feeling very proud of himself....

  • z (unregistered) in reply to FriedDan

    using else if gives you the benefit of not checking the conditionals after the first match has been found.

    if(mainType == 7) subType = 4; else if(mainType == 9) subType = 6; else if(mainType == 11) subType = 9;

    This should be a bit more efficient then -- disregarding the fact that switch would be even better.

  • Alan (unregistered)

    Pfft. You C guys and your silly switch statements. All we really need is a map!

    types = {
        7: 4,
        9: 6,
        11: 9,
    }
    
    if baseType not in types:
        return None
    
    return types[baseType]
    

    Python FTW!

    (of course it'd be better to avoid the magic numbers altogether, but we don't seem to have that luxury here)

  • Mikuso (unregistered) in reply to halcyon1234

    subType would be 4, just like in the original code.

  • PITA (unregistered) in reply to Ed
    Ed:
    FriedDan:
    As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

    It could be written:

    if(mainType == 7)
        subType = 4;
    if(mainType == 9)
        subType = 6;
    if(mainType == 11)
        subType = 9;
    

    There's no potential overlap so no NEED for an else (even if it might make it more readable).

    But what if mainType is 7, 9 AND 11? Then you're just screwed!

    CAPTCH: dignissim (missingID backwards, perhaps?)

    Where is Dr. Who when you need him?

  • (cs)

    This is a stupid block of code. They obviously should have used GOTOs.

  • Chuck (unregistered) in reply to ParkinT
    ParkinT:
    while(subcontractor==nitiwt)
    {
      if (Paula == "Brillant")
        return true;
      if (Table == "wooden")
        return true;
      if (XML == true)
        return true;
      if (Post == "FIRST")
        return null;
      if (Post == "FRIST")
        return subcontractor;
    }
    

    frawress

  • pete (unregistered)

    Other than the use a switch and you don't even need an else statement comments which are obviously true. I'm guessing that code has had some bad refactoring done to it. I'm guessing a giant method operating on a while loop for multiple records was used initially and broken into sub methods (badly).

    CAPTCHA: jumentum

  • Brian Manahan (unregistered)

    Nobody here seems to know about a map or an associative array.

  • lesle (unregistered) in reply to the beholder

    Who'd've thunk!

  • pete (unregistered) in reply to Zylon

    That's assuming a mainType always changes the subType. What if the mainType is not 7, 9 or 11.

  • pete (unregistered) in reply to Zylon
    Zylon:
    Consultuning:
    Erm... my optimizing mind only can say... why is that nobody is not pointing that branching is mostly unnecessary? All that discussion about switch is puzzling, because the whole thing can be reduced to
    subType = mainType - 3 ;
    if( subType == 8 )
        ++subType;
    
    There's a special place in Maintenance Hell for coders like you.

    That's assuming a mainType always changes the subType. What if the mainType is not 7, 9 or 11?

    First reply went wrong.

Leave a comment on “Else... where?”

Log In or post as a guest

Replying to comment #297564:

« Return to Article