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

Re: When do you function?

by Albannach (Prior)
on Dec 27, 2000 at 09:50 UTC ( #48389=note: print w/ replies, xml ) Need Help??


in reply to When do you function?

Just some random comments here... my basic principle (and this is just a layman's opinion ;-) is to isolate any repeated sequence of operations by placing them in a function/subroutine with a descriptive name. This can certainly be carried too far, so in practice I make functions of things that are becoming irritating to type or cut/paste from elsewhere.

I certainly agree with your first three points, and I'd like to add that the reduction of repetition also reduces coding errors. Even (especially?) when I cut and paste, I can introduce variations or subtle errors (often involving scoping) which are entirely avoided when I take the time to make a subroutine.

I find that building a sub forces me to think more fully about what exactly I'm trying to express as I try to make it as much a black-box as possible. Subs also give me ample room to add better error-checking and handling that may (gasp!) get left out if I were to strip down the operation and leave it in-line. And after all this extra effort, I get something that I can re-use elsewhere more easily than some sequence of lines from the middle of a big loop.

On your second set of points:

1 - On looping, I just don't like deep indentation, and especially if chunks of the loops are nicely isolated, I will put them in subs just to unclutter the structure, which leads me to commenting:

2- I tend to think the fewer comments the better, and that's not to make things harder for others. I mean that whenever I find myself making any comment at all (apart from header blocks which should be quite detailed) I ask myself just what is so confusing here, why isn't the code obvious, and can I make it obvious and avoid the comment altogether? Jumping from sub to sub shouldn't be confusing if they each do something that makes sense on it's own. For a trivial example, in $a = sin($b) * cos($c) the functions each have clear and obvious purposes of their own, and the thought of calculating them in-line would be a great starter for the "fattest obfuscation" category...

Finally there is the consideration of performance especially if you are passing a lot of data to a function (in which case you should probably pass a reference anyways but there are always issues...). I'd like to think that the compiler (speaking generally here) should optimize what I write and not really care if it's a subroutine or in-line, but again in practice this isn't the case (yet anyway) so it may well be that using a sub call can slow down an operation that I will perform millions of times to the point that I shouldn't make the call. When I wrote a lot of C I enjoyed making elaborate preprocessor definitions to get the best of both worlds, and to some extent I miss that in Perl.

I look forward to the more professional opinions of the learned monks on this question!

--
I'd like to be able to assign to an luser


Comment on Re: When do you function?
Download Code
On comments
by mirod (Canon) on Dec 27, 2000 at 21:24 UTC

    I just have one comment on... comments:

    I aggree that comments on code are often be redundant, and so should be used with caution.

    But comments on data should be all over the place, especially with Perl complex types (yes, I mean hashes!), a simple comment like

    my %nodes; # node_id => ref_to_node

    can go a _long_ way to help you or anybody else who has to maintain the code 3 month later.

      In my experience 90% of comments are unnecessary. But, on the other hand, 90% of the comments that should exist, are missing.

      Yes, code should be 'self-documenting', but comments should explain why you're doing what you're doing. It's no use being able to understand your regex or complex data structures etc if I can't work out why on earth you're doing things this way in the first place.

      Of course, to bring this back to the initial point, if you break everything down into simple, short, self-contained functions that do only one thing, and are well named, then it's going to be quite evident what they do, and POD will mostly be sufficient :)

      Tony

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2014-10-24 18:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (134 votes), past polls