0% found this document useful (0 votes)
2 views

Week 10

Uploaded by

Munazza Tabassum
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week 10

Uploaded by

Munazza Tabassum
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

SE602

SOFTWARE RE-ENGINEERING
Making Refactoring Safe
How do you make
refactoring safe?
1
Use Refactoring “Patterns”

Fowler’s book assigns “names” to refactorings in the


same way that the GoF’s book assigned names to
patterns
How do you make
refactoring safe?
2
Test Constantly

This ties into the extreme programming paradigm, you write


tests before you write code, after you refactor code, you run
the tests and make sure they all still pass
If a test fails, the refactoring broke something, but you know about it right away
and can fix the problem before you move on
TWO HATS
TWO HATS
Kent's metaphor of the two hats. His notion is that when you're programming, you
can operate in one of two modes: refactoring and adding function.
• When you're wearing the refactoring hat, every change you make preserves
observable behavior, keeps the tests green, and allows you to make many small
changes without going near a debugger.
• When you add function, however, things are more open ended as you will add
tests and break existing tests. The adding function hat is more stressful and
riskier, so it's nice to wear the refactoring hat as much as possible.
When Should You Refactor?
The Rule of Three

TIP
“Three strikes and you refactor”.
1
Refactor when you add function

The most common time to refactor is when you want to add a new
feature

• Refactoring helps in understanding the code that needs modification


• The other driver of refactoring here is a design that hinders adding a feature
easily
PREPARATORY REFACTORING

"make the change easy, then make the easy change"


PREPARATORY REFACTORING

It’s like I want to go 100 miles East but instead of just traipsing through
the woods, I’m going to drive 20 miles north to the highway and then I’m
going to go 100 miles East at three times the speed I could have if I just
went straight there. When people are pushing you to just go straight
there, sometimes you need to say, “Wait, I need to check the map and
find the quickest route.” The preparatory refactoring does that for me.
2
Refactor when you need to fix a bug

• In fixing bugs much of the use of refactoring comes from making code
more understandable
• if you do get a bug report, it's a sign you need refactoring
• the code was not clear enough for you to see there was a bug
3
Refactor as you do a code review

• Some organizations do regular code reviews

• Refactoring helps in reviewing someone else's code

• Refactoring helps the code review have more concrete results

You might also like