Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: •Re: On Foreach Loops and Maintainability

by jordanh (Chaplain)
on Oct 12, 2002 at 13:44 UTC ( [id://204762]=note: print w/replies, xml ) Need Help??


in reply to •Re: On Foreach Loops and Maintainability
in thread On Foreach Loops and Maintainability

    "Capture regularities in code, irregularities in data", is the koan of the senior programmer. You've bucked that for a microoptimization.

To me, it seems that your approach is working against this maxim. You are embedding the irregularities in the code. Sure, they are data in the code, but you have to work out that fact because you build up a list of 1-5 and 19 and attach gee37 to the end of it.

I think that this:

@obj_args = qw/ arg1 arg2 arg3 arg4 arg5 arg19 gee37 /; for ( @obj_args ) { ... }

Is clearer. In this example, you've clearly separated the irregularities into a data structure and used that to drive the code.

This kind of separation has all kinds of maintenance advantages, not just clarity on first reading. Say the maintenance programmer is confronted with a case where the arguments arg3 and gee37 have strange behavior. It's easy to change the code for this and you're doing it in such a way as to make it very clear if someone else working with the maintenance programmer would immediately grasp the intent.

I much prefer:

# @objargs = qw/ arg1 arg2 arg3 arg4 arg5 gee37 /; # # test just arg3 and gee37 @objargs = qw/ arg3 gee37 /; for ( @objargs ) { ... }

To this:

#for (map("arg$_", 1..5, 19), "gee37") { # # test just arg3 and gee37 for ( qw/ arg3 gee37 / ) { ... }

This shows how the irregularity of the data is more clearly separated from the code. When you use something like:

(map("arg$_", 1..5, 19), "gee37")

You are using code to represent the irregularity of the data.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2024-03-28 10:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found