- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Some people take pride in writing cryptic code in perl.
Admin
Its not a wtf, Its job security. :)
Admin
Admin
[quote user="DrYak"]I DO write such code, you insensitive clod...
...except mine happen to be correctly indented, fully commented, and do actually work.
The ternary operator is useful when you have to assign multiple different possible values to 1 single var-name and may later need to rename the var. Specially in languages that don't support the GNU-C feature ({ ... ; return_value; })
Preferring "if/then/else" clauses to ternary operators just for the sake of readability is like prefering "begin/end" to curly braces : it's just cosmetic without any real valid reason. } [/code]
Ugh, I HATE begin/end syntax! But yeah, same ol semantics.
Still hate it tho.
(Living in a Delphi/Sql Server wonderland of Begin/End)
Admin
I think the original claim that C is 'not only' a PDP-11 assembler on the basis of the ternary op is not quite correct. Misters K & R used the fancy machine's microcode-able instructions to implement an atomic test-and-set or something else equivalent - and I suspect how it ended up into C was the ternary op. Alternatively they could have microcoded a conditional-move instruction, which is quite handy.
Short-circuit boolean evaluation came to C much later.
Admin
That is why Ada works so well. If only there were more support ...
Admin
It's a classic arcade game. Do a search on it sometime.
Admin
The ternary operator is nice when used sparingly and in simple expressions, for example, something like: int num += bAdvance ? 1 : -1;
However, I have been known to abuse the operator in the past: for ( ++offset, num_conditional_constructs = 0; ( offset < expression->TokenOffset ) && ( ( expression->TokenList[ offset ].type != TOKEN_KEYWORD ) || ( expression->TokenList[ offset ].val != KW_colon ) || ( num_conditional_constructs > 0 ) ); ( ( expression->TokenList[ offset ].type == TOKEN_KEYWORD ) ? ( ( expression->TokenList[ offset ].val == KW_question ) ? num_conditional_constructs++ : ( ( expression->TokenList[ offset ].val == KW_colon ) ? num_conditional_constructs-- : 0 ) ) : 0 ), CopyToken( &subexpression->TokenList[ subexpression->TokenOffset++ ], &expression->TokenList[ offset++ ] ) );
Yes, that for-loop has no body. Yes, that's all on one line. Yes, it's horrendously unmaintainable (and full of WTFs). No, I've never changed it despite the fact that I know it's a mess. Sadly, I'm almost proud of making something this bad.
Incidentally, that code was part of a function that parsed ternary operators in a scripting language. Easily the most complicated part of the entire parser.
Admin
That should work, even in case of side-effectfull expressions.
Admin
The indenting actually makes it pretty easy to read. Of course, if you stripped out the formatting -- or worse, made the formatting intentionally deceiving -- that would be a nightmare.
Admin
You're in luck. The ioccc deadline is at the end of this month.
Admin
Could one of you people who have said that this is quite readable code explain to me how the next-to-last result ('' #'[Cancelling]') can ever be assigned to the $send_button variable?
Oh, and for those of you discussing associativity - please don't confuse associativity with precedence.
NOTE: the real WTF is that 'Canceling' is misspelled.
Admin
There you are.
Admin
Well, that's very readable if you're familiar with the idiom. If you aren't, you're going to have to unravel the semantics yourself. And that's not easy in a case like the one you presented.
Admin
Right associative means that
a ? b : c ? d : e
is evaluated as
a ? b : (c ? d : e)
not
(a ? b : c) ? d : e
You are confusing assocativity and precedence
Admin
Actually, it's some of the nicest perl code i've ever seen. Good work.
Admin
Thanks.
Admin
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.6
s the ?: operator evil since it can be used to create unreadable code?
No, but as always, remember that readability is one of the most important things.
Some people feel the ?: ternary operator should be avoided because they find it confusing at times compared to the good old if statement. In many cases ?: tends to make your code more difficult to read (and therefore you should replace those usages of ?: with if statements), but there are times when the ?: operator is clearer since it can emphasize what's really happening, rather than the fact that there's an if in there somewhere.
Let's start with a really simple case. Suppose you need to print the result of a function call. In that case you should put the real goal (printing) at the beginning of the line, and bury the function call within the line since it's relatively incidental (this left-right thing is based on the intuitive notion that most developers think the first thing on a line is the most important thing):
Now let's extend this idea to the ?: operator. Suppose your real goal is to print something, but you need to do some incidental decision logic to figure out what should be printed. Since the printing is the most important thing conceptually, we prefer to put it first on the line, and we prefer to bury the incidental decision logic. In the example code below, variable n represents the number of senders of a message; the message itself is being printed to std::cout:
All that being said, you can get pretty outrageous and unreadable code ("write only code") using various combinations of ?:, &&, ||, etc. For example,
Personally I think the explicit if example is clearer since it emphasizes the major thing that's going on (a decision based on the result of calling f()) rather than the minor thing (calling f()). In other words, the use of if here is good for precisely the same reason that it was bad above: we want to major on the majors and minor on the minors.
In any event, don't forget that readability is the goal (at least it's one of the goals). Your goal should not be to avoid certain syntactic constructs such as ?: or && or || or if — or even goto. If you sink to the level of a "Standards Bigot," you'll ultimately embarass yourself since there are always counterexamples to any syntax-based rule. If on the other hand you emphasize broad goals and guidelines (e.g., "major on the majors," or "put the most important thing first on the line," or even "make sure your code is obvious and readable"), you're usually much better off.
Code must be written to be read, not by the compiler, but by another human being.
Admin
Or, since saving newlines is one of the major advantages of the ternary operator, you have this slightly obfuscated version:
Oddly enough, Python 2.5 has a ternary operator (called a conditional expression), so the above could be written as:
For any vaguely complicated code (such as using two ternary operators) I'm strongly in favour of refactoring into something else entirely. However, there are examples (such as what we've just given) that aren't so complicated. For such simple cases I don't know which method would produce the most readable code in the long run.
Here here!Admin
I'm surprised that my remark about eliminating the entire extended selection statement never got any mention.
My initial thought was some sort of filter function, but that would be too alien to most C/Perl programmers :P, and besides, an even better idea occurred to me.
Would anyone else here think that the Right Thing for this would actually have been a lookup table or similar data structure (Perl hash tables are nice for this) that would allow you to handle the whole thing in one (easily extended) fell swoop? Just wondering.
Alternately, representing the different options as separate subclasses of a master controller class of some sort might work, pushing the whole problem into the underlying dispatch mechanism so the programmer can forget it entirely. It would probably be more work than is justified for the size of the problem, especially given what I recall of Perl's rather wonky OO support, and wouldn't really fit this particular problem well in any case, but it once it was done it would be a snap to maintain and extend.
Admin
Admin
says someone who presumably doesn't know perl.
No, it's not harder to read than C. Properly written Perl is much easier -- because it can do far more with the basic primitives than you can do in C. Yes, badly written perl is hard to read -- but then, so is badly written C.
Compare, say:
with
Admin
I remember I used to program a TI-86 like this, because it didn't have an 'if' operator (or I couldn't figure out what it was).
Admin
Yeah... I didn't actually have the slightest problem understanding this. I guess it's a Lisp thing.