Chapter 9: Constant Refactoring After Programming (If It Ain t Broke, Fix It Anyway)


Overview

Refactor

(Sing to the tune of She Loves You by The Beatles)

Refactor, yeah, yeah, yeah
Refactor, yeah, yeah, yeah
Refactor, yeah, yeah, yeah, yeah

You say you wrote some code
And it smells pretty bad
Your pair programming buddy
Is lookin kinda sad

You just refactor
And then you re having fun
Refactor
Don t worry bout getting done

You ve coded it ten times
But no one seems to mind
It passes unit tests
But it s never been designed

So just refactor
Cause it s all about havin fun
Refactor
Cause software s never done, oooooooooooooh!

Refactor, yeah, yeah, yeah.
Cause if your code smells clean your client won t get mad

Doesn t matter when it s done
Or even what it does
Cause you re doin XP
And it s the latest buzz

So you refactor
Cause you know you will be glad
Refactor
And you never will be sad, oooooooooooooh!

Refactor, yeah, yeah, yeah
Refactor, yeah, yeah, yeah
Cause if your code smells clean your client won t get mad
Cause if your code smells clean your client won t get mad
Cause if your code smells clean your client won t get mad

Yeah, yeah, yeah!
Yeah, yeah, yeah!
Yeah, yeah, yeah, yeah!

Refactoring in XP is based around the premise that you probably won t get all the requirements right up front and you ll probably pick an imperfect design (we discuss each of these premises in Chapter 10 and Chapter 12, respectively). Therefore, why not make up the design as you go along?

Refactoring, as a technique for cleaning up poorly structured code, certainly has its place in our toolkit of useful techniques. However, refactoring, as it s used in XP, is essentially the place where you catch all the design errors that you probably would have found in design if you hadn t skipped over designing up front in the first place.

In this chapter we examine refactoring in XP from a variety of angles.

start sidebar
SATIRE WARNING
There s No TIME to Write Down Requirements [1]

Suddenly the white rabbit, whom Alice had encountered earlier, dashed up and began shouting at her. There s no TIME to write down REQUIREMENTS, said the rabbit, iterating furiously. And what s more, users never know what they want!

Just have them tell you a story, and CODE IT, he said. They change their minds several times each morning, anyway ”the only way to keep up is to refactor the code faster than they can change their minds. Why, we can go through five iterations in the time it takes a typical user to change his mind, you see if we can t!

Refactoring? asked Alice. I think I ve heard of that, somewhere. It s the latest thing, said the rabbit. Everybody s doing it. Design s dead, you know. With enough refactoring, you don t need design. The design figures itself out from the code. Ask the Hatter! And off he rushed again.

You Might As Well Say the Code Is the Design

Alice, not knowing what else to do, and feeling somewhat shaky again after hearing about designs figuring themselves out from code, decided to follow the rabbit for a while. On they went, the rabbit pausing every few feet to iterate around in a circle a few times. Eventually the rabbit, rushing ahead at top speed, pulled far enough ahead that Alice couldn t see him anymore.

Alice kept walking, and after a time she came to a clearing where she saw the rabbit, along with a large mouse and a little man wearing a big hat, all working furiously over a table and some chairs. When Alice walked up, all the legs from the table and chairs were sitting in a big pile on the ground. Alice watched as the Hatter, the rabbit, and the dormouse each grabbed four legs from the pile and screwed them into a chair. The problem was that the legs were of different lengths, and Alice watched in fascination as they each finished assembling their chair , turned it over, and sat down. The chairs often tipped over, what with the legs being of different lengths and all, and when this happened they yelled out in unison , Failed the unit test! ; flipped the chairs back over; and began unscrewing the legs and tossing them back onto the pile.

What kind of game are you playing? asked Alice. It s not a game, we re refactoring the furniture, replied the Hatter. Why don t you read the documentation for assembling the chairs? asked Alice. It s sure to specify which legs should go on which chairs.

It s oral documentation, said the Hatter. Oh, said Alice. You mean you don t have any. No, said the Hatter. Written documentation is for cowards. We can refactor very quickly, so we can be brave enough to let the design figure itself out.

Oh, yes. The rabbit said I should ask you about that, said Alice. How can designs figure themselves out from code?

You poor, ignorant child, said the Hatter, in quite a condescending tone. The code is the design, don t you see? Perhaps you d better let the Duchess explain it to you, he said, and resumed refactoring the chairs.

end sidebar
 

[1] In XP, code smell is shorthand for a coder s intuition of when code is in need of improvement.




Extreme Programming Refactored
Extreme Programming Refactored: The Case Against XP
ISBN: 1590590961
EAN: 2147483647
Year: 2003
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net