CSC/ECE 517 Fall 2009/wiki2 7 cn: Difference between revisions
Line 77: | Line 77: | ||
int x, y; | int x, y; | ||
public: | public: | ||
void set_values (int,int); | |||
int area () {return (x*y);} | |||
}; | |||
void CRectangle::set_values (int a, int b) { | void CRectangle::set_values (int a, int b) { | ||
x = a; | x = a; | ||
Line 87: | Line 87: | ||
int main () { | int main () { | ||
CRectangle rect; | CRectangle rect; | ||
rect.set_values (5,4); | |||
cout << "area: " << rect.area(); //Output = 20 | |||
rect.set_values (3,4); | rect.set_values (3,4); | ||
cout << "area: " << rect.area(); | cout << "area: " << rect.area(); //Output = 12 | ||
return 0; | return 0; | ||
} | } |
Revision as of 02:07, 8 October 2009
Problem Statement
Different languages have different mechanisms for "code reuse". Assembly and low level languages have macros, C/C++ has includes, Ruby has mixins and modules, Java has packages, and there are many other concepts around reuse and extension. Here is a page that describes the various mechanisms, groups the mechanisms into logical categories, and discusses the advantages and disadvantages of the various schemes, in terms of simplicity, performance, reliability, or other factors.
Code Reuse - A Brief Overview
Code reuse, also called software reuse, is the use of existing software, or software knowledge, to build new software. Code reuse in software development is something assumed literally through re-using bits of code in more than one place. It is truly achieved through using common libraries and interfaces accessible to both the entire application and to other applications in the domain. Here are some of the features of code reuse.
Reuse is not duplication
Duplicating code is the result of a copy-paste scenario. The moment you have copy-pasted a module of code to two locations in your application you’ve just created an additional maintenance point. Also if you have coded a bug into the original code & duplicated it in multiple locations, the bug has now been instantiated in more than one place. Reuse is different from duplication. In code reuse the logic or work that a module does in multiple areas of the application can be reused from one place.The code doesn’t have to physically reside in all of those places to get reused.
Interfaces
Reuse is not always functional code that needs to be applied in more than one place. It can be obtained through the use of interfaces and generics as well. By using a solid interface design to allow any type of object with a compatible set of methods to be used in a piece of your application, you allow for everything on both sides of that interface to be easily interchangeable. Other type of interface-like methods that promote architectural reuse are generics, tag libraries, and dozens of frameworks out there that allow pieces to be easily decoupled and injected into the application.
Advantages of Code Reuse
- Reusing code saves programming time, which reduces costs.
- Sharing code can help prevent bugs by reducing the amount of code that needs to be written to perform a task. The shared code can also be tested separately from the end applications.
- Separating code into common libraries lets programmers specialize in their particular strengths. A security library, for example, can be built by security experts while a user interface which uses the library can let UI experts focus on their tasks.
- Delegation of tasks into shared modules allows offloading of some functionality onto separate systems.
Drawbacks
- Maintenance nightmare – If for any reason one to make changes in the copied section, changes have to be made ALL the copies.
- Unorganized – Reused code tends to be unorganized .It may generate create massive amounts of code which is difficult to decipher after a while.
Examples of Effective Code Reuse
- Google Chrome is an excellent example of code reuse, it uses at least 25 different software Libraries.
- Matlab programs are also a good example of code reuse where one reuses different functions from the matlab tool box.
General Methods for Code Reuse in Object Oriented Languages
The types of Code Reuse used for the Object Oriented Framework are as follows:
- Architected Reuse: The identification, development, and support of large-scale, reusable assets via enterprise architecture. Your enterprise architecture may define reusable domain components, collections of related domain/business classes that work together to support a cohesive set of responsibilities, or service domains which bundle a cohesive collection of services together.
- Pattern Reuse: The use of documented approaches to solving common problems within a specified context. With pattern reuse you’re not reusing code, instead you are reusing the thinking that goes behind the code. Patterns can be a multiple levels such as analysis, design, and architecture are the most common.
- Framework Reuse: The use of collections of classes that together implement the basic functionality of a common technical or business domain.Horizontal frameworks, such as a security framework or user interface framework such as the Java Foundation Class (JFC) library and vertical frameworks, such as a financial services framework, are common.
- Artifact Reuse: The use of previously created development artifacts such as use of cases, standards documents, domain-specific models, procedures and guidelines, and even other applications such as a commercial off the shelf (COTS) package to give one a kick start on a new project.
- Module Reuse. The use of pre-built, fully encapsulated "modules", components, services, or code libraries in the development of an application. A module is self sufficient and encapsulates only one concept. Module reuse differs from code reuse as one don’t to have access to the source code.
- Template Reuse. The practice of using a common set of layouts for key development artifacts such as documents, models, and source code within an organization.
- Code Reuse. The reuse of source code within sections of an application and potentially across multiple applications. At its best code reuse is accomplished through the sharing of common classes and/or collections of functions and procedures. At its worst code reuse is accomplished by copying and then modifying existing code causing a maintenance nightmare.
The Page now talks about reuse in different languages such as C, C++, Java & Ruby.
Code Reuse in C/C++
Functions
(Applicable for both C/C++)
Instead of writing a same function multiple times, we can call the function, thus making the code more concise, readable & modular. Another way of using functions is to write them in header files & then including the header files in the code. Later the functions written in the Header File can be called directly from the main program.
Different Types of Header Files used in C/C++ are as follows:
- assert.h, math.h, string.h, stdio.h, time.h etc
Simple Program to Illustrate C Functions:
#include <stdio.h> void print_message(void); { printf("Hello World \n"); }
Main Program
int main(void) { print_message(); return 0; }
Classes
(Applicable only for C++)
Classes define methods which process the data present in multiple objects. C++ also defines other features for classes which promote code reuse. Eg: Operator overloading helps the same function to perform different operations based on the parameters passed to it, thus allowing the use of one function to do multiple tasks.
An simple example of an class is as follows:-
// classes example #include <iostream> using namespace std; class CRectangle { int x, y; public: void set_values (int,int); int area () {return (x*y);} }; void CRectangle::set_values (int a, int b) { x = a; y = b; }
int main () { CRectangle rect; rect.set_values (5,4); cout << "area: " << rect.area(); //Output = 20 rect.set_values (3,4); cout << "area: " << rect.area(); //Output = 12 return 0; }
Code Reuse in Java
Code Reuse in Ruby
References
http://www.refactoring.com
Refactoring: Improving the Design of Existing Code by Martin Fowler.
References
http://www.refactoring.com
Refactoring: Improving the Design of Existing Code by Martin Fowler.