• giammin (unregistered)

    if (comment == "Frist") { comment = "Frist"; }

  • (nodebb)

    Even in languages with truthiness, their check of if(value == true) may fail if value is something like number 1.

  • Tim R (unregistered)

    In yesterdays WTF where they were generating JavaScript code from server-side code, this function would be extra useful because it works in both C# and JavaScript (and probably PHP and java as well for all I know)

  • SG (unregistered)

    It makes me cringe when I see a boolean variable compared to a boolean literal.

  • TheCPUWizard (unregistered)

    NOPE:

    bool isValue
    {
       get { if (r_isValue) DoSomeThings(); return r_isValue; }
       set { r_iSvalue = value; DoMoreThings(); }
    }
    
  • (nodebb)

    That could have some moderately surprising results in truthy languages as something that was true (or at least truthy) would become untrue after that.

  • Jaloopa (unregistered)

    If isValid is a nullable boolean, this is a (rather unclear) way of setting it to false if null. I'd rather do isValid ??= false but there could be times in C# that the basic logic is required

  • Sauron (unregistered)

    They forgot to add this block between the if and the else :

    else if (isValid == FileNotFound) {
        isValid = FileNotFound;
    }
    
  • Anon (unregistered)

    TRWTF is this employer using Lines of Code as a productivity metric.

  • (nodebb)

    In truthy languages, this will clear out what non-boolean value was in isValid for the simple fact, isValid will be converted to boolean before being compared to true because it used == instead of ===.

    PHP has some oddness with string variables, in this case, IIRC. The string "0" is false! I think an array with a single 0 or other falsey value is false.

  • Tim R (unregistered) in reply to miquelfire

    Javascript has some interesting behaviour with "0" as well - whilst boolean("0") is false, "0" == false is true

  • Champs (unregistered)

    Greg’s coworker can’t handle truth. They’re pretty !!paranoidtoo.

  • MatthewSt (unregistered)

    Useful if using something like NCrunch (or equivalent code coverage tool) as you can then see which tests hit which lines

  • (nodebb)

    Even in a truthy language, the easiest way to ensure strict boolness would be something like

    value = !!value
    
  • (nodebb) in reply to Mr. TA

    In PHP, value == true is true when value is any truthy value. In Python and JavaScript, it succeeds when value is 1, but not other truthy values.

  • (nodebb)

    I wanted to store this solution for future reference, but I couldn't find the file.

    (also can't believe how long this meme/joke has been around.)

  • (nodebb)

    It's a solution to a race condition. Between the time the first line is evaluated and the if/else is started, some other process might have changed the value of 'value' . Now it gets fixed right properly

  • (nodebb)

    Typical case of "rewriting code would be refactoring and they don't pay me enough to do this because I never enter into the field because I liked it" syndrome :-)

  • (nodebb)

    This snippet is valid C and valid C++, and it might change the value of isValid if it is an integer. Depending on the compiler you might get a warning.

    R3D3

    value = !!value
    

    I've read about this on the internet, but have never seen in production. Do people really do this? I feel an explicit cast is usually clearer.

  • Duke of New York (unregistered)

    Just drop the else branch. Validating someone who already has been is polite, but invalidating someone who already has been is bullying. Positive feedback or none at all.

  • Duke of New York (unregistered) in reply to Gearhead

    Of course they do. It's valid syntax and plenty clear after you've seen it.

    OTOH casting functions in JavaScript break if you plausibly, but mistakenly call them as constructors: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean

  • Randal L. Schwartz (github)

    isValid = isValid ? true : false;

  • Ralf (unregistered) in reply to Randal L. Schwartz

    isValid = isValid == true

  • (nodebb)

    My bet is it's a debug breakpoint trap. They needed a NOP inside the braces to set their breakpoint on, and those two NOPs were the ones they came up with. Since it's an assignment, it ought not be optimized out. OTOH, compilers are getting smarter all the time about what they'll do to your code. Then again, typically one uses a debugger with optimizations turned off.

    IME it's truly amazing how many people don't understand conditional breakpoints.

  • George Rogers (unregistered)
    Comment held for moderation.
  • Hmm (unregistered)
    Comment held for moderation.
  • Craig (unregistered)

    The underlying storage of a .NET Boolean is an integer, so it's theoretically possible to get a value other than true or false in there, especially if it's coming from something less strict than C#.

  • William Markus (unregistered)
    Comment held for moderation.

Leave a comment on “Totally Valid”

Log In or post as a guest

Replying to comment #:

« Return to Article