Study Guides
©1998–2010 R. Hinton, Broome Community College
Last Updated: 17–Feb–10

Code Development Algorithms
 

Overview

Programs are often very short when first learning a programming language. However‚ as time goes on‚ the number of instructions increases dramatically. In order to reduce the amount of time it takes to create and maintain programs‚ it is advantageous to identify and eliminate duplicate calculations and instructions as code is written. In the long run‚ this enables the development of reusable code (functions–JavaScript or methods–Java)‚ which further reduces the creation and maintenance time.


General Notes and Practices Return to Top of Page

Whether just beginning or writing complex code‚ the algorithm for reducing redundancy is the same. Many students wait until their code is written in the belief that they can go back and fix it later. However‚ this means going back and finding all the duplications‚ which not only adds time to the process‚ but introduces the chance of missing a duplicate.

It is actually better to identify duplications when they are originally created. At that point in time they are fresh in mind making them easier to identify and correct. Since this eliminates rewriting instructions later on‚ initial programs (and updates) take less time and are more accurate.

While this process appears more difficult at first‚ is actually easier because the student is forced to think more carefully about the instructions being written and their relationship to other instructions. They are also less likely to make computational errors since less calculations are being written.

The problem for most students is that they’ve never been provided instructions as to how this should be done. Therefore‚ the easiest place to start is with duplicate calculations. This can be done at any programmer’s skill level‚ but is especially useful to new programmers. It helps train them to look for duplications from the start‚ so they won’t have to "unlearn" bad habits when more complex programs are created. So to get started "on the road" to reduced instructions:

  1. The second time the same calculation or complete instruction is written is when a variable should be created and assigned the value.

Return to Top of Page


Identify and Eliminate All Duplicates Return to Top of Page

To make complex programs more manageable‚ a more thorough approach must be taken. Unfortunately‚ this isn’t as simple as with calculations‚ which are more easily remembered or located using an editor’s find tool.

However‚ this has the bigger pay–off!

Since computers essentially only perform four operations‚ input‚ processing‚ output‚ and storage‚ repetitive instructions are often a signal of commonly performed tasks. Isolating these instructions extends the benefits to any other program that also performs that task. Like a spreadsheet’s built–in functions (note word)‚ once written‚ they perform their tasks consistently and accurately as long as they are applied properly.

Instruction sets that perform a singular task are known as primitives. As the name implies‚ they are combined to perform more complex tasks. In fact‚ their singularity is what enables them to combine with various other primitives. The vast number of combinations allow for a diverse set of complex tasks so primitives are usually the most reusable. It is important to realize that not all sets of instructions are useful in other programs. However‚ that doesn’t mitigate the benefits to the current program. Consider this‚ is it easier to solve a big problem or a series of little problems? Most would agree that little problems are less overwhelming and more focused making them easier to solve. Like building blocks‚ each little solution is a "brick" towards the complete solution.

The instructions that combine the primitives are called drivers and aren’t usually as reusable as primitives. As a general rule‚ the less complex the task‚ the more reusable the code.

However‚ that isn’t their purpose‚ anyway!

Drivers group the tasks needed to create the screen elements. To make displaying them easier‚ drivers can be created for intermediate portions of the elements. Drivers can then be created to "assemble" the intermediate portions. Think of drivers as parentheses surrounding the code and‚ like parentheses‚ they reorder processing.

Each driver should represent a logical unit. This a meaningful item whether it is a complete screen element or some portion of one. Meaningful units facilitate development because they "trigger" related knowledge and experience buried in the subconscious. Create a series of these "little solutions" to create the complete program:

  1. The second time the same calculation or complete instruction is written is when a variable should be created and assigned the value.
    1. Going back to fix these takes more than twice the time it took to create them as they have to be found and finally changed.
    2. Doesn’t it make more sense to create the variable once and use it‚ rather than going through all this maintenance?
  2. The second time the same instruction is written in another function/method is when a variable should be created and assigned the value in the next higher up common function/method and passed to the different functions/methods using it.
  3. The second time a set of instructions are written is when a function/method should be created to perform those instructions and then substituted where they need to be performed.
  4. In each of these cases, the first instruction(s) can be copied and used as a model to create the generic (reusable) version.
    1. You may have modify the new generic version to make it work for multiple situations.
  5. Substitute the new version for the original instruction(s) and recompile code.
    1. If you get the same results‚ the generic version was probably written correctly.
    2. If you don’t‚ compare the two and see what is different. The usual problem is the data sent in. It may have to be passed a little differently if you made some changes to ensure the function/method operates generically.
    3. If you didn't make any changes, this may identify changes that need to be made to ensure generic operation of the function/method.

Return to Top of Page


Create a Function (JavaScript) or Method (Java) Return to Top of Page

The previous sections dealt with why eliminating duplication is important and provided general algorithms for reducing their occurrence. This section specifically provides instructions to help write functions/methods as when indicated in Step 3 above.

General Algorithm

  1. Copy the instruction(s) into the function/method.
  2. Substitute variables for the literal values.
    1. This leaves the common part as is‚ and enables the function/method to perform the same action with different data.
  3. Go back to original instruction and substitute a call to the function/method using specific values‚ variables‚ fields‚ or constants to customize.

Implementation

  1. To avoid mistakes‚ copy the instruction(s) to be moved.
  2. Paste into the function/method.
  3. Replace original values with variables passed in.
    1. Concatenate (+) the variable(s) to the common text for messages.
  4. Copy the function/method header without including the word function.
  5. Go back to original instruction(s) and paste text on the next line.
  6. Replace the variable name(s) with the original value(s) to customize the function/method.
  7. Comment out the original instruction(s).
  8. Run the modified version to see if it works.
  9. Once it works‚ find the other duplicate instructions and repeat Steps 5 – 8 until all original instructions have been replaced with function/method calls.
  10. When all statements have been moved and replaced‚ delete the commented out original instructions.

Return to Top of Page