Comment On Ternary Over a New Leaf

Dimitry likes it when the bad code he finds can be sung aloud. This particular line can be sung to the tune of “Rule Britannia”. [expand full text]
« PrevPage 1 | Page 2Next »

Re: Ternary Over a New Leaf

2013-07-03 07:40 • by TheIrritainer (unregistered)
$isFrist = 0;
$isFrist = (is_null($isFrist)) ? 0 : $isFrist;

Re: Ternary Over a New Leaf

2013-07-03 07:52 • by 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.

Re: Ternary Over a New Leaf

2013-07-03 07:55 • by Mark Bowytz
411633 in reply to 411632
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.

Re: Ternary Over a New Leaf

2013-07-03 07:58 • by Crash (unregistered)
411634 in reply to 411633
I almost got it to work with "row, row, row your boat".

Re: Ternary Over a New Leaf

2013-07-03 08:03 • by eVil (unregistered)
411636 in reply to 411633
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.

Re: Ternary Over a New Leaf

2013-07-03 08:06 • by 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.

Re: Ternary Over a New Leaf

2013-07-03 08:07 • by 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...

Re: Ternary Over a New Leaf

2013-07-03 08:13 • by Heisenberg (unregistered)
411639 in reply to 411637
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.

Re: Ternary Over a New Leaf

2013-07-03 08:24 • by 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.

Re: Ternary Over a New Leaf

2013-07-03 08:26 • by Ramchandra Apte (unregistered)
easter egg: click anywhere in "shell-shocked", and surprise!

Re: Ternary Over a New Leaf

2013-07-03 08:29 • by 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.

Re: Ternary Over a New Leaf

2013-07-03 08:47 • by ZoomST (unregistered)
411644 in reply to 411642
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.

Re: Ternary Over a New Leaf

2013-07-03 08:51 • by A (unregistered)
411646 in reply to 411637
Yes, they really should have been more careful and gone for something like:

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

Re: Ternary Over a New Leaf

2013-07-03 08:54 • by 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.

Re: Ternary Over a New Leaf

2013-07-03 08:54 • by Jim (unregistered)
How terribly unsafe. Better to do:

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

Re: Ternary Over a New Leaf

2013-07-03 09:01 • by 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.

Re: Ternary Over a New Leaf

2013-07-03 09:12 • by Krunt (unregistered)
411651 in reply to 411633
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".

Re: Ternary Over a New Leaf

2013-07-03 09:17 • by ¯\(°_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.

Re: Ternary Over a New Leaf

2013-07-03 09:21 • by gnasher729 (unregistered)
411653 in reply to 411647
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.

Re: Ternary Over a New Leaf

2013-07-03 09:29 • by Chaarmann (unregistered)
411654 in reply to 411649
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".

Re: Ternary Over a New Leaf

2013-07-03 09:34 • by dkf
411655 in reply to 411649
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?

Re: Ternary Over a New Leaf

2013-07-03 09:42 • by Anonymous Paranoiac (unregistered)
411657 in reply to 411654
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.

Re: Ternary Over a New Leaf

2013-07-03 09:46 • by golddog (unregistered)
411658 in reply to 411653
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.

Re: Ternary Over a New Leaf

2013-07-03 09:52 • by meh (unregistered)
411659 in reply to 411654
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.

Re: Ternary Over a New Leaf

2013-07-03 10:00 • by The Great Lobachevsky
411660 in reply to 411636
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...

Re: Ternary Over a New Leaf

2013-07-03 10:23 • by Lehrer was here. (unregistered)
411661 in reply to 411660
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....

Re: Ternary Over a New Leaf

2013-07-03 10:24 • by eViLegion
411662 in reply to 411660
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.

Re: Ternary Over a New Leaf

2013-07-03 10:25 • by Anomaly (unregistered)
411663 in reply to 411660
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."

Re: Ternary Over a New Leaf

2013-07-03 10:27 • by Ken B (unregistered)
411664 in reply to 411658
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.)

Re: Ternary Over a New Leaf

2013-07-03 10:29 • by DCRoss
411665 in reply to 411660
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!

Re: Ternary Over a New Leaf

2013-07-03 10:43 • by eViLegion
411667 in reply to 411665
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?

Re: Ternary Over a New Leaf

2013-07-03 10:45 • by DCRoss
411668 in reply to 411667
eViLegion:

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


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

Re: Ternary Over a New Leaf

2013-07-03 10:47 • by eViLegion
411669 in reply to 411668
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.

Re: Ternary Over a New Leaf

2013-07-03 11:17 • by Steve The Cynic
411670 in reply to 411633
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.

Re: Ternary Over a New Leaf

2013-07-03 11:18 • by Lehrer was here. (unregistered)
411671 in reply to 411662
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...)

Re: Ternary Over a New Leaf

2013-07-03 11:25 • by 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.

Re: Ternary Over a New Leaf

2013-07-03 11:28 • by pscs
411674 in reply to 411658
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.

Re: Ternary Over a New Leaf

2013-07-03 11:34 • by 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?

Re: Ternary Over a New Leaf

2013-07-03 11:34 • by Jeremy (unregistered)
411677 in reply to 411664
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.

Re: Ternary Over a New Leaf

2013-07-03 11:38 • by eViLegion
411679 in reply to 411674
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.

Re: Ternary Over a New Leaf

2013-07-03 11:44 • by Steve The Cynic
411680 in reply to 411673
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?

Re: Ternary Over a New Leaf

2013-07-03 11:50 • by Jeremy (unregistered)
411681 in reply to 411679
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.

Re: Ternary Over a New Leaf

2013-07-03 11:56 • by neminem (unregistered)
411684 in reply to 411671
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.

Re: Ternary Over a New Leaf

2013-07-03 11:56 • by Zylon
411686 in reply to 411640
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"?

Re: Ternary Over a New Leaf

2013-07-03 12:24 • by 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".

Re: Ternary Over a New Leaf

2013-07-03 12:26 • by nmare (unregistered)
411689 in reply to 411664
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!

Re: Ternary Over a New Leaf

2013-07-03 12:28 • by chubertdev
heh


var isReadable = (isWTF ? getWTF() : (makeReadable() ? getReadable() : FILE_NOT_FOUND));

Re: Ternary Over a New Leaf

2013-07-03 12:39 • by nmare (unregistered)
411692 in reply to 411674
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.

Re: Ternary Over a New Leaf

2013-07-03 13:07 • by bob (unregistered)
Multiple returns in a statement? Blasphemy!

Re: Ternary Over a New Leaf

2013-07-03 13:08 • by 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)
« PrevPage 1 | Page 2Next »

Add Comment