Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Two simple code style advice questions

by sundialsvc4 (Abbot)
on Jan 16, 2013 at 16:24 UTC ( #1013608=note: print w/replies, xml ) Need Help??

in reply to Two simple code style advice questions

I would prefer the code which states, most simply and obviously, what the intent of the designer is ... and, also, which builds the maximum flexibility for the future.

In case #1, if there was a legitimate other use for @tests as an array (versus using keys(%ntests)), and if the value-initialization of %ntests should always be zero .. likely .. then I would use a simple foreach my $key (@tests) { %ntests{$key} = 0; }, written on three source-lines.   Is it “shorter?”   Clearly not.   Is it “faster?”   (Rhett Butler, Gone With The Wind)   But it is clear, to almost anyone who’s written a program in any language out there.   And it’s easily maintainable going forward:   you can put anything you need to inside that block, at any time in the future.

In case #2, the first alternative is instantly “outed.”   (Say what you mean.)   And once again I would perhaps write something like:   my $mol = ""; if ($n == 42) { $mol = "forty-two"; }   Once again, I am looking towards the future, after two years of programmers meddled with this same bit of code and it grew over all those years into something totally different.

Basically, when someone writes the first bit of code, whatever it is, they have a very clear notion in their heads at that time of what they’re trying to write at that time, and they get the idea stuck in their heads that there are brownie-points for being “clever.”   We’ve even got a “golf” section here, and it’s fun, but it’s just for fun and we all know it ... or, we should.   But, what happens over the next several years?   Well, one day, a change surely comes to pass which negates the assumptions that allowed the O.C. to have been “clever” in the first place, and that means that his entire block of code must be replaced.   First, it must be correctly understood.   Then, it must be correctly replaced with code that correctly does, not only what the new-change needs to do (which is why the current coder is dealing with this code now at all), but also everything that it did in the past.   Depending on the code, that entirely-unwanted voyage of discovery can become huge, and profoundly de-stabilizing.   A great big cost and delay, maybe, and all for nothing.   “Thanks for nothing, clever-one wherever you are now ...”

  • Comment on Re: Two simple code style advice questions

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1013608]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2018-08-15 06:41 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (159 votes). Check out past polls.