45
Module 1. Introducing Course: Refactoring

Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Embed Size (px)

Citation preview

Page 1: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Module 1. Introducing

Course: Refactoring

Page 2: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Overview

Definition

Principles

Building Tests

Bad Smells in Code

Page 3: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Definition

Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure.

It is a disciplined way to clean up code that minimizes the chances of introducing bugs.

In essence when you refactor you are improving the design of the code after it has been written.

Page 4: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Defining Refactoring

Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.

Refactor (verb): to restructure software by applying a series of refactorings without changing its observable behavior.

Page 5: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Individuals and interactions over processes and tools

Working software over comprehensive documentation

Customer collaboration over contract negotiation documentation

Responding to change over following a plan

http://www.agilemanifesto.org/

Page 6: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Building on the Foundations Laid by Others

Ward Cunningham

Kent Beck

Ralph Johnson

Bill Opdyke

John Brant

Don Roberts

Martin Fowler

Page 7: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Demo: IDE

Conduct Refactoring with IDE

Page 8: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Lesson: Principles in Refactoring

Why? When? What?

Indirection and Refactoring

Problems with Refactoring

Refactoring and Design and Performance

The Environment for Refactoring

Two approaches

Thinking in Refactoring

Page 9: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Why Should You Refactor?

Refactoring Improves the Design of Software

Refactoring Makes Software Easier to Understand

Refactoring Helps You Find Bugs

Refactoring Helps You Program Faster

Refactoring Makes coding less annoying

Page 10: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

When Should You Refactor?

Refactor When You Add Function

Refactor When You Need to Fix a Bug

Refactor As You Do a Code Review

The Rule of Three (Don Roberts)

Page 11: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

What Do I Tell My Manager?

Good Manager

Bad Manager

Design Debt (Ward Cunningham)

“Not fix what ain't broken“

Ask: "How many months in a row do you not pay down your debt?" (show your credit card)

Page 12: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Indirection and Refactoring

To enable sharing of logic

To explain intention and implementation separately

To isolate change

To encode conditional logic

Page 13: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Problems with Refactoring

Hard to see that greatly improves your productivity

Databases

Changing Interfaces

Design Changes That Are Difficult to Refactor

When Shouldn't You Refactor?

Page 14: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Refactoring and Design and Performance

Upfront design vs Extreme Programming

Only refactoring does work, it is not the most efficient

A greater movement toward simplicity of design

How difficult is it going to be to refactor a simple solution into the flexible solution?

Refactoring and Performance

“With design I can think very fast, but my thinking is full of little holes”

Alistair Cockburn

Page 15: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Breakthrough

Page 16: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

The Environment for Refactoring

Team or Partner

Tests

Testing Framework

CRC Cards or UML Sketches

Configuration and Version Control

Sophisticated IDE

Page 17: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Two approaches

Code-First Refactoring

Test-First Refactoring

Combination method

Page 18: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Thinking in Refactoring

november(20, 2005) java.util.Calendar c = java.util.Calendar.getInstance();

c.set(2005, java.util.Calendar.NOVEMBER, 20);

c.getTime();

Many Eyes

Human-Readable Code

Page 19: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Rules

Keeping It Clean

Small Steps

Design Debt

«Any fool can write code that a computer can understand. Good programmers write code that humans can understand»

Martin Fowler, Refactoring: Improving the Design of Existing Code

Page 20: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Exercise: Inside a Refactoring

Create Extract Method Refactoring

Pick a refactoring and identify a place where the approach builds in small steps even though larger steps could work.

Page 21: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Exercise Discussion

Small Steps

Simple Design

passes all tests communication no duplication fewest classes and methods

Page 22: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Lesson: Building Tests

The Value of Self-testing Code

The xUnit Testing Framework

Test Location

Tips

Page 23: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

The Value of Self-testing Code

The essential precondition is time

Greatly speeds your programming

Fully automatic

A powerful bug detector

Prime technique for Refactoring

Type: inline, friend, reflection

Page 24: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

JUnit

import junit.framework.TestCase;

