CSC/ECE 517 Fall 2009/wiki2 7 co

From Expertiza_Wiki
Revision as of 00:48, 11 October 2009 by Catowner (talk | contribs)
Jump to navigation Jump to search

Code Reuse Methods and Mechanisms

  The best way to attack the essence of building software is not to build it at all.
        - Fredrick P. Brooks, Jr.,The Mythical Man-Month

In his 1975 classic, The Mythical Man-Month, Fred Brooks claimed that the implementation of design (what he called "accidental tasks") was essentially efficient enough that improvements in that area would not result in significant gains in productivity. Twenty years later, in a follow-up edition, he refined and clarified his view. He recognized that the rise of object-oriented languages and methodologies had the potential and promise of easy reuse, although he was still skeptical of dramatic productivity claims. Fundamentally, though, he saw that overall software development productivity can be enhanced through reuse.

This page will review some of the different techniques that are available for code reuse, and then a comparison of the techniques will be presented.

Code Reuse Through Direct Code Use

There are several classic code reuse mechanisms which work at the fundamental source code level. This section will discuss this class of mechanisms.

Cut & Paste

One of the most basic reuse mechanism is the cut and paste method. Simply find the source code that performs the function that is required and copy the code into the place it is needed. This method can be implemented either by copying from some external source, such as a book or another software program, or by duplicating the same code within the same program. The latter method is generally a very discouraged practice. Additionally, using this method can lead to plagiarism.

Includes

In C and C++, the includes construct places the contents of the file specified in the include parameter in the spot where the include is placed:

  #include "time.h"
  
  class Time
  {...

In this example, the compiler effectively copies the code within time.h at the point where the #include statement is declared. Nearly all compilers will provide for specifying paths to search for the indicated file and provide rules or conventions for dealing with multiple occurrences of the same file.

Code Reuse through Subroutines

This section explores code reuse mechanisms that involve executing common blocks of code that can provide utility or common operations.

Gosub

In Vintage Basic (comparable to the Basic language dialects from the late 70s and early 80s), individual lines of code are designated by line numbers. Blocks of code can be designated as subroutines and the GOSUB command can jump to these blocks of code. Execution jumps back to the line following the GOSUB command once a RETURN is encountered in the subroutine:

  10  FOR I=1 TO 5
  20  GOSUB 100
  30  NEXT I
  40  END
 100  PRINT "Value = "; I
 110  RETURN

Note that in this construct, parameters are not passed to the subroutine, but since all variables were global, parameters could still be simulated through careful use of global variables.

Procedures

In Pascal, procedures provided a way to jump to a block of code, and parameters can be passed to that block as well:

  var j:Integer;
  
  procedure PrintValue(i: Integer);
  begin
     Write("Value = ");
     Writeln(i);
  end;
  
  begin
     for j := 1 to 5 do
        PrintValue(j)
  end.

In this scheme, in order to update parameters with actions performed within the procedure, a parameter must be passed by reference, effectively passing the memory address of the parameter so it can be effected directly.

Functions

Pascal also includes a construct called functions which allow a subroutine to return a value to the caller:

  var j:Integer;
  
  function Square(i: Integer): Integer;
  begin
     Square = i * i;
  end;
  
  begin
     for j := 1 to 5 do
        Writeln("Square of ", j, " = ", Square(j));
  end.

Although a similar construct can be created using procedures, functions allow for more concise coding.

Code Reuse through Extension

Many languages allow existing code to be reused through extensions that leave the original code unaltered. This section explores those mechanisms.

Methods

Methods (in Java and C++) are functions which are encapsulated within a class structure. The class structure can provide a level of data and implementation hiding which can facilitate better design.

  public class Square {
     ...
     public void draw(String s) {
        ...
     }
  }

In and of themselves, methods do not offer any additional capability over functions, but operating within a class structure, several object-oriented paradigms can make methods more effective for code reuse.


Inheritance

Inheritance is an object-oriented concept which allows a developer to create subclasses of a base class which can refine or expand the base class.

  Example TBD

Using inheritance, base classes can hold much of the common elements that are used by the subclasses, thus code is reused by the subclasses through the common base class.

Polymorphism

Polymorphism is another object-oriented concept which allows a developer to reuse a defined interface in a class, but provide for an implementation that is specialized to that specific class.

  abstract public class Shape {
     abstract public void draw();
  }
  
  public class Square extends Shape {
     public draw() {
        ...
     }
  }
  
  public class Circle extends Shape {
     public draw() {
        ...
     }
  }

In this example, the draw method has been indicated in the Shape class, but implemented in the subclasses Square and Circle. This example may not seem like code reuse, but it can be argued that the concept around providing a common signature for the subclasses is a type of reuse. Further, polymorphism provides one of the key foundations for object-oriented programming.

Generics

Generics in Java and C++ provide a mechanism where a type or class (such as String, int, or Shape) can be provided to a method as a parameter. In this fashion, the generic method can accommodate multiple types, or they can be used to catch potential typing issues at compile time that might otherwise be missed.

Mixins/Modules

  Text and Example needed

Aspect

  Text and Example needed

Comparing the Various Mechanisms

 Comparison table needed

Why Reuse Is Not Always Embraced

With all the various techniques available, code reuse is not universally embraced, either academically or commercially. What are the reasons for not reusing existing code?

  • Speed - explain
  • Lack of documentation - explain
  • Complexity - explain
  • Poor Design - explain
  • Licensing Issues - explain
  • Lack of Trust/Fear - explain

Conclusion

  Conclusion needed

References