Notes: Perl Lab 14

Program Design

The following are some things that can be done to help in the design, development, and maintenance of a program. Most are not specific to Perl and can be applied to most scripting or programming languages.
  1. Look at the whole problem.

    Don't take the requirements and deal with them linearly. Read through the entire problem (if it is written) so that it is fully understood.
  2. Break the problem down into smaller pieces.

    Look at the overall problem and look at the steps that will be necessary to complete the task. Define each step as a separate problem to be solved.
  3. Outline how the program will function.

    This does not need to be a formal flow chart but rather, a simple outline of what operations will need to be performed. This can be done simply by writing in my comments and then later filling in the actual code for the program.
  4. Write and test each step of the program one at a time.

    Do NOT write the entire program and then start debugging. Doing this will waste a lot of time and make it much more difficult to isolate errors. Use print statements to verify the values of variables when debugging.
  5. Use subroutines.

    • Subroutines are a great way of breaking down the overall logic of the program into discrete problems. With well defined arguments and return values it is much easier to find problems in the code itself.
    • Keep all variables local unless they MUST be global. This ensures that the subroutine code does not alter global values in an unexpected manner.
    • Use return values instead of altering global variables.
  6. Use indentation.

    indentation and consistent code formatting make it much easier to identify which groups of code are part of a given block structure. It also helps to be able to visually recognise when a block of code is not properly defined (such as mis-matched braces). Nested blocks can be difficult to find without indentation.
  7. Use descriptive variable names.

    If the variable names actually reflect the values they are expected to store then it will make reading the code and debugging much easier. Good variable names reduce the need for code comments.
  8. Comment your code.

    This should be a given. Not every line needs a comment but there should be enough comments to explain what is happening with each block of code. Any unusual syntax or complex statements should specifically be commented.
  9. Make the beginning of your code a configuration area.

    • Define constants and path values at the beginning of the program. This makes it much easier to maintain the program. When the values of a path or constant much change, it is much easier to find them if they are always stored in a easily located place in the code. If the code has to be searched, it is very easy to miss something important.
    • Add in any modules or "required" libraries at the beginning of the code. This makes it easier to determine immediately what external dependancies the code may have.
  10. Look at other code examples.

    Don't waste time reinventing the wheel when your problem has already been solved. There are many sources for viewing code examples that are *LEGAL* to reuse. Do not use copyrighted code without the author's permission.
  11. Read the documentation.

    Perl has a wealth of good documentation. Use it.

last updated: 18 Mar 2012 12:44