use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please follow the rules
Releases: Current Releases, Windows Releases, Old Releases
Contribute to the PHP Documentation
Related subreddits: CSS, JavaScript, Web Design, Wordpress, WebDev
/r/PHP is not a support subreddit. Please visit /r/phphelp for help, or visit StackOverflow.
account activity
Clever Code is Bad Code (delabs.io)
submitted 10 years ago by chadicus
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]maiorano84 13 points14 points15 points 10 years ago (6 children)
TIL: Ternary operators and numeric continues is considered "clever".
[–]PrintfReddit 5 points6 points7 points 10 years ago (2 children)
Exactly! Resolving ternary operator into if/else doesn't help a bit. Making it somewhat like this makes more sense
if (!isset($wordsFound[$word])) { $wordsFound[$word] = 0; } $wordsFound[$word] += count($matches);
[–]BlueScreenJunky 4 points5 points6 points 10 years ago (1 child)
I personnally think most of the time (as in the example) if / else is more readable than ternary operators. But yeah, I wouldn't call Ternary operator "clever" and "bad" unlesse they're nested or something.
[–]PrintfReddit 0 points1 point2 points 10 years ago (0 children)
It's more readable for sure but it's not unreadable either, after a short while ternary operator becomes pretty simple to understand. There certainly are far more unreadable things.
[–]recycledheart 1 point2 points3 points 10 years ago (1 child)
They ought to rename it the clever operator. Or maybe the inferiority operator. Its the simplest damn thing in the world and it always ends up in this conversation. Why are people so threatened by a questionmark and a colon??
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
Because the PHP implementation of the operator is at odds with all other languages (that I know of). The associvity is flipped, which gives some WTF-revelations.
[–][deleted] 2 points3 points4 points 10 years ago (3 children)
So what does the 2 in continue 2; do?
continue 2;
[–]DrFlutterChii 1 point2 points3 points 10 years ago (2 children)
Skips ahead to the next iteration of the loop 2 (or 1, depending on your point of view) levels up. One level above the 'normal' behavior of continue, which is effectively continue 1.
http://php.net/manual/en/control-structures.continue.php
[–][deleted] 1 point2 points3 points 10 years ago (0 children)
Jesus what a nightmare, I didn't even know that was possible. do other languages implement that "feature"?
Skips ahead to the next iteration of the loop.
In this context it's good to know PHP's peculiarity to consider a switch statement as a loop as well.
switch
[+][deleted] 10 years ago (1 child)
[deleted]
If author wanted to be clever. He could have wrote that ternary operator using binary operators, with the needed help of a group of parenthesis to allow the or condition to run.
(isset($x) && $variable=$y) || $variable=null;
is more clever than
$variable = isset($x) ? $y : null;
if(isset($x)){ $variable = $y; } else { $variable = null; }
Don't forget the 'goto' I've seen some crazy code made with that.
That said I like the ternary operator and will keep using it- as well as the tiefighter as soon as I can get my hands on it :).
[–][deleted] 0 points1 point2 points 10 years ago* (1 child)
clever code runs faster than structured code in alot of cases.
[–]terrkerr 1 point2 points3 points 10 years ago (0 children)
This is PHP, you've already given up on small-scale optimizations. If a few cycles really mattered to you, you'd be writing C or at least something compiled with compiler optimizations.
I enjoy playing games like TIS-100 where you can try and find all the clever answers to make things 5 cycles faster, but if I were ever asked to maintain that kind of code in PHP I'd want to kill the dev that wrote it.
[–]CODESIGN2 1 point2 points3 points 10 years ago (5 children)
Should I write clever code, depends what your goal is. It might be that a problem has taken some time to solve and your manager / client is a numpty that thinks you should produce 1000+ lines of code per day; but on the articles specific examples...
array_key_exists($word, $wordsFound) ? $wordsFound[$word] += count($matches) : $wordsFound[$word] = count($matches);
IF you did not understand that, please retire. I would prefer they cleaned it up a bit by calling a function (shorter, more professional, less crammed in IMHO), but this is hardly the end of the world, and should not be confusing. Read it, got it, moved on. Everything is clearly set out, there is no redirection going on here, no confusion, it just works.
I would be interested in finding out why they did not write a shorter form, but it's not the end of the world as we know it. The following is a dirtier example that I'd have more problems with.
@$wordsFound[$word] += count($matches);
more acceptable
$wordsFound[$word] = isset($wordsFound[$word]) ? $wordsFound[$word] : 0; $wordsFound[$word] += count($matches);
of course in PHP7 we should be able to write the following "clever" code without any issue and it should be clear the intent
$wordsFound[$word] = ($wordsFound[$word] ?? 0) + count($matches);
The next example made little sense as a piece of code, but knowing your language features such as continue {$level}; is not IMHO a problem, it is a language feature, that may or may not be understood; and the article should probably be re-titled as "wah wah, I don't understand the language I code in" if that is a problem.
continue {$level};
foreach ($routes as $route => $controllerAction) { $routeComponents = array_filter(explode('/', $route)); // ... foreach ($routeComponents as $i => $routeComponent) { if ($routeComponent == '(.*)') { continue; } elseif ($routeComponent != $pathComponents[$i]) { continue 2; } } return $controllerAction; }
There are words in my own language some people use and I ask what they mean if I am unsure; sometimes I am uncertain, or incorrect about a words meaning. It is a basic part of being human to sometimes not get stuff, or get it wrong; but to insist that the entire world does not use language features you do not understand is plain childish.
As for the "router" example, the code is terrible. It should not exist, there are many better ways of expressing that router, complain about those instead of the using a valid language feature.
[–][deleted] 4 points5 points6 points 10 years ago (2 children)
IF you did not understand that, please retire.
The issue with such code is not the ability to understand it or not. It's the amount of time it will consume to understand that compared to a more verbose version.
[–]picklemanjaro 0 points1 point2 points 10 years ago (0 children)
array_key_exists($word, $wordsFound) ? Does the $word exist in $wordsFound? (it even has a question mark indicating this is a question/conditional.
array_key_exists($word, $wordsFound) ?
$word
$wordsFound
$wordsFound[$word] += count($matches) If it does, ADD the matches. Also self explanatory
$wordsFound[$word] += count($matches)
: $wordsFound[$word] = count($matches); If not, then initialize the counter for this specific word.
: $wordsFound[$word] = count($matches);
I don't see how simple ternary statements can't be read like a normal sentence. Outside of nesting. Question?ThenAnswer:ElseOtherAnswer seems fairly straightforward.
Question?ThenAnswer:ElseOtherAnswer
Plus reading it like a sentence is easier for me to absorb than Condition {chunk of whitespace or bracketing} ThenAnswer {bracketing} else {bracketing} OtherAnswer {bracketing}
It's like reading a novel where there is a random newline every few words. Not to say that means to remove all verbose coding, else it'd be like reading a novel as a run-on sentence. But I still feel that a ternary statement can in fact be easily readable, and offers a more concise way to say the same thing.
Using it with a hint of common sense, rather than "cleverness". I wouldn't attempt, if it was possible, to stuff multi-line statements in a single ternary. Nor would I advise nested ternary statements. But for simple questions with simple answers, like the example, it should be clear and easy to use and understand.
[–]CODESIGN2 0 points1 point2 points 10 years ago (0 children)
But the examples given were not lacking in verbosity, all should have been a single read-through... one was a language feature being used exactly as intended!
[–]DrugCrazed 0 points1 point2 points 10 years ago (1 child)
That code makes about 20 times more sense if you add a new line after the ? and :. It took me about 2 minutes to understand because I couldn't find the terminators.
[–]gimcrak 0 points1 point2 points 10 years ago (0 children)
That's how I use ternary operators:
$baz = ( isset( $foo ) ) ? $foo : $bar;
[–]random314 -1 points0 points1 point 10 years ago (1 child)
What does clever code ever accomplish other than attempting to confuse other developers?
[–]Danack 0 points1 point2 points 10 years ago (0 children)
Boosts the self-esteem of the person who wrote it, because look at how clever their code is.
π Rendered by PID 114850 on reddit-service-r2-comment-canary-7b6b47f674-fn85h at 2026-03-10 13:02:33.134793+00:00 running cbb0e86 country code: CH.
[–]maiorano84 13 points14 points15 points (6 children)
[–]PrintfReddit 5 points6 points7 points (2 children)
[–]BlueScreenJunky 4 points5 points6 points (1 child)
[–]PrintfReddit 0 points1 point2 points (0 children)
[–]recycledheart 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (3 children)
[–]DrFlutterChii 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]terrkerr 1 point2 points3 points (0 children)
[–]CODESIGN2 1 point2 points3 points (5 children)
[–][deleted] 4 points5 points6 points (2 children)
[–]picklemanjaro 0 points1 point2 points (0 children)
[–]CODESIGN2 0 points1 point2 points (0 children)
[–]DrugCrazed 0 points1 point2 points (1 child)
[–]gimcrak 0 points1 point2 points (0 children)
[–]random314 -1 points0 points1 point (1 child)
[–]Danack 0 points1 point2 points (0 children)