your code is collapsing- how refactoring can save your ass 2012 03-01 - confoo

36
Your Code is Collapsing How Refactoring Can Save Your Thursday, 1 March, 12

Upload: olivier-gourment

Post on 06-May-2015

1.788 views

Category:

Technology


0 download

DESCRIPTION

One of the most overlooked Agile techniques may also be the most useful one. In the context of software product development, Refactoring (improving the design of existing code) is absolutely mandatory to continuously deliver quality code in reasonable timeframes. Teams that do not practice continuous refactoring see their quality and velocity go down with time. Developers should understand from this presentation: - How to explain the topic to managers? - How to make sure refactoring is done, and regularly? Managers should take away from this session: - What is refactoring? - What to do to maintain a good velocity as products are being built?

TRANSCRIPT

Page 1: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Your Code is CollapsingHow Refactoring Can Save Your

Thursday, 1 March, 12

Page 2: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

DISCLAIMER

Refactoring can generate endless discussions. What we will share

today are original thoughts on the topic, in order to provide ideas to address your own situation (every

situation is different)

Thursday, 1 March, 12

Page 3: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Lack of Refactoring

Thursday, 1 March, 12

Page 4: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

GrandPerspectiveThursday, 1 March, 12

Page 5: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

DbVisualizerThursday, 1 March, 12

Page 6: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

StatSVN

Developers modify code much more often than they write new code

Thursday, 1 March, 12

Page 7: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Our brain’s processing power is extremely overrated

Thursday, 1 March, 12

Page 8: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Thursday, 1 March, 12

Page 9: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Thursday, 1 March, 12

Page 10: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Lack of Refactoring causes:

Slower DeliveryMore DefectsLower Morale

It’s a vicious circle!

Thursday, 1 March, 12

Page 11: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

“If it ain’t broke, don’t fix it”

Thursday, 1 March, 12

Page 12: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

When reusing the source code ends up costing more than

starting from scratchThursday, 1 March, 12

Page 13: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

You say: “technical debt”

I ask: “credit history?”

Thursday, 1 March, 12

Page 14: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Solution

The solution involves change management techniques. It requires education, awareness, incentives, making metrics visible, reports and alerts automatically generated (CI). Different levels of awareness will cause frustrations.You will often need external help (coaching.).

Thursday, 1 March, 12

Page 15: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Solution

1. NEW CODE BASE: continuous refactoring (and automated tests). Ideally, TDD (Red, Green, Refactor)

2. EXISTING CODE BASE: cost-benefit analysis (how long is the product to be supported?)

Thursday, 1 March, 12

Page 16: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Raising Awareness

Thursday, 1 March, 12

Page 17: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Explain refactoring to non-programmers with

writing recipes

Thursday, 1 March, 12

Page 18: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Thursday, 1 March, 12

Page 19: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Thursday, 1 March, 12

Page 20: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

How much time does it take to spot the error?

Thursday, 1 March, 12

Page 21: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Preheat oven to 425F and place rack in center.Wash the cherries, remove stems and pits.In blender, process flour, salt, eggs, sugar,and vanilla extract until completely smooth.Let batter rest.In large 9-inch nonstick skillet melt butter over medium heat.Add cherries, and cook for 3 minutes. Add the remaining sugar and cook for 2 more minutes.Pour the batter over the cherries and bake for about 20 minutes or until the clafoutis is puffed, set, and golden brown around the edges.

1/2 cup all purpose flour1/4 teaspoon

salt2 large eggs2 tbsp sugar3/4 cup milk1/2 teaspoon

vanilla extract

1 lb fresh cherries

1 tbsp butter2 tbsp sugar

Thursday, 1 March, 12

Page 22: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Solution

Thursday, 1 March, 12

Page 23: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Preheat oven to 425F and place rack in center.Wash the cherries, remove stems and pits.In blender, process flour, salt, eggs, sugar, milk,and vanilla extract until completely smooth.Let batter rest.In large 9-inch nonstick skillet melt butter over medium heat.Add cherries, and cook for 3 minutes. Add the remaining sugar and cook for 2 more minutes.Pour the batter over the cherries and bake for about 20 minutes or until the clafoutis is puffed, set, and golden brown around the edges.

1/2 cup all purpose flour1/4 teaspoon

salt2 large eggs2 tbsp sugar3/4 cup milk1/2 teaspoon

vanilla extract

1 lb fresh cherries

1 tbsp butter2 tbsp sugar

Thursday, 1 March, 12

Page 24: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Let’s Refactor

Thursday, 1 March, 12

Page 25: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Preheat oven to 425F and place rack in center.Wash the cherries, remove stems and pits.

In blender, process flour, salt, eggs, sugar, milk,and vanilla extract until completely smooth.

Let batter rest.

In large 9-inch nonstick skillet melt butter over medium heat.

Add cherries, and cook for 3 minutes. Add the remaining sugar and cook for 2 more minutes.Pour the batter over the cherries and bake for about 20 minutes or until the clafoutis is puffed, set, and golden brown around the edges.

1 lb fresh cherries

1/2 cup flour1/4 teaspoon salt

2 large eggs2 tbsp sugar3/4 cup milk1/2 teaspoonvanilla extract

1 tbsp butter

2 tbsp sugar

Thursday, 1 March, 12

Page 26: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

And now?

Thursday, 1 March, 12

Page 27: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Preheat oven to 425F and place rack in center.Wash the cherries, remove stems and pits.

In blender, process flour, salt, eggs, sugar,and vanilla extract until completely smooth.

Let batter rest.

In large 9-inch nonstick skillet melt butter over medium heat.

Add cherries, and cook for 3 minutes. Add the remaining sugar and cook for 2 more minutes.Pour the batter over the cherries and bake for about 20 minutes or until the clafoutis is puffed, set, and golden brown around the edges.

1 lb fresh cherries

1/2 cup flour1/4 teaspoon salt

2 large eggs2 tbsp sugar3/4 cup milk1/2 teaspoonvanilla extract

1 tbsp butter

2 tbsp sugar

Thursday, 1 March, 12

Page 28: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Source code is invisible (by default)

Problems with source code are even less visible

Thursday, 1 March, 12

Page 29: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Junior developers don’t see the point. Senior developers

don’t care.. Project Managers, well...

Thursday, 1 March, 12

Page 30: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

MAKE IT VISIBLE!

Even Better:

MAKE IT STINK!

Thursday, 1 March, 12

Page 31: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Code Smellshttp://www.codinghorror.com/blog/2006/05/code-smells.html

Thursday, 1 March, 12

Page 32: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Ideas

1. Sonar (SonarSource.org) can display, for each build: code coverage, complexity metrics, rules compliance...2. Use in conjunction with Continuous Integration (Jenkins). Configure to alert when non compliant3. Name Code Quality Managers4. Regular Retrospectives to discuss rules

Thursday, 1 March, 12

Page 33: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

SonarThursday, 1 March, 12

Page 34: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Ideas5. Provide realistic estimates which include time to refactor (include in your definition of DONE)6. Multiple little refactorings are better than large ones7. Rule of thumb: dedicate 10% of your time to refactor (before each commit)8. Metrics: watch TRENDs, NOT absolute numbers

Thursday, 1 March, 12

Page 35: Your Code is Collapsing- How Refactoring Can Save Your Ass  2012 03-01 - confoo

Books

• Refactoring by Martin Fowler (1999)

• Refactoring to Patterns by Joshua Kerievsky (2004)

• Code Complete 2 by Steve McConnell (2004)

• Clean Code by Robert C. Martin (2008)

Thursday, 1 March, 12