• Rico Chet (unregistered)

    but you can debug the code better :D

  • Richard (unregistered)

    <FONT face=Georgia color=#ff0000>You gotta ask yourself though, which looks prettier? [;)]</FONT>

  • (cs)

    Obviously there are cases for code like this.

    Take VB, for example, which does not have short-circuit evaluations. This could lead to much more efficient code if the function in the if statement actually takes a certain amount of time.

    Perhaps the author if this nugget of clean, efficient code came from a Visual Basic background and understood this, and was in the habit of creating hand-tooled short-circuit evaluations such as the one you see above.

    Of course, any sufficiently trained monkey could look at the documentation for the language and see that it does support evaluations of this type.

    BTW, what language is this? It looks like psuedo-c...

  • A N Other (unregistered)

    It's an optimization. The second version doesn't need to execute all the tests when one of the earlier ones fails.

  • (cs) in reply to A N Other

    Anonymous:
    It's an optimization. The second version doesn't need to execute all the tests when one of the earlier ones fails.

    it looks like c# so this type of optimization is inherent in the language.

    but thanks for playing today's exciting game of "excuse the inexcusable."  we have a very lovely consolation prize for you...

  • (cs)

    Nested ifs, early return statement, magic strings, using a string when an int would work...  It's all right there for ya.  I give it a 4.5/5

  • Jan (unregistered) in reply to A N Other

    Anonymous:
    It's an optimization. The second version doesn't need to execute all the tests when one of the earlier ones fails.

    Dude, you are kidding, right! That code looks like C++, Java or C#. All three support short-circuiting.

  • (cs) in reply to Mike R

    It looks like C++ to me.

  • (cs) in reply to Beek

    WTF is wrong with an early return?

  • kalaef (unregistered) in reply to Mike R

    VB.NET now supports short-circuiting operations as well, so even that isn't an excuse.

    In VB.NET

    AndAlso is the short circuiting AND
    OrElse is the short circuiting OR

  • Mihai (unregistered) in reply to A N Other

    Boolean operators && and || stop executing as soon as the truth of the final expression is determined. This is the C standard (if the code fragment is C).

    The two forms are 100% equivalent.
    Might be style preference, or easyer debugging.
    But I do not think is a WTF.

  • (cs)

    The real shame here is that there's a MUCH more elegant way to do it.

    <FONT face="Courier New">switch(typec)
    {
      case 20:
        break;
      case 13:
        break;
      case 5:
        break;
      case 4:
        break;
      default:
        SelectType("ALLOC");
      break;
    }
    </FONT>

    <FONT face="Courier New">[:D]<FONT face=Arial size=1>(the above was a joke btw)</FONT>

    </FONT>
  • (cs) in reply to kalaef

    Heh I'd probably write it like this:

    if ((typec!="20") && (typec!="13") && (typec!="5") && (typec!="4"))
    if (
        typec == 20 || // 20 means...
        typec == 13 || // 13 means...
        typec == 5 ||  // 5 means...
        typec == 4     // 4 means...
    )
    {   // we don't need to SelectType for these
        // because...
    }
    else
    {   SelectType("ALLOC");
        return;
    }
    there's also a typo, Alex - you've got an extra ) after the 4

  • (cs) in reply to shane
    shane:

    but thanks for playing today's exciting game of "excuse the inexcusable."  



    I hereby propose that this website be renamed to the above  
  • (cs) in reply to kalaef

    Anonymous:
    VB.NET now supports short-circuiting operations as well, so even that isn't an excuse.

    In VB.NET

    AndAlso is the short circuiting AND
    OrElse is the short circuiting OR

    Hahaha! You can't be serious... can you?

  • (cs) in reply to Otac0n
    Otac0n:
    WTF is wrong with an early return?

    Some people believe functions should have one and only one way into and out of them.  That way if you have foolishly written a monsterously long function, you won't have to read the whole thing to see if you have missed a return statement that prevents your changes from getting executed.

    Forcing functions to be short and do only one thing is too much of a burden.  Best to just outlaw all types of branching, including if-then, loops, and switch-case statements with more than one possible case.

  • (cs) in reply to Otac0n

    Otac0n:
    WTF is wrong with an early return?

    Because it's not 'structured programming' and structured programming is a religion of which there is no part that can be questioned without the believers covering their ears and yelling "nanananana!"

    Therefore, we must accept the horrible obfuscations that are often required by having a single return statement as being superior to the easily readable early return versions.

  • (cs) in reply to A N Other
    Anonymous:
    It's an optimization. The second version doesn't need to execute all the tests when one of the earlier ones fails.


    No good language implementation will in the first one either.

    Actually, in any half-decent implementation of just about any language, the second one is quite a few more op codes.

    This is why you can use && and || for ternary-esque operations (although, not always returning a value, depending again on language and implemenation), by doing if("stupid") && then("kill them") || exit()

    exit() is only called if("stupid") returns a false value, and then("kill them") is only called when if("stupid") returns a true value.

    Any fucking programming 101 class or operator reference or a bazillion other things should have told you this, what the fuck, guy.
  • (cs) in reply to Maurits

    Maurits:

    there's also a typo, Alex - you've got an extra ) after the 4

    Now that's efficiency [:D]

    My comments on the WTF:
    It makes me wonder why none of today's languages have a construct like SQL has, that will let you do things like SELECT (...) WHERE @whatever NOT IN (1, 3, 5, 7, 9). Of course you can use an array or whatever else, but a language construct would be pretty cool. Maybe like:

    <FONT face="Courier New" color=#000000>if (typec !in ("20", "13", "5", "4")) {
       DoStuff();
    }</FONT>

    or something.

  • (cs)

    Okay this just makes me giggle like a schoolgirl. 

  • (cs)
    <FONT face="Courier New" size=2>

    There are 4 possibilities here:

    1. There was a requirement to enter comments so what better comment than code to explain what the code should do.

    2. Was getting paid by the line and needed more beer and pizza than the 1 line paid so went with the 6 lines.

    3. Was getting paid by the line and it required comments.

    4. WTF

     

    [8-|]

    </FONT>
  • (cs) in reply to Jan

    Looks like any one of:
    C++
    C#
    Java
    Pike
    D
    ECMAScript

    But is definitely not C

    Additionally, in any of these languages the expression could have been wonderfully optimized by performing a type conversion and making an integral comparison instead of a string comparison (which requires far more operations).  Wow, the WTF-posters are almost worse than the WTFs.

  • (cs) in reply to lucio
    lucio:

    Maurits:

    there's also a typo, Alex - you've got an extra ) after the 4

    Now that's efficiency [:D]

    My comments on the WTF:
    It makes me wonder why none of today's languages have a construct like SQL has, that will let you do things like SELECT (...) WHERE @whatever NOT IN (1, 3, 5, 7, 9). Of course you can use an array or whatever else, but a language construct would be pretty cool. Maybe like:

    <FONT face="Courier New" color=#000000>if (typec !in ("20", "13", "5", "4")) {
       DoStuff();
    }</FONT>

    or something.

    PHP has that and it's pretty handy for data validation and whatnot.

    <FONT face="Courier New">if (!in_array($typec, array(4, 5, 13, 20)))</FONT>

    <FONT face="Courier New">  complain();</FONT>

  • (cs) in reply to rogthefrog

    I meant "something like that using arrays", not having read your post too carefully :)

  • (cs) in reply to loneprogrammer

    loneprogrammer:
    Otac0n:
    WTF is wrong with an early return?

    Some people believe functions should have one and only one way into and out of them.  That way if you have foolishly written a monsterously long function, you won't have to read the whole thing to see if you have missed a return statement that prevents your changes from getting executed.

    Yeah, someone should invent some sort of "search" tool for source files or even make some sort of editor that makes control statements such as return stand-out somehow.  Like, maybe they could be a different color or something.

    The flip side to that argument is when you ae modifying some stupidly long method and you want to change what it returns under certain conditions.  Since the developer has some dumbass return variable, you can't be sure you've done it without walking though the entire method to verfiy that nothing else modifies that return variable.

    loneprogrammer:

    Forcing functions to be short and do only one thing is too much of a burden.  Best to just outlaw all types of branching, including if-then, loops, and switch-case statements with more than one possible case.

    Yeah, these kinds of rules are pretty worthless.  People should just keep things short and code it the most natural and understandable way.

  • Evan M. (unregistered)

    I see this, and while I accept it's not the best hting to do, I also don't see it as the worst thing. What's worse would be someone subscribing to the "Only 1 line perstatement" philosophy, and then doing something like that. I've had to fix up people code where they have if statements go on 3 screens wide (and I run my screen at 1600 x 1200 rez), and there I'd just wish that they had used separate if clauses if they aren't going to break the statement up.

  • (cs) in reply to tag
    tag:
    Looks like any one of:
    C++
    C#
    Java
    Pike
    D
    ECMAScript

    But is definitely not C

    Additionally, in any of these languages the expression could have been wonderfully optimized by performing a type conversion and making an integral comparison instead of a string comparison (which requires far more operations).  Wow, the WTF-posters are almost worse than the WTFs.


    How could you leave out JavaScript?
    Also if it is Java, there is another WTF.
  • kalaef (unregistered) in reply to Miszou
    Miszou:

    Anonymous:
    VB.NET now supports short-circuiting operations as well, so even that isn't an excuse.

    In VB.NET

    AndAlso is the short circuiting AND
    OrElse is the short circuiting OR

    Hahaha! You can't be serious... can you?



    Absolutely serious

    MSDN article describing this
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconLogicalOperators.asp
  • (cs)

    This is David Koontz the original poster of the code.  I agree that because of the lack of short circuting (which I was not aware of in VB) that the expanded block could possibly be faster, but all that extra code to save just a few small equality checks?  Anyway, I don't know what happened when the example got "anonymized" but originally all those AND's in the commented out line were OR's which somewhat changes the WTF.  When I found it I immediatly saw that whoever was coding the thing didn't understand the difference between OR and AND and therefore did it the second way to acheive the result they wanted instead of just changing a few keywords.

  • Roy (unregistered) in reply to tag

    It's not Java.  In Java, we would generally use !(string1.equals(string2)) to compare strings, rather than string1 != string2.

  • (cs) in reply to Rick
    Rick:

    How could you leave out JavaScript?
    Also if it is Java, there is another WTF.


    I believe ECMAScript was meant to stand in for JavaScript since it's sort of the "official" version now.  I do find it rather annoying though to have two names for basically the same language.
  • (cs) in reply to EsotericMoniker

    I work with David, and I can assure you that the programmer who wrote that code didn't take into account short circuit evaluation, or logic for that matter.

    I think the greater WTF is that while David cleans this stuff up, the other "programmers" are still there leaving more of this code behind!

    From one code janitor to another, I feel for you David. At least your job isn't going anywhere! d:

  • Justin Sippel (unregistered) in reply to Rick

    He mentioned ECMAScript; JavaScript would be pretty much implied with that. Not to mention it's an incomplete list of possible languages this could could be written in. It doesn't even matter what language it's in, its stupidity transcends language.

  • Effendi (unregistered) in reply to Roy

    It looks to me like the coder had it coded the right way, but during development the check was always failing so they expanded it to debug it and forgot to put it back.

  • (cs) in reply to kalaef
    Anonymous:
    VB.NET now supports short-circuiting operations as well, so even that isn't an excuse.

    In VB.NET

    AndAlso is the short circuiting AND
    OrElse is the short circuiting OR


    OrElse is offically my new favorite operator.
  • Maik (unregistered) in reply to Roy
    Anonymous:
    It's not Java.  In Java, we would generally use !(string1.equals(string2)) to compare strings, rather than string1 != string2.


    Two parentheses too many.
  • Anonymous (unregistered) in reply to lucio

    lucio:

    It makes me wonder why none of today's languages have a construct like SQL has, that will let you do things like ...

    What do you mean none of today's languages? [:P]

    unless [ "20", "13", "5", "4" ].include? typec
       do_stuff
    end
  • brian (unregistered) in reply to SurfaceTension

    Surely you meant: "your job isn't going away..."

  • (cs) in reply to brian
    Anonymous:
    Surely you meant: "your job isn't going away..."


    No, working on this code my job surely isn't going anywhere.  Career either for that matter.  :'(
  • (cs) in reply to EsotericMoniker

    EsotericMoniker:
    I immediatly saw that whoever was coding the thing didn't understand the difference between OR and AND

    How is that even possible? [:(]

  • (cs) in reply to lucio
    lucio:

    It makes me wonder why none of today's languages have a construct like SQL has, that will let you do things like SELECT (...) WHERE @whatever NOT IN (1, 3, 5, 7, 9).


    Don't forget python!

    if(x not in (1,2,3,4,5)):
      doSomething()
    else:
      doSomethignElse()

  • (cs) in reply to kalaef
    Anonymous:

    AndAlso is the short circuiting AND
    OrElse is the short circuiting OR


    A WTF in AndAlso of itself OrElse its an elegant solution to the sins of VB past.
  • (cs) in reply to rogthefrog
    rogthefrog:

    EsotericMoniker:
    I immediatly saw that whoever was coding the thing didn't understand the difference between OR and AND

    How is that even possible? [:(]



    People get "promoted" out of customer service positions because "they know Excel macros".  Now they have an initiative here to move the tech support people into programmer positions which I suppose is a better plan, but they're having the current programmers teach these up and coming ones.  "The blind leading the blind" springs to mind.
  • (cs) in reply to Rico Chet

    I love it even more for the magic numbers.   

    There aren't any comments explaing WHY, oh WHY
    typec can't be 4, 8, 18 whatever...

  • (cs) in reply to Maurits

    My version (just comment the f*** line for the magic numbers or at least
    delcare constants)

    // We need to call out to SelectType() if typec is not:
    // 20 - blah
    // 13 - blah blah
    // 5 - blah blah blah
    // 4 - blah blah blah blah
    if ((typec!="20") && (typec!="13") && (typec!="5") && (typec!="4"))

  • (cs) in reply to EsotericMoniker
    EsotericMoniker:
    Rick:

    How could you leave out JavaScript?
    Also if it is Java, there is another WTF.


    I believe ECMAScript was meant to stand in for JavaScript since it's sort of the "official" version now.  I do find it rather annoying though to have two names for basically the same language.


    "JavaScript" is a proprietary and trademarked language belonging to Netscape Communications Corporation, and includes not only the core language but the browser object bindings for the Navigator browser (and the Netscape Server, when server-side JavaScript was allowed). "JScript" is a syntactically similar language implemented by Microsoft, but with a legally distinct name for distinctly legal reasons. ECMAScript is an international language standard based on  the core JavaScript  language; it includes no object bindings other than the core language objects. (The W3C defines the ECMAScript object bindings for HTML 4.x/XHTML and XML in general  in their Recomendations.)
  • (cs)

    There is a much better implementation:

    if (typec=="20") {

    }
    else {
          if (typec=="13") {

          }
          else {
               if (typec=="5") {

               }
               else {
                     if (typec=="4") {

                     }
                     else {
                          SelectType("ALLOC");
                          return;
                     }
               }
          }
    }

    It is much cleaner and easier to read.  Plus, you can much more easily add code to do something if typec is 20, 13, 5, or 4.
  • (cs) in reply to rogthefrog
    lucio:

    My comments on the WTF:
    It makes me wonder why none of today's languages have a construct like SQL has, that will let you do things like SELECT (...) WHERE @whatever NOT IN (1, 3, 5, 7, 9). Of course you can use an array or whatever else, but a language construct would be pretty cool. Maybe like:

    <font color="#000000" face="Courier New">if (typec !in ("20", "13", "5", "4")) {
       DoStuff();
    }</font>

    or something.

    Perl and Lisp thrive on constructs like this, and I believe Python includes them, along with a number of less-useful languages. My memory of VB is fading, but it might have a set construct. Oddly, foreach is a lot more common than this more basic operation, though most languages make up for it with a function.

    Miszou:

    Anonymous:
    VB.NET now supports short-circuiting operations as well, so even that isn't an excuse.

    In VB.NET

    AndAlso is the short circuiting AND
    OrElse is the short circuiting OR

    Hahaha! You can't be serious... can you?



    You just made my day. =D Google before post plz.

    Rick:
    How could you leave out JavaScript?

    Also if it is Java, there is another WTF.



    Thank Sun for that, they trademarked JavaScript(tm), and apparently no one could come up with a better alternative than the ECMA's name.

    Roy:
    It's not Java.  In Java, we would generally use !(string1.equals(string2)) to compare strings, rather than string1 != string2.


    Some nights I lie awake and wonder what Sun's engineers and designers were smoking when they thought that up. Does object-oriented paint thinner exist?
  • (cs) in reply to Stan Rogers
    Stan Rogers:
    "JavaScript" is a proprietary and trademarked language belonging to Netscape Communications Corporation


    My bad, I thought it was Sun.

    I wonder how JScript, a mediocre name at best, has died out while ECMAScript, the ugliest language name that doesn't start with X I know of, has become so popular. There must be a lot of standard-whores. The rest of the world just sticks to javascript or javascript1.2. =p
  • (cs) in reply to foxyshadis

    It was Sun's originally (Oak/Live Oak renames), but was sold (probably for an obscene amount of money). I mean, the code is just "out there", no proprietary runtime required or nuthin'. How else are you supposed to make money off of something like that?

Leave a comment on “Right In Front of You”

Log In or post as a guest

Replying to comment #:

« Return to Article