public class MyTest extends TestCase {

public void test () {

public class MyMain {

public static void main (String[] args) {

new junit.textui.TestRunner().doRun(new MatcherTest());

Page 25: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

NUnit

using NUnit.Framework;

[TestFixture]

public class DatabaseFixture

{

[SetUp]

public void CreateDatabaseObjects() { ... }

[Test]

public void ReadOneObject() { ... }

}

Page 26: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

The NUnit Testing Framework

Page 27: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Test Location

Within Classes

Another Solution + public interfaces

Another Solution + Reflection

Page 28: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Tips

Run your tests frequently

Testing should be risk driven

It is better to write and run incomplete tests than not to run complete tests

Think of the boundary conditions

Use exceptions

Testing can't catch all bugs, but most ones

Page 29: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Demo: jUnit test

Demonstration

Page 30: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Lesson: Bad Smells in Code

Definition

Simple Design

Design Principles

Smells Catalog

Database Smells

Architectures Smells

Page 31: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Bad Smells in Code

Smells (especially code smells) are warning signs about potential problems in code.

If it stinks, change it.

Grandma Beck, discussing

child-rearing philosophy

Page 32: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Simple Design

Runs all the tests.

Has no duplicated logic. Be wary of hidden duplication like parallel class hierarchies.

States every intention important to the programmers.

Has the fewest possible classes and methods.—Beck, Extreme Programming Explained, p. 57

Page 33: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Design Principles

DRY : Don’t Repeat Yourself

SCP : Speaking Code

OCP : Open Closed

LSP : Liskov Substitution

DIP : Dependency Inversion

ISP : Interface Segregation

REP : Reuse/Release Equivalency

CRP : Common Reuse

CCP : Common Closure

ADP : Acyclic Dependencies

SDP : Stable Dependencies

SAP : Stable Abstractions

TDA : Tell, Don’t Ask

SOC : Separation Of Concerns

Stefan Rock. Refactoring in Large Software. 2006

Page 34: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Some depends

Methods <= 30 code lines

A Class <= 30 methods

A Package <= 30 classes

Subsystems <= 30 packages

System <= 30 subsystems

3-10 Layers

Methods <= 10 code lines

A Class <= 10 methods

A Package <= 10 classes

Subsystems <= 10 packages

System <= 10 subsystems

3-10 Layers

Page 35: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Smells CatalogW

ith

inNames

Type Embedded in Name (including Hungarian)Uncommunicative NameInconsistent Names

Dead CodeSpeculative Generality

Unnecessary Complexity

Magic NumberDuplicated CodeAlternative Classes with Different Interfaces

Duplication Conditional Logic

Null CheckComplicated Boolean ExpressionSpecial CaseSimulated Inheritance (Switch Statement)

Bet

wee

n

Data

Primitive ObsessionData ClassData ClumpTemporary Field

Refused BequestInappropriate Intimacy (Subclass Form)Lazy ClassSimulated Inheritance (Switch Statement)Parallel Inheritance HierarchiesCombinatorial Explosion

Inheritance

Feature EnvyInappropriate Intimacy (General Form)Message ChainsMiddle Man

Responsibility Accommodating Change Library Classes

Divergent ChangeShotgun SurgeryParallel Inheritance HierarchiesCombinatorial Explosion

Incomplete Library Class

Database Smells

Architecture Smells

Dependency Graphs Inheritance Hierarchies Packages LayersSubsystems

Cla

sses

Measured

CommentsLong MethodLarge ClassLong Parameter List

Page 36: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Test Smells (Code)

Obscure test

Conditional test logic

Hard to test code

Test code duplicated

Test Logic in Production

…Visit “Test-Driven

Development Course”

Page 37: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Database Smells

Multipurpose column.

Multipurpose table.

Redundant data.

Tables with too many columns.

Tables with too many rows.

"Smart" columns.

Fear of change.

Visit “Database Refactoring Course”

Page 38: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Architectures Smells

visible dependencygraphs

cycle betweenclasses

unused classes

tree-likedependency graphs

parallel inherencehierarchies

inheritance hierarchy

without polymorphicassignments

inherence hierarchies

to deep

like-listinherence

hierarchies

type query

subclasses withoutredifinitions

unused packages

too smallpackages

cycle betweenpackages

too largepackages

packagesunclearly named

packagestoo deep or nesting

unbalanced

subsystem overgeneralized subsystem API

bypassed

subsystem too small

cycle betweensubsystems

subsystem APItoo large

too manysubsystems

subsystemtoo large

no subsystems

inheritance betweenprotocol oriented

layers

upward referencesin layers

too many layers

no layersstrict layers violated

reference betweenvertically separate

layers

Dependency graphs

Inheritance hierarchies

Packages

Subsystems

Layers

Page 39: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

SA4J: Dependencies

Page 40: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

NDepend

Page 41: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Software visualization

Sotograph

JDepend / NDepend

Eclipse Metrics Plugin , JDepend4Eclipse

Dr. Freud

SonarJ

ClassCycle

SA4J

http://en.wikipedia.org/wiki/Software_visualization

Page 42: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Management Smells

Absentee Manager

All you have is a Hammer

Golden Child

Leader not Manager / Manager not Leader

Metric Abuse

Road to Nowhere

Page 43: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Smell - its name

Symptoms — cues that help you spot it

Causes — notes on how it might happen

What to do — possible refactorings

Payoff — the ways your code will improve

Contraindications — when not to fix it

Page 44: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Exercise: Real Project

Review real project

Smells list

Page 45: Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code

Review

Definition

Principles

Building Tests

Bad Smells in Code