|Think about Loose Coupling|
When Is Dividing Code Into Different Subroutines/Packages Important?by Missing Words (Scribe)
|on Jun 14, 2003 at 03:01 UTC||Need Help??|
Missing Words has asked for the
wisdom of the Perl Monks concerning the following question:
Through a serious of events that I am sure no one here at the monestary wants to here, a program that I use often got deleted. This actually comes as a blessing in disguise since the code was fairly messy(It was my first perl program that actually did anything of use for me). I was laying out a basic template for the structure of the program and stumbled upon a question. When is it appropriate separate code into packages and subroutines? Should you only put something into a subroutine if it is code that is used multiple times?
Here is the template for the program I am creating:
PassMasker is a module I created which contains a fairly simple subroutine which allows a password to be given by the user, but only stars(or any choosen mask) will show up on the screen. (See this node if you are interested in it)
Now to the questions. I felt it was a good idea to separate the code dealing with the user interface from the code dealing with the database. Futhermore, I have given the interface to the a separate package. I did this becuase I think it will improve maintainability,since I would be able to seperate problems caused by my communication with the database I am using(probably a plain DBM file) from problems caused by my communcation with the user. However I am very new to perl and programming, and am not sure if this is the best way to proceed. Am I simply adding more complication becuase of the comminication between the different subroutines and if some of these subroutines are only used once is this just a waste of space?
To give an example:
As I am typing this, I am realizing the exact meaning of what I am asking may be ambiguous without code to show what would be going on in each sub. Here is an attempt to pose a more general question:
I feel that if I am ever going to learn to program well I need to start making clean, managable code. Any suggestion on this topic would be very helpful, so I can at least be headed in the right direction before I start the more major coding.