View this PageEdit this Page (locked)Attachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

CA 2

Refactoring

Refactoring is the process of rewriting existing code to improve its overall design, readability, and structure while preserving the same functionality it had before. Refactoring greatly improves the maintainability and extensibility of the program. Signs that code needs to be refactored are a lot of code that has been duplicated and very large classes. To refactor duplicated code that code should be transfered to a method and that method should be called instead of pasting the duplicated code where it is needed. To refactor large classes the design needs to be redone. The large class does too much work and should be broken down into smaller classes. Similar responsibilities should go to each smaller class making each one more specialized leading to a better object oriented design.

Unit testing is very useful when refactoring. After each small change to the code you make you can instantly tell if you broke something by performing tests that worked before you starting refactoring the code. This saves a lot of time and
also makes sure you won’t forget to test specific cases.


Tracing Code

1 + 2 3 - 4 factorial ===> -15

a := #(1 2 3 4 5) ===> #(1 2 3 4 5)

a select: [:i | i odd] ===> #(1 3 5)

b := a ===> #(1 2 3 4 5)

a := a reversed ===> #(5 4 3 2 1)

b collect: [:i | i i] ===> #(1 4 9 16 25)

a perform: #at: withArguments: #(4) ===> 2



Message Passing

while loop

while (aBooleanTest)
{
// do stuff
}

translates to…

[a boolean test] whileTrue: [do stuff].

[a boolean test] ===> receiver object
whileTrue: ===> message
[do stuff] ===> arguments

for loop

for (i = 1; i = 10; i++)
{
// do stuff
}

translates to...

1 to: 10 do: [index | do stuff].

1 ===> receiver object
to: ===> message
10 ===> argument
do: ===> message
[index | do stuff] ===> argument

if/then/else

if (aBooleanTest)
{ // do stuff
}
else
{ // do stuff
}

translates to...

[boolean test] ifTrue: [do stuff] ifFalse: [do stuff].

[boolean test] ===> reciever object
ifTrue: ===> message
[do stuff] ===> argument
ifFalse: ===> message
[do stuff] ===> argument


Link to this Page