• TheIrritainer (unregistered)

    $isFrist = 0; $isFrist = (is_null($isFrist)) ? 0 : $isFrist;

  • eVil (unregistered)

    No, it can't be sung to Rule Britannia... it fits neither the verse or chorus structure. You can try to jam loads of syllables into a single note, but it doesn't scan very successfully.

    Heaven's command, by the way... heaven's command, matchless beauty, envy of them all... and don't any of you forget that.

  • (cs) in reply to eVil

    Depends on how you choose to pronounce the symbols, or if you choose to. It also can fit to "Happy Birthday" and "Les Petits Reins" if you're creative.

  • Crash (unregistered) in reply to Mark Bowytz

    I almost got it to work with "row, row, row your boat".

  • eVil (unregistered) in reply to Mark Bowytz
    Mark Bowytz:
    Depends on how you choose to pronounce the symbols, or if you choose to. It also can fit to "Happy Birthday" and "Les Petits Reins" if you're creative.

    Well, you can fit the word "Blah" to The Modern Major General Song if you really want to, but you can't reasonably say that "it fits". Its just words badly fit to some random tune.

    Just singing a melody with X notes, and sentence with Y syllables (Y!=X), in any old haphazard fashion, doesn't mean it scans.

  • GD (unregistered)

    initialTab = 0; if (isNaN(initialTab)) { initialTab = 0; } This is the Butterfly Effect pattern. Something might maybe have the possibility of having the probability to cause initialTab to change.

  • Mike (unregistered)

    initialTab = 0; if (isNaN(initialTab)) { initialTab = 0; }

    Probably best to check that initialTab is a number after that conditional assignment. After all, if the first assignment could fail...

  • Heisenberg (unregistered) in reply to GD
    GD:
    initialTab = 0; if (isNaN(initialTab)) { initialTab = 0; } This is the Butterfly Effect pattern. Something might maybe have the possibility of having the probability to cause initialTab to change.

    I really like this quantum-indetermination safe code. The variable won't have its value determined until the moment when its value is actually observed.

  • (cs)

    I think me and Jeremy have worked at the same place. The guru there would always quote "belt, braces and superglue" when I asked him about his massive WTFs.

  • Ramchandra Apte (unregistered)

    easter egg: click anywhere in "shell-shocked", and surprise!

  • Warren (unregistered)

    I was slightly surprised when reading the one about Jeremy's ex-coworker.

    Bear in mind that in "British English" (i.e. English not American) suspenders are called braces.

    Suspenders are lingerie and imply a belt already!

    Of course, this conjures up a totally different image depending on whether you've assumed the coworker is male or female.

  • ZoomST (unregistered) in reply to Warren
    Warren:
    I was slightly surprised when reading the one about Jeremy's ex-coworker.

    Bear in mind that in "British English" (i.e. English not American) suspenders are called braces.

    Suspenders are lingerie and imply a belt already!

    Of course, this conjures up a totally different image depending on whether you've assumed the coworker is male or female.

    If it is a male coworker, you can try to sing the code as any of "The Rocky Horror Show" songs. Seems legit to me.

  • A (unregistered) in reply to GD

    Yes, they really should have been more careful and gone for something like:

    initialTab = 0; while (isNaN(initialTab)) { initialTab = 0; }

  • (cs)

    Nested if statements: Easy to read, easy to follow the logic, easy to modify the logic if needed. Nested Ternary operations: Pain in the brain (way above neck) to read, easy to screw up the logic entirely by misplacing a bracket. Reminds me why I don't write code in LISP anymore.

  • Jim (unregistered)

    How terribly unsafe. Better to do:

    initialTab = 0;
    while(isNaN(initialTab)){
        initialTab = 0;
    } 
    
  • (cs)
    initialTab = 0;
    if (isNaN(initialTab)) { initialTab = 0; }

    Is this what you have to do if you declare a floating-point variable to be volatile? Does it really need to be declared volatile? Let the optimizer do its job.

  • Krunt (unregistered) in reply to Mark Bowytz
    Mark Bowytz:
    Depends on how you choose to pronounce the symbols, or if you choose to. It also can fit to "Happy Birthday" and "Les Petits Reins" if you're creative.

    Or literally any Ludacris "song".

  • ¯\(°_o)/¯ I DUNNO LOL (unregistered)

    When your only tool is a shell_exec, every problem looks like a shell command.

    And when your only tool is PHP, every problem looks like an opportunity to shoot yourself in the foot.

  • gnasher729 (unregistered) in reply to Nutster
    Nutster:
    Nested if statements: Easy to read, easy to follow the logic, easy to modify the logic if needed. Nested Ternary operations: Pain in the brain (way above neck) to read, easy to screw up the logic entirely by misplacing a bracket. Reminds me why I don't write code in LISP anymore.

    No difference really to ?: ?: can be harder to read per line, but you need fewer lines. Of course in this example, there was no need at all for nesting, if the "not editable" case had been handled first. And putting the source code into some structured form helps with readability.

  • Chaarmann (unregistered) in reply to Nutster

    initialTab = 0; if (isNaN(initialTab)) { initialTab = 0; }

    It makes sense if

    • there are concurrent threads accessing and changing the variable and the variable is global (and volatile)
    • the first assignment value is not the character zero, but a capital letter "oh". And O for example then is the abbreviated name for an instance of class "Object".
  • (cs) in reply to Nutster
    Nutster:
    initialTab = 0;
    if (isNaN(initialTab)) { initialTab = 0; }
    Is this what you have to do if you declare a floating-point variable to be volatile? Does it really need to be declared volatile? Let the optimizer do its job.
    Why have I got this horrible premonition that initialTab is an integer?
  • Anonymous Paranoiac (unregistered) in reply to Chaarmann
    Chaarmann:
    initialTab = 0; if (isNaN(initialTab)) { initialTab = 0; }

    It makes sense if

    • there are concurrent threads accessing and changing the variable and the variable is global (and volatile)

    Unless, of course, the code in question was written in JavaScript, in which case, it's just stupid.

  • golddog (unregistered) in reply to gnasher729
    gnasher729:
    Nutster:
    Nested if statements: Easy to read, easy to follow the logic, easy to modify the logic if needed. Nested Ternary operations: Pain in the brain (way above neck) to read, easy to screw up the logic entirely by misplacing a bracket. Reminds me why I don't write code in LISP anymore.

    No difference really to ?: ?: can be harder to read per line, but you need fewer lines. Of course in this example, there was no need at all for nesting, if the "not editable" case had been handled first. And putting the source code into some structured form helps with readability.

    Fewer lines not a goal. Maintainability is.

    That said, it's a case-by-case basis. I've used nested ternary when it's a simple case and was still easy to read.

    Just try to review your code from the perspective of an outsider: is it something the functionality of which can be gleaned relatively quickly and properly? If not, go to the more verbose if-then-else construct.

  • meh (unregistered) in reply to Chaarmann
    Chaarmann:
    initialTab = 0; if (isNaN(initialTab)) { initialTab = 0; }

    It makes sense if

    • there are concurrent threads accessing and changing the variable and the variable is global (and volatile)

    No, it doesn't. volatile is not a substitute for synchronization.

  • (cs) in reply to eVil
    eVil:
    Well, you can fit the word "Blah" to The Modern Major General Song if you really want to, but you can't reasonably say that "it fits". Its just words badly fit to some random tune.

    I prefer The Elements set to that tune, myself...

    theeeeresss antimony, arsenic, aluminum, selenium...

  • Lehrer was here. (unregistered) in reply to The Great Lobachevsky
    The Great Lobachevsky:
    eVil:
    Well, you can fit the word "Blah" to The Modern Major General Song if you really want to, but you can't reasonably say that "it fits". Its just words badly fit to some random tune.

    I prefer The Elements set to that tune, myself...

    theeeeresss antimony, arsenic, aluminum, selenium...

    And hydrogen and oxygen and nitrogen and rhenium, And nickel, neodymium, neptunium, germanium, And iron, americium, ruthenium, uranium, Europium, zirconium, lutetium, vanadium, And lanthanum and osmium and astatine and radium, And gold and protactinium and indium and gallium, <gasp> And iodine and thorium and thulium and thallium.

    There's yttrium, ytterbium, actinium, rubidium, And boron, gadolinium, niobium, iridium, And strontium and silicon and silver and samarium, And bismuth, bromine, lithium, beryllium, and barium.

    There's holmium and helium and hafnium and erbium, And phosphorus and francium and fluorine and terbium, And manganese and mercury, molybdenum, magnesium, Dysprosium and scandium and cerium and cesium. And lead, praseodymium, and platinum, plutonium, Palladium, promethium, potassium, polonium, And tantalum, technetium, titanium, tellurium, <gasp> And cadmium and calcium and chromium and curium.

    There's sulfur, californium, and fermium, berkelium, And also mendelevium, einsteinium, nobelium, And argon, krypton, neon, radon, xenon, zinc, and rhodium, And chlorine, carbon, cobalt, copper, tungsten, tin, and sodium....

  • (cs) in reply to The Great Lobachevsky
    The Great Lobachevsky:
    eVil:
    Well, you can fit the word "Blah" to The Modern Major General Song if you really want to, but you can't reasonably say that "it fits". Its just words badly fit to some random tune.

    I prefer The Elements set to that tune, myself...

    theeeeresss antimony, arsenic, aluminum, selenium...

    Yeah but that works, because Mr. Lehrer sat down and figured it out properly..... and hydrogen and oxygen and nitrogen and rhenium... and now look what you've made me do.

    Edit: I bet the guy above didn't type that from memory.

  • Anomaly (unregistered) in reply to The Great Lobachevsky
    The Great Lobachevsky:
    eVil:
    Well, you can fit the word "Blah" to The Modern Major General Song if you really want to, but you can't reasonably say that "it fits". Its just words badly fit to some random tune.

    I prefer The Elements set to that tune, myself...

    theeeeresss antimony, arsenic, aluminum, selenium...

    I prefer the tune to "I am the very model of a Scientist Salarian." Or for the slightly older crowd who remembers Reboot. "Everything is alphanumeric."

  • Ken B (unregistered) in reply to golddog
    golddog:
    (Nested ternary "?:" operator.) Fewer lines not a goal. Maintainability is.

    That said, it's a case-by-case basis. I've used nested ternary when it's a simple case and was still easy to read.

    Just try to review your code from the perspective of an outsider: is it something the functionality of which can be gleaned relatively quickly and properly? If not, go to the more verbose if-then-else construct.

    I have been known to occasionally use a nested "?:" operator. However, they are few and far between in my code, and I always use multiple lines and "proper" indentation to make it more human-readable. For example:

    ( condition_1 )
    ? ( ( condition_2 )
        ? value_1
        : value_2
      )
    : ( ( condition_3 )
        ? value_3
        : value_4
      )
    
    (I sure hope I didn't make a typo in that.)
  • (cs) in reply to The Great Lobachevsky
    The Great Lobachevsky:
    eVil:
    Well, you can fit the word "Blah" to The Modern Major General Song if you really want to, but you can't reasonably say that "it fits". Its just words badly fit to some random tune.

    I prefer The Elements set to that tune, myself...

    theeeeresss antimony, arsenic, aluminum, selenium...

    Algeria Bulgaria Cambodia Dominica Egypt France the Gambia! Aitch-teem-ell, zero. Japan Kazakhstan, Lybia Emm-el, HTML! Norway, Oman, Shell exec, Qatar Russia mysql Turkey Uruguay Vietnam. Handle the error! Yemen Zimbabwe!

  • (cs) in reply to DCRoss
    DCRoss:
    Algeria Bulgaria Cambodia Dominica Egypt France the Gambia! Aitch-teem-ell, zero. Japan Kazakhstan, Lybia Emm-el, HTML! Norway, Oman, Shell exec, Qatar Russia mysql Turkey Uruguay Vietnam. Handle the error! Yemen Zimbabwe!

    Did you just have a stroke in your superior temporal gyrus?

  • (cs) in reply to eViLegion
    eViLegion:
    Did you just have a stroke in your superior temporal gyrus?

    Perhaps singing code out loud is not a thread-safe operation.

  • (cs) in reply to DCRoss
    DCRoss:
    eViLegion:
    Did you just have a stroke in your superior temporal gyrus?

    Perhaps singing code out loud is not a thread-safe operation.

    So, definitely a stroke then.

  • (cs) in reply to Mark Bowytz
    Mark Bowytz:
    Depends on how you choose to pronounce the symbols, or if you choose to. It also can fit to "Happy Birthday" and "Les Petits Reins" if you're creative.
    This reference confuses me. I keep trying to read it as "The little kidneys" - although this word "kidney" requires care when translating between English and French, as the sort of kidney you eat (non-cannibalistically!) is not a "rein" but a "rognon". The ones in the rear part of your abdomen are "reins", and they are "reins" while still inside a living animal, changing to "rognons" when they are removed and used for culinary purposes.
  • Lehrer was here. (unregistered) in reply to eViLegion
    eViLegion:
    Edit: I bet the guy above didn't type that from memory.

    Yah, I had to look up the rest of the lyrics...

    I can still recite "Mother", "Pigeons", and "Irish Ballad" from memory though (much to the dismay of those around me...)

  • Hotline for ternary abuse (unregistered)

    Let's see if formatting that ternary makes it any clearer... (pffftt...!)

    return (isEdited() ? ((!usePercentage()) ? editedFixedAmount
                                             : ((percentageOfBasis == null) ? NullMoney.NULL_AMOUNT
                                                                            : basis.multiply(percentageOfBasis)))
                       : NullMoney.NULL_AMOUNT);

    Nope, no luck. The author of this pile of shite should be sentenced to debugging and modifying this pile of code excrement on a Friday afternoon before a holiday weekend.

  • (cs) in reply to golddog
    golddog:
    Fewer lines not a goal. Maintainability is.

    Ah, but people have coding standards you know! The type of thing that says 'there shall be no more than 20 lines per function'. So, you have to squeeze as much into each line as you can! You can even fit several actions onto a single line if you use commas judiciously, even if your coding standards mandate only on statement per line.

  • Ironside (unregistered)
    public Money getFinanceCredit() { return (isEdited() ? ((!usePercentage()) ? editedFixedAmount : ((percentageOfBasis == null) ? NullMoney.NULL_AMOUNT : basis.multiply(percentageOfBasis))) : NullMoney.NULL_AMOUNT); }

    I rewrote it clearer:

    public Money getFinanceCredit() { if (!isEdited()) return NullMoney.NULL_AMOUNT; if (!usePercentage()) return editedFixedAmount; if (percentageOfBasis == null) return NullMoney.NULL_AMOUNT; return basis.multiply(percentageOfBasis); }

    This still isn't good though. The main problem is that the method is doing too much. It should just return the finance credit, not do validation as well. Validation should be done in a separate method which can be called if necessary.

    public Money getFinanceCredit() { if (!IsValidState()) return NullMoney.NULL_AMOUNT; if (usePercentage()) return basis.multiply(percentageOfBasis); return editedFixedAmount; }

    public bool IsInputValid() { if (!isEdited()) return false; if (usePercentage() && percentageOfBasis == null) return false; return true; }

    Although there are still more problems such as the need for NullMoney.NULL_AMOUNT at all and the fact percentageOfBasis can equal null (why not have a default?). The naming also is faulty and unclear.

    basis.multiply(percentageOfBasis)

    What does that have to do with finance credit? the term finance credit isn't in the names of those variables at all.

    And NullMoney.NULL_AMOUNT. WTF does that even mean?

    Surely it means something like Money.InvalidState

    But even that is a WTF why not return null?

  • Jeremy (unregistered) in reply to Ken B

    That definitely improves on it, but I personally I still feel it's needlessly complicated. If you're not doing something trivial like

    i%2==0?"Even":"Odd"
    then I have to disagree with a ternary approach. Even a somewhat veteran programmer needs to "process" that way more than should have to considering the appropriate if/else structure could be followed by someone 2 days into programming 101.

  • (cs) in reply to pscs
    pscs:
    golddog:
    Fewer lines not a goal. Maintainability is.

    Ah, but people have coding standards you know! The type of thing that says 'there shall be no more than 20 lines per function'. So, you have to squeeze as much into each line as you can! You can even fit several actions onto a single line if you use commas judiciously, even if your coding standards mandate only on statement per line.

    Yeah... maybe you should architect your code to break large functions into a number of smaller, maintainable functions, then you wouldn't have to rely on esoteric language features to smugly get round that shit.

  • (cs) in reply to Hotline for ternary abuse
    Hotline for ternary abuse:
    Let's see if formatting that ternary makes it any clearer... (pffftt...!)
    return (isEdited() ? ((!usePercentage()) ? editedFixedAmount
                                             : ((percentageOfBasis == null) ? NullMoney.NULL_AMOUNT
                                                                            : basis.multiply(percentageOfBasis)))
                       : NullMoney.NULL_AMOUNT);

    Nope, no luck. The author of this pile of shite should be sentenced to debugging and modifying this pile of code excrement on a Friday afternoon before a holiday weekend.

    Rewrite it in if()ese:

    if( isEdited() )
    {
      if( !usePercentage() )
        return editedFixedAmount;
      else if( percentageOfBasis == null )
        return NullMoney.NULL_AMOUNT;
      else
        return basis.multiply(percentageOfBasis);
    }
    else
      return NullMoney.NULL_AMOUNT;
    Or alternatively, in English-like:
    If the thingy was edited, then use the percentage times the basis if we are using a percentage and one is available, or if we are not using a percentage, use the fixed amount. If none of the above conditions are met, use the null amount.
    In general, I'd prefer to invert the is-null test so that the positive "use-something" conditions are clumped together, and I'd prefer a function "useFixedAmount()" that is equivalent to "!userPercentage()" so that the test looks more related to the code in the branches, but it's reasonably clear.
    if( isEdited() )
    {
      if( useFixedAmount() )
        return editedFixedAmount;
      else if( percentageOfBasis != null )
        return basis.multiply(percentageOfBasis);
      else
        return NullMoney.NULL_AMOUNT;
    }
    else
      return NullMoney.NULL_AMOUNT;
    Better?
  • Jeremy (unregistered) in reply to eViLegion
    eViLegion:
    pscs:
    golddog:
    Fewer lines not a goal. Maintainability is.

    Ah, but people have coding standards you know! The type of thing that says 'there shall be no more than 20 lines per function'. So, you have to squeeze as much into each line as you can! You can even fit several actions onto a single line if you use commas judiciously, even if your coding standards mandate only on statement per line.

    Yeah... maybe you should architect your code to break large functions into a number of smaller, maintainable functions, then you wouldn't have to rely on esoteric language features to smugly get round that shit.

    Indeed. I'm not sure I agree with just declaring "no function can be over 20 lines, ever, period." But the point of such a declaration is that a large function is a sign you're doing something else wrong, not an invitation to "collapse" your overdone function into an even crappier version.

  • neminem (unregistered) in reply to Lehrer was here.
    Lehrer was here.:
    eViLegion:
    Edit: I bet the guy above didn't type that from memory.

    Yah, I had to look up the rest of the lyrics...

    I can still recite "Mother", "Pigeons", and "Irish Ballad" from memory though (much to the dismay of those around me...)

    I have Oedipus Rex and Bright College Days memorized. And most of Poisoning Pigeons in the Park and Pollution, but I blank on lines sometimes. Only have about a verse of The Elements memorized; that's all you really need.

  • (cs) in reply to JimLahey
    JimLahey:
    I think me and Jeremy have worked at the same place. The guru there would always quote "belt, braces and superglue" when I asked him about his massive WTFs.
    A quote is only a quote if it's quoted from something. WTF is "belt, braces and superglue"?
  • not else (unregistered)

    So its agreeded then.

    "if not x then without-x else with-x" is a bad pattern.

    Prefer "if x then with-x else without-x".

  • nmare (unregistered) in reply to Ken B
    Ken B:
    I have been known to occasionally use a nested "?:" operator. However, they are few and far between in my code, and I always use multiple lines and "proper" indentation to make it more human-readable. For example:
    ( condition_1 )
    ? ( ( condition_2 )
        ? value_1
        : value_2
      )
    : ( ( condition_3 )
        ? value_3
        : value_4
      )
    
    (I sure hope I didn't make a typo in that.)

    Then why dont you use a good old if() structure? There no adventage to use ternary ops in this case.

    Ternary operator are always horrible to read. The only case where i use it is on SSIS with the conditional split because you can only enter ONE line of code w/o indentation/carriage return. It often give a mess worst then the today's wtf.

    You should kill the inventor of ternary ops!

  • (cs)

    heh

    var isReadable = (isWTF ? getWTF() : (makeReadable() ? getReadable() : FILE_NOT_FOUND));
    
  • nmare (unregistered) in reply to pscs
    pscs:
    golddog:
    Fewer lines not a goal. Maintainability is.

    Ah, but people have coding standards you know! The type of thing that says 'there shall be no more than 20 lines per function'. So, you have to squeeze as much into each line as you can! You can even fit several actions onto a single line if you use commas judiciously, even if your coding standards mandate only on statement per line.

    The real coding standars is: Make your code readable!

    using an horrible ternary to save you 3-4 lines of code is worst then make a function with over 20 lines.

  • bob (unregistered)

    Multiple returns in a statement? Blasphemy!

  • Niko (unregistered)

    TRWTF is, as usual, PHP.

    // on first glance, the following appears to output 'true'
    echo (true?'true':false?'t':'f');
    
    // however, the actual output of the above is 't'
    // this is because ternary expressions are evaluated from left to right
    
    // the following is a more obvious version of the same code as above
    echo ((true ? 'true' : false) ? 't' : 'f');
    

    If anyone working on your codebase has even thought about PHP, ternary operators should be verbotenated.

    (And anyone who nests them should be sucker-punched once for every ? and twice for every : in the expression)

Leave a comment on “Ternary Over a New Leaf”

Log In or post as a guest

Replying to comment #:

« Return to Article