• Paul Neumann (unregistered) in reply to Medinoc
    Medinoc:
    Visual Basic .Net has it implemented as a function. A non-generic function, that takes one Boolean and two Object as arguments and returns Object (plus, the fact that both sides are evaluated in both cases, but that one is inevitable when using functions).
    Fun with VB.NET's if operator and functions:
    Dim myVar = IIf(Test As Boolean, IfTrue As Object, IfFalse As Object)
    Is a function which will evaluate both the truthy and non-truthy argumentsdespite the value of Test.
    Dim myVar = If(Test As Boolean, IfTrue As T, IfFalse As T)
    Is an operator which will NOT evaluate both IfTrue and IfFalse. The only difference is the extra I in the function name.

    For bonus points:

    Dim myVar = If(Test As Nullable(Of T), IfNull As T)
    Will return the value of Test if it is not null, otherwise it will return the value of IfNull.

    If the If is not the part of an evaluation or assignment the code is invalid and will not compile. IIf does not need to be a part of an evaluation or assignment. See also: http://ideone.com/e81FvE

  • g (unregistered) in reply to Kata
    Kata:
    =! != != !

    I would give you "like", but there is not this kind of button :-).

  • Oh, anonymous (unregistered)

    mentor == false

    dementor == true

  • hank (unregistered) in reply to LimEJET
    LimEJET:
    PHP springs to mind, with this nugget:
    While true, how exactly that behavior presents a problem given your basic "value ? false : true" condition like the one we're dealing with here remains a mystery to me.

    Then again, the original commenter thinks everyone except morons should be manipulating boolean values with bitwise operators, so that kinda calls into question the frame of reference we're dealing with when confronted with words like "moron" and "messed up".

  • (cs)

    Flashback...

    Some non software guy (probably management): "See, its easy, in this text editor you can change all the A's to B's". He says after saving the file, overwriting the only copy.

    Question from a real software guy: "How do I change it back to the way it used to be, as it was my only copy?"

    Non software guy: "Oh, that's easy, just change all the B's to A's. Let me show you!"

    Real software guy: "Arrrg" as he does a faceplant.

  • Mr A (unregistered) in reply to Kev

    Actually, my current post is almost exclusively filled with such 'seniors' - people with 10, 15 and even 20 years of service with the same company.

    Not actually very good when they started. Not learned much over the years. Never challenged by equally inept management to improve and never seen the need to improve.

    Plus, they're often antagonistic to any attempt to improve.

    The sad thing is if the company hits a problem and they are made redundant, they are utterly unemployable by anyone not on medication.

    Look up the articles on the Deadsea Effect and you'll see it's not uncommon to see this.

  • Someone (unregistered) in reply to Shinhan7
    Shinhan7:
    "Some people have 10 years of experience. Others have 1 year of experience 10 times."

    (source unknown)

    Still others apparently have one week of experience 500 times.

  • (cs) in reply to Someone
    Someone:
    Shinhan7:
    "Some people have 10 years of experience. Others have 1 year of experience 10 times."

    (source unknown)

    Still others apparently have one week of experience 500 times.
    That's not fair. I think most programmers with an entire week of experience would be able to see the WTF in this one. Seriously, it's one of the worst I've seen in a LONG time, struggling with the most basic program flow.

  • jay (unregistered) in reply to G
    G:
    qbolec:
    As he is refered to as "senior", perhaps, he has just forgotten.
    Hehe. More like a senile developer.

    I think I'll use that as my job description.

  • (cs) in reply to Kev
    Kev:
    I've seen some pretty poor new developers, but I can't believe anyone could stay working for a company and not understand how an If statement works, let alone get to be a senior developer. Surely this can't be genuine
    You ain't seen nothing yet, then :)
  • (cs) in reply to Jeff Dege
    Jeff Dege:
    [...]I was reading through the code, and went to the lead, who was supposed to be breaking me in, with a question.

    Q: Why are we always searching through the database records sequentially? Surely the database library provides some sort of mechanism for searching for a record using an index.

    A: I don't think it does.

    And that's where a bit less of thinking and a bit more of reading the fine manual would do just fine, thank you.

  • AMR (unregistered)

    Maybe the senior developer is a VHDL or Verilog developer at heart?

    In both languages, the code would have behaved as intended, provided the non-blocking assignment operator (<=) was used!

  • T (unregistered)

    Once I told my non-programmer friend that "forum password != FTP password". He spent the next 20 minutes trying to use the FTP with the forum password.

  • jay (unregistered)

    Even if he'd included the ELSE clause so the function gave correct results, it's just dumb.

    I know there's lots of bad code out there, but for some reason one thing that annoys me every time is:

    if (flag==true)
      flag=false;
    

    What is the purpose of the test? So if flag is already false, we don't unnecessarily set it to false?

    A very similar one I see all the time is:

    if (amount!=0)
      total=total+amount;
    

    Because if we omitted the test and added zero to the total, that would totally screw up our results.

    My only guess is that programmers who write this think they are making the program more efficient. If the amount is zero, we can save the effort of performing an addition that won't change the total.

    Except ... umm ... Think about this logically for a moment. If the amount is not zero, then you're stilling doing the addition, plus you're doing the test. So clearly it must take longer in the case where the amount is not zero. If the amount is zero, then on any computer where I've ever actually checked into it, the test takes longer than an addition. So you're "saving" the time to do a faster function by instead doing a slower function. So in both cases, you lose.

  • ThatSlyQuarian (unregistered) in reply to jay

    The point of doing if(flag==true)flag=false; Is to toggle something from on to off. It increases readability of the code. The test for true is superfluous though.

  • C-Derb (unregistered) in reply to ThatSlyQuarian
    ThatSlyQuarian:
    The point of doing if(flag==true)flag=false; Is to toggle something from on to off. It increases readability of the code. The test for true is superfluous though.
    As pointed out by others, you can toggle just by using
    flag=!flag
  • (cs)
    if(can)
       do();
    else
       mentor();
    
  • I fell for it. (unregistered) in reply to taytay

    Wha..?

    You can see that's not the intent of the code though right? That's what it does but it's clear to see that the intended purpose was to flip the boolean.

  • meh (unregistered) in reply to Tim
    Tim:
    if (!!a != !!b) { ReturnError("you must either specify both a and b or omit them both); }

    While C tricks are fun, wouldn't this be easier to read: (a==NULL) != (b==NULL)

  • n_slash_a (unregistered) in reply to SeySayux
    SeySayux:
    value ^= 1;
    

    Or, if your language does not convert non-zero integers to true:

    value ^= true;
    

    Or, if the other people on your team are morons:

    value = !value;
    

    If you want to be explicit:

    value = value ? false : true;
    

    If you want to be explicit and others are not smart enough to understand the ternary conditional operator (or your language does not have/messed up the TCO):

    if(value)
        value = false;
    else
        value = true;
    

    If you are paid by the character/SLOC:

    if ( value == true )
    {
        Logger.getLogger(this.getClass().getCanonicalName()).debug("Before: value == "+value);
        value = false;
        Logger.getLogger(this.getClass().getCanonicalName()).debug("After: value == "+value);
    }
    else if ( value == false )
    {
        Logger.getLogger(this.getClass().getCanonicalName()).debug("Before: value == "+value);
        value = true;
        Logger.getLogger(this.getClass().getCanonicalName()).debug("After: value == "+value);
    }
    else
    {
        Logger.getLogger(this.getClass().getCanonicalName()).debug("Value changed? Possible synchronization error!");
        throw new IllegalStateException("Value changed while comparing!");
    }
    

    Don't forget documentation and unit tests, though.

    Feature comment!

  • (cs)

    100% valid:

    return !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!false;
    
  • _darkstar_ (unregistered)

    Dmitry Karasik has an interesting list of esoteric Perl operators:

    http://www.nntp.perl.org/group/perl.fwp/2007/11/msg4028.html

    Yes - a list of operators that Perl developers consider esoteric.

  • C-Derb (unregistered)
    Remy:
    For those that cheat, this is your target: http://imgur.com/8oQwL28.jpg
    Wow. Some things cannot be unseen.
  • naka (unregistered) in reply to xorsyst
    xorsyst:
    Kasper:
    SeySayux:
    messed up the TCO
    Any example of a language, which did mess it up?

    In Python, it's a little messed up:

    value = False if value else True

    Uh, in python you can also use:

    value = not value
  • Thy Master (unregistered) in reply to Kata

    Your comment is magnificent!

    Even the CAPTCHA agrees: ingenium.

  • Paul Neumann (unregistered) in reply to AMR
    AMR:
    Maybe the senior developer is a VHDL or Verilog developer at heart?

    In both languages, the code would have behaved as intended, provided the non-blocking assignment operator (<=) was used!

    VHDL would cry about two wires being connected without a comparator
  • naka (unregistered) in reply to naka

    Oh, didn't realise we were talking ternary operators. I am an idiot. Please continue.

  • Dave-Sir (unregistered) in reply to Tim
    Tim:
    when doing parameter validation in C I often needed to check whether one or the other or both string parameters was not null. this led to lots of expressions like

    if (!!a != !!b) { ReturnError("you must either specify both a and b or omit them both); }

    A little excessive. The conditional can be reduced to (!a != !b) or even (!a ^ !b). Though a good compiler should generate nearly identical code for all three...
  • Dave-Sir (unregistered) in reply to xorsyst
    xorsyst:
    Kasper:
    SeySayux:
    messed up the TCO
    Any example of a language, which did mess it up?

    In Python, it's a little messed up:

    value = False if value else True
    IMHO, Python's been getting steadily more messed up with the addition of each new "feature," starting with list comprehensions. At least all attempts to add block delimiters and implicit "self" have failed, so it's still one of the better options out there.

    If you want to see what happens when a Python programmer gets Perl envy, take a look at Ruby. The worst of both worlds...

  • gnasher729 (unregistered) in reply to tobor the mediocre
    tobor the mediocre:
    Jim must be one of those "1 year of experience 12 times" type senior developers.
    More like 5 minutes of experience about a million times.
  • Captain Oblivious (unregistered) in reply to eViLegion
    eViLegion:
    You don't need to explain anything.

    Just each write out truth tables, then compare them. If Jim is still unable to get it, write out step-by-step truth tables.

    If he's still unable to get it, either resign, or get Jim fired.

    The problem isn't the truth table. It's the fact that he thinks he's pattern matching. That is, he thinks that there is an implicit case around the two if-blocks, and that only one of the if-blocks will get run.

    The guy even said as much.

  • I fell for it. (unregistered) in reply to gnasher729

    That's less.

  • Jason (unregistered)

    No way can a senior developer write that code. OK, maybe he can write that code, if he was having a horrible day, but no way would he not spot the problem once it was pointed out to him. No matter how incompetent.

    I can't believe this one.

  • Welch (unregistered) in reply to jay
    jay:
    Even if he'd included the ELSE clause so the function gave correct results, it's just dumb.

    I know there's lots of bad code out there, but for some reason one thing that annoys me every time is:

    if (flag==true)
      flag=false;
    

    What is the purpose of the test? So if flag is already false, we don't unnecessarily set it to false?

    A very similar one I see all the time is:

    if (amount!=0)
      total=total+amount;
    

    Because if we omitted the test and added zero to the total, that would totally screw up our results.

    My only guess is that programmers who write this think they are making the program more efficient. If the amount is zero, we can save the effort of performing an addition that won't change the total.

    Except ... umm ... Think about this logically for a moment. If the amount is not zero, then you're stilling doing the addition, plus you're doing the test. So clearly it must take longer in the case where the amount is not zero. If the amount is zero, then on any computer where I've ever actually checked into it, the test takes longer than an addition. So you're "saving" the time to do a faster function by instead doing a slower function. So in both cases, you lose.

    That's why the first rule of optimization is, "Don't do it."

  • (cs)

    And another rule is, "don't try to be smarter than the compiler."

  • (cs) in reply to Bill
    That that that that boy wrote is not that that that that boy wrote.

    Because... That that is is that that is not is not

  • Sean (unregistered) in reply to taytay
    taytay:
    real wtf is people not noticing it is simply
    showOptionsButton = true;
    From the article: “Except,” Ben said, “this will just always set the value to true.”
  • (cs)

    "You could just do, showOptionsButton = (showOptionsButton) ? false : true."

    "Ewwwww!" Jim said. "Question marks belong in grammar books, not code."

  • SG_01 (unregistered)

    Some libraries I work with seem to prefer the following syntax:

    showOptionsButton ^= true;

    Not sure if that works in C#, or Java, or whatever language they're trying to use, but it works fine in C++ :)

  • JH00ker (unregistered) in reply to Kata

    My coworker and I cannot figure out what this comment means. Is it a sarcastic joke or an actual better way? Can you expound on the example so we can be brought into the loop on this one?

    Is it like this?

    value =!!=!=!value;

  • (cs) in reply to TRick
    TRick:
    This is one of those times where drawing a flowchart really helps.
    Nothing will help Jim. Watching him try to teach Ben his theory of booleans...well, never did the truism, "Those who cannot do, teach," ring so loudly.
  • Ken (unregistered) in reply to _darkstar_
    _darkstar_:
    Dmitry Karasik has an interesting list of esoteric Perl operators:

    http://www.nntp.perl.org/group/perl.fwp/2007/11/msg4028.html

    Yes - a list of operators that Perl developers consider esoteric.

    The ! and !! are (somewhat?) well-known C idioms too; want to count how many zeros there are in an array? Just do a
    cnt += !item;
    in the body of a loop, instead of the more verbose
    if (item!=0) cnt++
    . Also tends to avoid generating conditional branches with compilers that aren't as smart.

  • Rob (unregistered)

    TRWTF is Ben's inability to explain why this doesn't work. As a previous commenter already pointed out: a simple flowchart would suffice.

  • João Galamas (unregistered) in reply to Kata

    True....

  • João Galamas (unregistered) in reply to Kata

    True....

  • João Galamas (unregistered) in reply to Kata

    True....

  • João Galamas (unregistered) in reply to Kata

    True....

  • MacFrog (unregistered) in reply to chubertdev
    chubertdev:
    100% valid:
    return !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!false;
    100% valid, but still false...
  • RandomGuy (unregistered) in reply to JH00ker
    JH00ker:
    My coworker and I cannot figure out what this comment means. Is it a sarcastic joke or an actual better way? Can you expound on the example so we can be brought into the loop on this one?

    I'm hoping not to fall for some double irony here: It says that the =! operator(s) are not the same (therefore the middle !=) as the != operator and this is furthermore emphasized using the ! at the end.

    Btw: Did you know of the "converges to" operator in C/C++:

    int x=10;
    while(x --> 0) {
    printf("%d\n",x);
    }

  • (cs) in reply to hank
    hank:
    LimEJET:
    PHP springs to mind, with this nugget:
    While (true){ how exactly that behavior presents a problem given your basic "value ? false : true" condition like the one we're dealing with here remains a mystery to me. } // unreachable text below this line

    Then again, the original commenter thinks everyone except morons should be manipulating boolean values with bitwise operators, so that kinda calls into question the frame of reference we're dealing with when confronted with words like "moron" and "messed up".

    I just can't read any further, pls. someone interrupt me

Leave a comment on “The Truth of the Matter”

Log In or post as a guest

Replying to comment #:

« Return to Article