Page 247 - DCAP305_PRINCIPLES_OF_SOFTWARE_ENGINEERING
P. 247
Unit 12: Refactoring
(each.getMovieagetPriceCode ()){ Notes
case Movie.REGUI,AR:
thisAmount += 2;
if (each.getDaysRented()> 2) thisAmount += (each.getDaysRented()- 2)*
1.5: break; case Movie.NEW RELEASE: thisAmount += each.
getDaysRented() * break,
case Movie.CHILDRENS;
thisAmount += 1.5;
if (each.getDaysRented() > 3)
thisAmount += (each. getDaysRented() - 3) * 1.5: break;
// add frequent renter points frequentRenterPoints ++;
// add bonus for a two day new release rental if ((each.
getMovieagetPriceCode()
==Movie.NEW_RELEASE) && each.getDaysRented() > I)
frequentRenterPoints ++;
//show figures for this rental result += “\t” + each.getMovieagetTitle0+
“\t” + String.value0f (thisAmount) + “1n”;
totalAmount += thisAmount;
//add footer lines result += “Amount owed is” + String.
value0f(totalAmount) + “111”;
Result += “You earned” + String.valueof(frequentRenterPoints) + “
frequent renter points”;
return result;
};
12.1.2 Bad Smells in Code
What qualities do we expect in good software? It has been suggested that we should aim at
developing programs that are easy to read, that have all logic specified in only one place, that
allow modifications without endangering existing behaviour, and whose conditional logic is
expressed as easily as possible Programs that do not have those qualities smell bad.
The mother of all sins in programming is Duplicated Code. It is easy to see why it makes software
maintenance a nightmare: you need to make (nearly) the same modifications to many places
and it is hard to know when you are done with them. Naturally code duplication increases also
the amount of code making systems harder to understand and maintain.
Another major source for bad smells is the organization of classes and methods. They can be
too big and complex (Large class, Long Method, Long Parameter List) or too small and trivial
(Lazy Class, Data Class). Lack of classic modularity qualities of loose coupling between structures
and cohesion within them may also cause bad smells (Inappropriate Intimacy, Feature Envy,
and Data Clumps). Other sources for bad smells include using too much or too little delegation
(Message Chains, Middle Man) and using non-object-oriented control or data structures (Switch
Statements, Primitive Obsession).
If you think about the original version of the movie rental example, you may notice several bad
smells. There are instances of Data Class (Movie, Rental), Long Method (statement) and Switch
Statements just to name a few.
The term appears to have been coined by Kent Beck on WardsWiki in the
late 1990s.
LOVELY PROFESSIONAL UNIVERSITY 241