Do you know where your variables are? | |
PerlMonks |
Re: When Is Dividing Code Into Different Subroutines/Packages Important?by jepri (Parson) |
on Jun 14, 2003 at 13:35 UTC ( [id://265892]=note: print w/replies, xml ) | Need Help?? |
Is there merit in sub-dividing code into packages/subroutines even if it is not syntactically needed? Oh yes indeedy. Not only does breaking your code into subs enhance usability, promote code reuse, get your whites whiter and make your code smell fresher, it also helps you catch screw ups. Breaking code into subs gives you more protection against errors. If you fumble and put one too many '}' characters into your code, you can locate it faster. And it eliminates one of my most popular screw-ups: using the wrong temporary variable by mistake.
I actually did that once. Now if I had code that looked like:
then I couldn't have got confused - I wouldn't be able to accidently use the wrong variable. There was at one time a fad for having no routine longer than 7 lines (or 9 lines, or 13).You can take that to extremes (creating thousands of 7-line functions is obviously silly) but it's not a terrible thing to try for, so long as you know when to break it. I tend to arrange big code blocks into paragraphs of 5-10 lines, it seems natural to me. The concept was based on some urban legend about the brain only being able to concentrate on 7 things at a time. That might even be true, but I don't see how it translates to 7 lines. And as others have mentioned, you can change those subroutines a lot more easily than editing some lines buried in the middle of a large chunk of code.
____________________
In Section
Seekers of Perl Wisdom
|
|