single-line terinary operators considered harmful

Clark C. Evans cce at clarkevans.com
Wed Mar 5 12:22:07 EST 2003


On Wed, Mar 05, 2003 at 11:06:46AM +0000, Stephen Horne wrote:
| If you are literally building the source automatically by inserting
| bits of expressions into a larger chunk of code (I assume that Zope is
| doing this, and I also have some code which does this) then having to
| obey indentation rules for the inserted expression is a nightmare.

It isn't that hard.  Also, for my dynamically-generated SQL I spend
a good amount of effort making sure it looks nice.  Nothing worse
than a bug in jumbled-into-a-single-line-auto-generated code.  ;)

| So is there another argument?
| 
| Well - the programmer is not forced to do all block structure by
| indentation in Python. There are aspects that are left to the
| programmers judgement. For instance, you can already write...
| 
|   if c : do_x; do_y; do_z

I don't use (nor do I miss) the semi-colon in my code...

| No-one would write a language which insists that basic arithmetic
| operators are structured with indentation. This can be useful,
| however, even with parentheses or precedence making the real decisions
| - for instance...
| 
|   a = (  (  (some_long_expression)
|           * (some_long_expression))
|        + (  (some_long_expression)
|           * (some_long_expression)))
| 
| ...but that's no reason to force it on everyone in every case.

Yes, but a conditional is unlike these binary operators,
it has, to quote Dannis Reinhardt, a 'dangling else' ambiguity.
And rather than making an ugly "endif" construct, I'd rather
force indentation for the conditional expression.

| As it happens, I tend to think of the else problem in existing if
| statements as a (very minor) wart. I see no problem with readability
| in something like...
| 
|   if b != 0 : print "a/b = ", a/b ;; else : print "div by zero"
|
| (using ';;' as a hypothetical 'stronger' end-of-statement operator)
|

You don't !?   In a word, *ouch*

| While I can do that in C++ (still keeping the braces, which I really
| don't like to omit) I've lost that capability in Python. It's
| certainly a very good trade overall, but I don't want to see even a
| very minor wart replicated unnecessarily.

I don't see Python's indentation as a wart; I see it as an
essential aspect which making Python code easy to read
and understandable without distracting indicators.

| I suspect that even Guido might be tempted if he were rewriting
| Python, not so much to put an else on the same line but because a lot
| of people complain about structuring by indentation from time to time.
| Yes - there are even people (though emphatically NOT me) who see all
| structuring by indentation as a wart.

And lucky for them there are lots of languages out there,
such as Perl, Ruby or even Java/C#, which suit their tastes
more closely.   I'd rather go without a conditional expression
than have one which uses parenthesis or god-awful "endif" 
ickyness instead of relying upon good-ole whitespace.

Winks,

Clark





More information about the Python-list mailing list