Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello Monks,

     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?

Readmore to see the template I have created and more detailed description of my question.


Here is the template for the program I am creating:
#!perl #RunLogger2 v0.001 use warnings; use diagnostics; use strict; use PassMasker; #####User Interface##### sub login{} sub login_parser{} sub menu{} sub menu_parser{} ###Data Checking### sub check_date{} ###DataBase Interface### { package Runner; sub new{} sub ran{} sub edit_run{} sub edit_runner{} sub print_log{} }

     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:

The subroutine Runner::new will probably only be used once during the program. Would it be better to simply put that code directly into Main::menu_parser, which will determine which subroutine to call based on the user input?

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:

Is there merit in sub-dividing code into packages/subroutines even if it is not syntactically needed?



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.

Thanks in Advance.


In reply to 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found