Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: Ternary operators: a hinderance, not a help

by fizbin (Chaplain)
on Aug 09, 2005 at 20:55 UTC ( #482426=note: print w/replies, xml ) Need Help??


in reply to Re^2: Ternary operators: a hinderance, not a help
in thread Ternary operators: a hinderance, not a help

I think that what you may be missing is that vertical space savings often enhance readability in terms of making it possible to see the whole forest at once.

In the same vein, I've seen (mostly beginning C++) people say that printf-formatted strings are too much like line noise, and everything should really be formatted through the expressive cout format, with operators to change the number of digits of precision, etc.

The same people might well complain about pack. The common lisp community often fields complaints about how terribly obscure the syntax is to format.

The point is that compressing stuff that's not terribly important to understanding the whole improves understanding of the code overall. Good code should read like stuff you want to read, and my eyes glaze over with too much pointless verbosity. (Usually, my eyes glazing is a sign that the code I'm reading suffers from severe cut-and-paste and really needs to be refactored to deal with multiple cases in a more generic fashion.)

You've obviously encountered several bad uses of the ?: operator. Allow me to present at least one good one:

my $numspells = ($doDeanStuff ? 1 : 0) + ($doSeamusStuff ? 1 : 0) + ($doHarryStuff ? 2 : 0) # Harry's stuff takes multipl +e spells + ($doRonStuff ? 1 : 0) + ($doSeverusStuff ? 5 : 0); # it's really complicated print "Budget for this incantation is $numspells spells\n";
Now in this case I'm sure you could do:
my $numspells = 0; $numspells += 1 if ($doDeanStuff); $numspells += 1 if ($doSeamusStuff); $numspells += 2 if ($doHarryStuff); # Harry's stuff takes multiple sp +ells $numspells += 1 if ($doRonStuff); $numspells += 5 if ($doSeverusStuff); # it's really complicated
But I think that the first version is easier - or at least, faster - to read. (more whitespace per bit of meaning) And code that's faster to read is more likely to get read in full before someone makes changes to it.

(Yeah, I'm just getting around to reading book 6 now)

-- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://482426]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2020-06-05 01:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (35 votes). Check out past polls.

    Notices?