Your skill will accomplish what the force of many cannot |
|
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
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! -- In reply to Re: When do you function?
by Albannach
|
|