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

Re: Short and easy way to write if...elsif syntax

by sundialsvc4 (Abbot)
on Aug 27, 2012 at 11:14 UTC ( [id://989963]=note: print w/replies, xml ) Need Help??


in reply to Short and easy way to write if...elsif syntax

Does it work?   Is it crystal clear?   Is it maintainable?

If the next condition that needs to be tested, for the next version of the software or for the next wild-hare idea that comes in from Marketing, is slightly different, will the logic still answer all of the preceding questions in the affirmative?   (Hint:   “No.”)

Replies are listed 'Best First'.
Re^2: Short and easy way to write if...elsif syntax
by sundialsvc4 (Abbot) on Aug 27, 2012 at 14:30 UTC

    To the “down-voters” among you, who may mistake brevity for lack of wisdom or experience, I invite your reconsideration.   Here’s why.

    The “clever improvements” as suggested cause each of the various if cases to become coupled.   In other words, as long as each and every if-case that could possibly be required, for the entire service lifetime of this application (which could be a decade or more), is identical ... the code is “clever,” and perhaps it may look a wee bit more agreeable to the digestion.

    However ...

    Change will come.   Some day, a condition will need to be added that will break the rule.   And, when that happens, suddenly the code that is working properly now must be torn-apart, more or less, and recoded.   What was meant to be clever has just turned bad, and it de-stabilized all of the logic that it “cleverly” tied together.   Whereas, if the “ugly” if..elsif structure had simply been retained, no changes to any of the existing code, with its admittedly repetitive structure, would have been required.   You would simply need to add another elsif block at the appropriate point.   You can keep that up indefinitely.

    Please bear in mind that I have spent most of my career in “code rescue” and project-turnarounds.   Which means that my perspective on such things is somewhat like that of a coroner.   Such “cleverness,” well-intentioned though it once may have been, after a surprisingly short amount of time attracts a large number of blowflies.   Maintainability is king, and software must be designed in anticipation of a very long service life involving many different people.   You won’t be at that job forever ... but your work will be, and someday I might be called in to have a look.

      The “clever improvements” as suggested cause each of the various if cases to become coupled.

      (Some annoying typography elided.) How so—especially in the given/when case?

        Given/When is syntactic sugar for if..elsif.

      The real problem here could be probably the choice of a hash for this work. The idea behind a hash should be that keynames are unique and not subject to small variations, like in this case.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-03-29 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found