CSC/ECE 517 Fall 2010/ch2 S20 CR: Difference between revisions
Line 26: | Line 26: | ||
} | } | ||
</pre> | </pre> | ||
We can use Java Packages as follows | We can use Java Packages as follows.If we want to use Date class we can write the code as follows. | ||
<pre> | <pre> | ||
java.util.Date today = new java.util.Date(); | java.util.Date today = new java.util.Date(); |
Revision as of 05:51, 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.
For example 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 use Java Packages as follows.If we want to use Date class we can write the code as follows.
java.util.Date today = new java.util.Date(); System.out.println("Todays Date is "+today);
We can also create Macro in C/C++ and reuse it.Following example clearly explains it.
#include "stdio.h" #define Cubeof(x) x*x*x //Here we define a MACRO that calculates the cube of number int main() { int input = 6; printf("\nCubeof(x)=%i",Cubeof(input)); //Here we reuse the MACRO. return 0; }