Beefy Boxes and Bandwidth Generously Provided by pair Networks
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??


in reply to When Is Dividing Code Into Different Subroutines/Packages Important?

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.

Replies are listed 'Best First'.
Re: Re: When Is Dividing Code Into Different Subroutines/Packages Important?
by yosefm (Friar) on Jun 14, 2003 at 19:02 UTC
    That thing about not making a sub more than X lines has nothing to do with the brain. It's just something computer-sciences teachers do to make you learn how to divide your code into paragraphs acording to its use instead of writing long unreadable "spaghety code" (that's what we called complicated code back in the days of BASIC...)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-25 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found