Study Guides ©1998–2010 R. Hinton, Broome Community College Last Updated: 17–Feb–10 |
Code Development Algorithms |
---|
OverviewPrograms 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 PageWhether 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:
Identify and Eliminate All Duplicates Return to Top of PageTo 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:
Create a Function (JavaScript) or Method (Java) Return to Top of PageThe 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
Implementation
|