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.

#Convert latitude and longitude to DMS and save $lat = getlat(); convert(\$lat); #Cut and paste goes horribly wrong $long = getlat(); convert(\$lat); print FH "Lat: $lat, Long: $long\n";

I actually did that once. Now if I had code that looked like:

print FH "Lat: ", convert_lat(), " Long: ", convert_long(), "\n";

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.

____________________
Jeremy
I didn't believe in evil until I dated it.


In reply to Re: When Is Dividing Code Into Different Subroutines/Packages Important? by jepri
in thread When Is Dividing Code Into Different Subroutines/Packages Important? by Missing Words

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.