Refactoring Practice
Refactoring Practice
j7h2b
liujenn
Jenny Liu
just doing it for credit, not really looking at the questions right now (by selecting this, you will get credit, but you will not
see the questions -- come back later for real practice!)
actually practicing
looking at the questions, but not entering anything for real right now
When you have to make a related (but not identical) change in multiple places within the same class, what code smell (or
symptom) must be evident in the code (not what could be evident, but must be present):
magic number
one class doing the work of two
method in the wrong class
delocalised change
duplicated code
shotgun surgery
divergent change
complicated conditionals
switch statement/type conditionals Comments
Method too long
Your method uses no local fields but does use fields from other classes
Your method is very long and doesn’t call any local methods
Your method violates the law of demeter but also uses local fields
Your method violates the law of demeter and uses no local fields
Your method mostly uses fields accessed from one of the parameters or one of the local fields of the class
Your method is only called from outside the class, and is never used locally
void printOwing() {
printBanner();
//print details
System.out.println("name: " + name);
System.out.println("amount: " + getOutstanding());
}
You have been told you should apply the extract method refactoring (extracting one or more methods) from the printOwing
method to make it clearer.
Assuming you extracted lines of code (groups or individuals) in the order they appear in the method, which lines of code do you
extract into the first method you extract:
void printOwing() {
printBanner();
//print details (this line is optionally extracted)
printOwing
printBanner
printDetails
printName
printOutstanding
Assuming you extracted lines of code (groups or individuals) in the order they appear in the method, which lines of code do you
extract into the second method you extract:
void printOwing() {
printBanner();
printOwing
printBanner
printDetails
printName
printOutstanding
printBanner()
"name"
name
"amount"
getOutstanding()
No it doesn't
There is no second method
magic number
duplicated code
shotgun surgery
divergent change
complicated conditionals
After you have finished refactoring to fix the predominant code smell(s) (so, not including how the code might look in
intermediate stages of refactoring), you would:
Make three new classes: Some new class (called whatever you want), Auditor, Major
Make two new methods, each of which holds the bodies of the ifstatements. These methods would be in the Student class.
The code inside the methods would be precisely as is currently in the ifstatements.
Make two new methods, each of which holds the bodies of the ifstatements. One of these methods would be in the Auditor
class, another would be in the Major class. The code inside the methods would be modified no temporary object variable would
be needed, and only local calls would be made.
Make two new methods, each of which holds the bodies of the ifstatements. These methods would be moved to another
class, and the code inside the method bodies would need to be parameterised with a Student object.
Make a constant to hold the strings “AUDIT” and “MAJOR” so that they are not misspelled elsewhere in the code
Powered by Qualtrics A