Preventative Programming Techniques. Avoid and Correct Common Mistakes
Authors: Hawkins B.
Published year: 2002
Pages: 21-22/121
Buy this book on amazon.com >>

 < Day Day Up > 



Related Illnesses

The other illness that contributes most to the CAP Epidemic is Myopia. Cut-and-paste represents a quick-and-dirty short-term solution, but inevitably leads to problems in the long term. A greater focus on the long- term effects will reduce the amount of cut-and-past as the detrimental effects become obvious. Another contributor to code duplication is NIH Syndrome. Even if you are duplicating the functionality of code that is not developed in-house, the resulting effects of having two copies of the code can still rear its ugly head. Another programmer might decide to use the third-party algorithm, and then you will end up supporting two sets of code that do the same thing. The next chapter takes an in-depth look at this and other results of NIH Syndrome.

Using cut-and-paste when building the basis for the application leads to a brittle framework that is an occurrence of Brittle Bones. Changes become difficult, and as functionality is built on top of the framework, it uses different instances of the same functionality. Future changes to one instance but not the others can cause surprising results. Another illness that often manifests by accident when using cut-and-paste is i. This results from the placement of code into a new context without updating the variable names, which leads to poorly named variables that can mislead another programmer reading the code. A myopic solution to this is to ensure that you update the variable names , but a much better ultimate solution is to avoid the duplication of code altogether.



 < Day Day Up > 

 < Day Day Up > 



First Aid Kit

Ironically, the primary means of preventing a CAP Epidemic is not to use a particular tool: cut-and-paste. Nevertheless, there is more to it than that because if you still duplicate code and information without using cut-and-paste, you will be missing the point and creating extra work for yourself. You must therefore use tools that allow you to remove the need for duplicating information, particularly the need for programmers to perform the duplication manually.

You are likely to be using one of the primary tools for this purpose already, and that is the high-level language with which you are programming. The trick is to use the language to its full advantage, with the idea of removing duplication in mind. There are also language extensions, such as the aspect-oriented programming extension to Java named AspectJ, that provide additional methods for reducing duplication in ways that are not available in the standard language.

While the best method for preventing a CAP Epidemic is to avoid duplication entirely, it is not always possible due to limitations in the language or technology being used. This is where scripting languages such as Ruby, Python, Perl, and many others become important. Scripting languages allow fast implementation of useful utilities for automating information duplication tasks . Several of the common build utilities, such as Make and Ant, also have features that can assist in automating duplication.

If a CAP Epidemic has already reared its ugly head through your project’s code, it is time to refactor that code to remove the redundancies. To this end, refactoring tools such as Smalltalk’s refactoring browser and the refactoring options offered by IntelliJ’s IDEA for Java are excellent utilities to remove the tedious repetitive chores of refactoring. If you read Chapter 1, you will notice that refactoring tools were also mentioned there. Refactoring tools are important in curing many programmer illnesses, but are particularly useful for Premature Optimization and the CAP Epidemic due to their status as major illnesses and corresponding frequency of occurrence. Refactoring is an extremely useful process to begin with, and by removing the tedium and error-prone manual implementation, it becomes an essential part of a programmer’s repertoire .



 < Day Day Up > 

Preventative Programming Techniques. Avoid and Correct Common Mistakes
Authors: Hawkins B.
Published year: 2002
Pages: 21-22/121
Buy this book on amazon.com >>