CSC/ECE 517 Fall 2009/wiki2 7 co: Difference between revisions
No edit summary |
No edit summary |
||
Line 19: | Line 19: | ||
=== Includes === | === 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: | 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" | #include "time.h" | ||
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: | |||
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 === | === 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 [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 === | ||
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 == | == Code Reuse through Extension == |
Revision as of 23:21, 9 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
blah
Inheritance
blah
Polymorphism
blah
Generics
blah
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
- Brooks, Jr., F.P. The Mythical Man-Month. Boston, MA: Addison Wesley Longman, Inc., 1995.
- | Cut-and-Paste Programming