CSC/ECE 517 Fall 2009/wiki2 7 co: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 15: Line 15:
=== Cut & Paste ===
=== 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 [http://sourcemaking.com/antipatterns/cut-and-paste-programming | very discouraged practice]. Additionally, using this method can lead to [http://en.wikipedia.org/wiki/Copy_and_paste_programming | plagiarism].
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 [http://sourcemaking.com/antipatterns/cut-and-paste-programming very discouraged practice]. Additionally, using this method can lead to [http://en.wikipedia.org/wiki/Copy_and_paste_programming plagiarism].


=== Includes ===
=== Includes ===
Line 34: Line 34:
=== Gosub ===
=== Gosub ===


In [http://www.vintage-basic.net/ | 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:
In [http://www.vintage-basic.net/ 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
   10  FOR I=1 TO 5
Line 62: Line 62:
   end.
   end.


In this scheme, in order to update parameters with actions performed within the procedure, a parameter must be [http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_11/CH11-2.html#HEADING2-29 | '''passed by reference'''], effectively passing the memory address of the parameter so it can be effected directly.
In this scheme, in order to update parameters with actions performed within the procedure, a parameter must be [http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_11/CH11-2.html#HEADING2-29 '''passed by reference'''], effectively passing the memory address of the parameter so it can be effected directly.


=== Functions ===
=== Functions ===
Line 88: Line 88:
=== Methods ===
=== Methods ===


blah
'''Methods''' (in [http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html Java] and [http://www.cplusplus.com/doc/tutorial/classes/ 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 ===


blah
'''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
 
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 ===


blah
'''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 ===


blah
'''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.
 
* [http://java.sun.com/docs/books/tutorial/extra/generics/index.html Tutorial on Generics in Java]
* [http://msdn.microsoft.com/en-us/library/c570k3f3%28VS.80%29.aspx Overview of Generics in Visual C++]


=== Mixins/Modules ===
=== Mixins/Modules ===
Line 130: Line 165:


* Brooks, Jr., F.P. ''[http://en.wikipedia.org/wiki/The_Mythical_Man-Month  The Mythical Man-Month]''. Boston, MA: Addison Wesley Longman, Inc., 1995.
* Brooks, Jr., F.P. ''[http://en.wikipedia.org/wiki/The_Mythical_Man-Month  The Mythical Man-Month]''. Boston, MA: Addison Wesley Longman, Inc., 1995.
* [http://sourcemaking.com/antipatterns/cut-and-paste-programming | Cut-and-Paste Programming]
* [http://sourcemaking.com/antipatterns/cut-and-paste-programming Cut-and-Paste Programming]

Revision as of 00:38, 10 October 2009

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

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

blah

Aspect

blah

Comparing the Various Mechanisms

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 -
  • Lack of documentation -
  • Complexity -
  • Poor Design -
  • Licensing Issues -
  • Lack of Trust/Fear -

Conclusion

blah!

References