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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 200: Line 200:
   Comparison table needed
   Comparison table needed


== Why Reuse Is Not Always Embraced ==
== Encouraging Reuse ==


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?
With all the various techniques available, code reuse is not universally embraced, either academically or commercially. [http://www.itstrategycenter.com/wbbm/Res/analytics/black_art_of_reusing_software/index.html CIO Strategy Center] recommends 4 key actions to encourage code reuse in a company:


* '''Speed''' - explain
* Build a business case to get buy-in from the organization's business leaders.
* '''Lack of documentation''' - explain
* Establish repeatable, practical processes which reduce the potential roadblocks in developing reusable code.
* '''Complexity''' - explain
* Focus on ways the design process can facilitate code reuse.
* '''Poor Design''' - explain
* Shift the business culture to a reuse mindset.
* '''Licensing Issues''' - explain
 
* '''Lack of Trust/Fear''' - explain
From an academic perspective, encouraging code reuse can be tricky, especially given concerns about maintaining academic integrity. One way education institutions are doing both is through encouraging students to participate in [http://arstechnica.com/open-source/news/2009/06/academic-source-code-dust-up-symptom-of-cs-education-ills.ars open source projects].


== Conclusion ==
== Conclusion ==

Revision as of 02:16, 13 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 {
     int size;
     ...
     public void draw() {
        ...
     }
  }

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.

  public class Rectangle {
     int width, height;
     public Rectangle(int w, int h) {
        ...
     }
     ...
  }
  
  public class Square extends Rectangle {
     int size;
     public Square(int s) {
        super(s,s);
        ...
     }
     ...
  }

In this example, the Square class extend the Rectangle class. In the Square constructor, the Rectangle constructor is called and, thus, is reused.

Using inheritance, base classes can hold much of the common elements that are used by the subclasses. 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.

One example of generics in Java is in the java.util class for collection classes. In the declaration for the Collection class, the type is declared in angle brackets ("<Type>") and is implicitly provided as a parameter to the class and instance methods for an object of that class. So, instead of having to write one Collection class for Integers, another for Strings, another for Shapes, etc., using generics allows a single class to support multiple types, which enables the class to be utilized to a much greater extent.

Mixins/Modules

In Ruby, classes and modules can be combined to provide a "mixin". Modules are code blocks which define methods which are not directly associate with a particular class:

  module Greeter
     def sayHi(name)
        puts "Hi $#{@name}"
     end
  
  module Shouter
     def yellHello(name)
        puts "HELLO $#{@name}"
     end

As you can see, sayHi and YellHello are methods with implementations, but they are not associated with a specific class. These modules can be associated with a class in one of two ways. Through the class definition:

  class Talker
     include Greeter
     extend Shouter
     ...
  end
  
  class TalkerToo
     ...
  end

...or an instance of the class can be mixed at runtime:

  ttoo = TalkerToo.new
  ttoo.extend Shouter
  ttoo.include Greeter

Extending a class or instance with a module adds the methods as class methods. Including a class or instance with a module adds the methods as instance methods.

The Ruby language provides some specific modules that can be used to extend classes such as Comparable and Enumerable.

Aspect

  Text and Example needed

Comparing the Various Mechanisms

 Comparison table needed

Encouraging Reuse

With all the various techniques available, code reuse is not universally embraced, either academically or commercially. CIO Strategy Center recommends 4 key actions to encourage code reuse in a company:

  • Build a business case to get buy-in from the organization's business leaders.
  • Establish repeatable, practical processes which reduce the potential roadblocks in developing reusable code.
  • Focus on ways the design process can facilitate code reuse.
  • Shift the business culture to a reuse mindset.

From an academic perspective, encouraging code reuse can be tricky, especially given concerns about maintaining academic integrity. One way education institutions are doing both is through encouraging students to participate in open source projects.

Conclusion

  Conclusion needed

References