CSC/ECE 517 Fall 2010/ch2 S20 CR: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 11: Line 11:
==== Using Software Library ====
==== Using Software Library ====
It is the most common example of code reuse.Suppose we want certain manipulating functions like date,time,string or functions related to interfacing of an external database with our program  we use software libraries.These libraries are either created by us or these comes as external library in the languages.The code reuse in various languages are.<br>
It is the most common example of code reuse.Suppose we want certain manipulating functions like date,time,string or functions related to interfacing of an external database with our program  we use software libraries.These libraries are either created by us or these comes as external library in the languages.The code reuse in various languages are.<br>
* '''C/C++'''
* '''Examples of code reuse in C/C++'''
If we want to use all string functionality we write our code as following we can include string header as follows.
If we want to use all string functionality we write our code as following we can include string header as follows.
<pre>
<pre>
Line 37: Line 37:
}
}
</pre>
</pre>
*'''Java'''
*'''Examples of code reuse in Java'''
We can use Packages as follows.If we want to use Date class we can write the code as follows.Packages is a way of collecting and organizing related files for a particular functionality and usability.Generally a package contains classes,interfaces etc.
We can use Packages as follows.If we want to use Date class we can write the code as follows.Packages is a way of collecting and organizing related files for a particular functionality and usability.Generally a package contains classes,interfaces etc.
The example of a '''class''' inside a package.
The example of a '''class''' inside a package.
Line 66: Line 66:
}
}
</pre>
</pre>
*'''Ruby'''
*'''Examples of code reuse in Ruby'''
In Ruby the code reuse is mainly done through usage of modules and mixins.
In Ruby the code reuse is mainly done through usage of modules and mixins.
The example of Module is  
The example of Module is  

Revision as of 13:46, 6 October 2010

Introduction to Code Reuse

Advantages of Code Reuse

Disadvantages of Code Reuse

Categories of Code Reuse

Internal Code Reuse

Suppose a team needs to start a new project.In order to start the project the team can use all possible reusable components that it has built earlier instead of again re-creating them.Such usage of reusable components is referred as Internal Code Reuse.Few such examples of Internal Code reuse are :

Simple Copy and Paste of the Code

Here we simply try to find the source code that perform the required functionality.We copy and paste the source code into our required code area.But in this case people sometimes try to copy from an external source which is discouraged method and it leads to plagiarism.

Using Software Library

It is the most common example of code reuse.Suppose we want certain manipulating functions like date,time,string or functions related to interfacing of an external database with our program we use software libraries.These libraries are either created by us or these comes as external library in the languages.The code reuse in various languages are.

  • Examples of code reuse in C/C++

If we want to use all string functionality we write our code as following we can include string header as follows.

#include "stdio.h"
#include "string.h"  //Inclusion of string header file 

int main()
{
 if (strcmp("ncsucsc517","ncsucsc517")== 0) //strcmp is function provided by string library which compares two string.
 {
   printf("the string are same");
 }
return 0;
}

We can also create Macro and reuse it.Following example clearly explains it.

#include "stdio.h"
#define Cubeof(x) x*x*x  //Here at first we define a Macro that calculates the cube of a number
int main()
{
  int input = 6;
  printf("\nCubeof(x)=%i",Cubeof(input)); //Here we reuse the Macro "Cubeof"
  return 0;
}
  • Examples of code reuse in Java

We can use Packages as follows.If we want to use Date class we can write the code as follows.Packages is a way of collecting and organizing related files for a particular functionality and usability.Generally a package contains classes,interfaces etc. The example of a class inside a package.

 java.util.Date today = new java.util.Date();//java.util is a package which contains Date Class
 System.out.println("Todays Date is "+today);

The example of an Interface inside a package.

interface powerOf
{
	public void squareOf(int number);
	
}
public class calculator implements powerOf
{
	public void squareOf(int number)
	{
		int result;
		result = number * number;
		System.out.println("The square of the number is"+result);
	}
	public static void main(String args[])
	{
		calculator obj = new calculator();
		obj.squareOf(20);
	}
}
  • Examples of code reuse in Ruby

In Ruby the code reuse is mainly done through usage of modules and mixins. The example of Module is

module Power
  NUMBER = 5
  def Power.Square(x)
    return x*x
  end
  def Power.Cube(x)
    return x*x*x
  end
end

cube = Power.Cube(Power::NUMBER)
puts cube

The example of Mixin is

class Calculator
  include Power        //We have included the module created above         
  def square_of_number number
    puts Power.Square(number) //Reusing the code
  end
end

obj = Calculator.new
obj.square_of_number 20

External Code reuse