CS603- Software Architecture
and Design
ASSIGNEMN-2
JUNAID MALIK
BC190202640@[Link] For More Visit: [Link] JUNAID MALIK
Junaidfazal08@[Link]
(0304-1659294)
AL-JUNAID TECH INSTITUT
Question 1:
Refactor the code for the function printOwing given below using extract method technique.
Each step should be clearly presented. Also, write a final refactored version of
printOwning.
SOLUTION:
Step 1: Extract the code for printing the banner into a separate method called "printBanner"
void printOwing()
AL-JUNAID TECH INSTITUT
{
Enumeration e = _orders.elements();
double outstanding = 0.0;
// print banner
printBanner();
// calculate outstanding
while ([Link]()) {
Order each = (Order) [Link]();
outstanding += [Link]();
}
//print details
[Link] ("name:" + _name);
[Link] ("amount" + outstanding);
}
void printBanner()
{
[Link] ("*******************");
[Link] (" Customer Owes **");
[Link] ("********************");
}
Step 2: Extract the code for calculating the outstanding into a separate method called
"calculateOutstanding"
void printOwing()
{
Enumeration e = _orders.elements();
double outstanding = 0.0;
// print banner
printBanner();
// calculate outstanding
outstanding = calculateOutstanding(e);
//print details
[Link] ("name:" + _name);
[Link] ("amount" + outstanding);
}
double calculateOutstanding(Enumeration e)
{
double result = 0.0;
while ([Link]()) {
Order each = (Order) [Link]();
result += [Link]();
}
return result;
AL-JUNAID TECH INSTITUT
}
Step 3: Extract the code for printing the details into a separate method called "printDetails"
void printOwing()
{
Enumeration e = _orders.elements();
double outstanding = 0.0;
// print banner
printBanner();
// calculate outstanding
outstanding = calculateOutstanding(e);
//print details
printDetails(outstanding);
}
void printDetails(double outstanding)
{
[Link] ("name:" + _name);
[Link] ("amount" + outstanding);
}
Final Refactored Version:
void printOwing()
{
Enumeration e = _orders.elements();
double outstanding = 0.0;
// print banner
printBanner();
// calculate outstanding
outstanding = calculateOutstanding(e);
//print details
printDetails(outstanding);
}
void printBanner()
{
[Link] ("**************************");
[Link] ("***** Customer Owes ******");
[Link] ("**************************");
}
double calculateOutstanding(Enumeration e)
{
double result = 0.0;
AL-JUNAID TECH INSTITUT
while ([Link]()) {
Order each = (Order) [Link]();
result += [Link]();
}
return result;
}
void printDetails(double outstanding)
{
[Link] ("name:" + _name);
[Link] ("amount" + outstanding);
Question 2:
Typical examples of code smells include the following:
Duplicate code
Dead code
Long methods
Long parameter list
Comments
Data clumps
Lazy Class
Large Class
Refused Bequest
Shotgun Surgery
Feature Envy etc.
Given the code snippets below, Identify the code smells.
SOLUTION:
1 public Person(string id, string firstName, string lastName, string address, string postcode,
string city, string country)()
Answer Long parameter list
2 int sum_a = 0;
for (int i = 0; i < 4; i++) sum_a += array_a[i];
int average_a = sum_a / 4;
AL-JUNAID TECH INSTITUT
int sum_b = 0;
for (int i = 0; i < 4; i++) sum_b += array_b[i];
int average_b = sum_b / 4;
Answer Duplicate code
3 class Strength
{
int value;
}
class Person
{
int health;
int intelligence;
Strength strength;
}
Answer Data clumps
It looks like Strength class is used only in Person class and doesn't have any
additional functionality