Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: (3) Ternary confusion

by arturo (Vicar)
on Feb 28, 2004 at 00:11 UTC ( #332423=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Ternary confusion
in thread Ternary confusion

I'm not going to quibble with the basic thrust of your advice, but the following quote struck my eye:

The above code is obfuscated seemingly for the sake of obfuscation.

What leads you to say that? Because it uses the ternary operator? The first thing I noticed about the code is that it put each significant chunk on its own line. That hardly seems like the practice of a determined obfuscator. Apart from the issue of whether or not a given developer is familiar with the ternary operator, it boils down to :

$db = [connection specification] or die;
which is plain 'ol idiomatic Perl.

There are a number of reasons one might write code like that, having nothing to do with obfuscation. Perhaps the original author liked the terse idiom and compensated for it by formatting the code the way s/he did. Maybe s/he had five minutes and somebody asked her/him to make the script use a different connection if it was running on a specific server.

Of course, if you're going to rewrite it for later expansion, you might as well move the connection selecting code off into its own subroutine or, if it's complex enough, module. But I think we should all be aware that developers don't always have the time to develop code like that right from the very start -- especially when you don't know whether your code is going to be in use two weeks after you write it, which is a distinct possibility.

If not P, what? Q maybe?
"Sidney Morgenbesser"

Replies are listed 'Best First'.
Re: (4) Ternary confusion
by dragonchild (Archbishop) on Feb 28, 2004 at 04:24 UTC
    Maybe it's because
    • I've had to write the same DBI connection code over and over
    • I've had to have a $dbh available in a bazillion different places
    • I've needed config files for other reasons
    • I'm just really paranoid about usernames/passwords in code
    but I find that making decisions in code about which database to connect to based on an environment variable is obfuscated by its very nature. The ternary operator is not obfu. I personally don't use it a lot (kinda like I don't use select alot), because I abstract decisions away to the entity that needs to make the decision. (I'm a refactoring-nazi, actually.)

    I'm going to take issue with your last paragraph. Any developer worth their salt should have a number of modules in use that allow for reuse. For example, I have a set of tools that would allow me to do almost any one-off task without writing much code for database connections, configuration, and the like. And, it's almost quicker to write it factored out than it is to write it as one big blob. Why? Because it's easier to verify that your code which may only have one chance to be executed will do it right. Because you're never noticed when you succeed, but you're in the spotlight if you fail. Developers will always have time to develop correctly, if they plan as if they will need to do so in the next hour.

    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://332423]
[Corion]: Congratulations Lady_Aleena ;)
[Lady_Aleena]: Thanks Corion.
[Lady_Aleena]: This is one of my oldest accounts on the web. 8)
[Corion]: Lady_Aleena: Hehe - I think this is also my oldest active account on the web. I have an old university email address but that only gets spam nowadays :)
Discipulus arrived at work 1 hour late.. he do not noticed the clock bell
[Lady_Aleena]: My Wikipedia account is 3 years - 10 days younger.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2017-04-26 07:39 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (469 votes). Check out past polls.