78
Improving the Quality of Existing Software Steve Smith Ardalis Services @ardalis | ardalis.com

Improving the Quality of Existing Software - DevIntersection April 2016

Embed Size (px)

Citation preview

Page 1: Improving the Quality of Existing Software - DevIntersection April 2016

Improving the Quality of Existing Software

Steve SmithArdalis Services

@ardalis | ardalis.com

Page 2: Improving the Quality of Existing Software - DevIntersection April 2016

Tweet Away

• Live Tweeting and Photos are encouraged• Questions and Feedback are welcome• Use #DevIntersection and/or #ImproveSoftware• Or #DevIntersectionImprovingTheQualityOfExistingSoftware

Page 3: Improving the Quality of Existing Software - DevIntersection April 2016

Pluralsight

I have some 1-month free passes; see me after if you’d like one

Page 4: Improving the Quality of Existing Software - DevIntersection April 2016

Software Rots

Page 5: Improving the Quality of Existing Software - DevIntersection April 2016
Page 6: Improving the Quality of Existing Software - DevIntersection April 2016

Technical Debt

• Low quality code and shortcuts in our applications

• Technical debt, like real debt, has direct cost to pay off as well as interest

Page 7: Improving the Quality of Existing Software - DevIntersection April 2016

http://www.jimhighsmith.com/

Page 8: Improving the Quality of Existing Software - DevIntersection April 2016

Preventive Maintenance

• Refactoring• Eliminate Duplication• Simplify Design

• Automated Tests• Verify correctness• Avoid regressions• Increase Confidence

Page 9: Improving the Quality of Existing Software - DevIntersection April 2016

When should you refactor?

• While delivering value

Page 10: Improving the Quality of Existing Software - DevIntersection April 2016

“”

You don’t need permission to practice basic hygiene when you write software.

http://ardalis.com/when-should-you-refactor/

Make cleaning up your code something you do as part of writing code.

Page 11: Improving the Quality of Existing Software - DevIntersection April 2016
Page 12: Improving the Quality of Existing Software - DevIntersection April 2016

Refactoring Should Not Change System Behavior

Page 13: Improving the Quality of Existing Software - DevIntersection April 2016

The Refactoring Process

• Verify existing behavior• Write Characterization Tests if none exist• Find test points• Break dependencies

• Apply Refactoring• Confirm existing behavior is preserved

Page 14: Improving the Quality of Existing Software - DevIntersection April 2016

Characterization Tests

Process1. Write a test you know will fail2. Use the output of the failing test to determine the existing

behavior to assert3. Update the test with the new value/behavior4. Run the test again – it should now pass

Page 15: Improving the Quality of Existing Software - DevIntersection April 2016
Page 16: Improving the Quality of Existing Software - DevIntersection April 2016
Page 17: Improving the Quality of Existing Software - DevIntersection April 2016

S O L I D

Principleshttp://flickr.com/photos/kevinkemmerer/

2772526725/

Page 18: Improving the Quality of Existing Software - DevIntersection April 2016

Principles of OO Design

0. Don’t Repeat Yourself (DRY)

1.Single Responsibility2.Open/Closed3.Liskov Substitution4.Interface Segregation5.Dependency Inversion

Page 19: Improving the Quality of Existing Software - DevIntersection April 2016
Page 20: Improving the Quality of Existing Software - DevIntersection April 2016
Page 21: Improving the Quality of Existing Software - DevIntersection April 2016

Don’t RepeatRepeat Yourself

• Duplication in logic calls for abstraction

• Duplication in process calls for automation

Page 22: Improving the Quality of Existing Software - DevIntersection April 2016

Common Refactorings

• Replace Magic Number/String• Parameterize Method• Pull Up Field• Pull Up Method• Replace Conditional With Polymorphism• Introduce Method

Page 23: Improving the Quality of Existing Software - DevIntersection April 2016

Common Source of Repetition: Role Checks

if(user.IsInRole(“Admins”){ // allow access to resource}

// favor privileges over role checks// ardalis.com/Favor-Privileges-over-Role-Checks

var priv = new ContentPrivilege(user, article);if(priv.CanEdit()){ // allow access}

Page 24: Improving the Quality of Existing Software - DevIntersection April 2016

Visual Studio Code Clones

• Find similar blocks of code in your projects/solution

• Can detect matches that are similar but vary in small ways (like variable names)

• Available in VS2015 Premium and Ultimate

Page 25: Improving the Quality of Existing Software - DevIntersection April 2016
Page 26: Improving the Quality of Existing Software - DevIntersection April 2016

Single Responsibility Principle

The Single Responsibility Principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class.

Wikipedia

There should never be more than one reason for a class to change.Robert C. “Uncle Bob” Martin

Page 27: Improving the Quality of Existing Software - DevIntersection April 2016

What is a responsibility?

“My CustomerManager class is only responsible for anything to do with a Customer. That follows SRP, right?”

Page 28: Improving the Quality of Existing Software - DevIntersection April 2016

Examples of Responsibilities• Persistence• Validation• Notification• Error Handling• Logging• Class Selection / Construction• Formatting• Parsing• Mapping

Page 29: Improving the Quality of Existing Software - DevIntersection April 2016

Dependency and Coupling

• Excessive coupling makes changing legacy software difficult

• Breaking apart responsibilities and dependencies is a large part of working with existing code

Page 30: Improving the Quality of Existing Software - DevIntersection April 2016

Common Refactorings

• Extract Class• Extract Method• Move Method

Page 31: Improving the Quality of Existing Software - DevIntersection April 2016

Heuristics and Code Smells

• Visual Studio Metrics

Page 32: Improving the Quality of Existing Software - DevIntersection April 2016

Cyclomatic Complexity

https://en.wikipedia.org/wiki/Cyclomatic_complexity

Page 33: Improving the Quality of Existing Software - DevIntersection April 2016
Page 34: Improving the Quality of Existing Software - DevIntersection April 2016

Code Smell: Regions

?More on Regions: http://ardalis.com/regional-differences

Page 35: Improving the Quality of Existing Software - DevIntersection April 2016
Page 36: Improving the Quality of Existing Software - DevIntersection April 2016

Open / Closed Principle

The Open / Closed Principle states that software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.

Wikipedia

Page 37: Improving the Quality of Existing Software - DevIntersection April 2016

Open / Closed Principle

Open to ExtensionNew behavior can be added in the future

Closed to ModificationChanges to source or binary code are not required

Dr. Bertrand Meyer originated the OCP term in his 1988 book, Object Oriented Software Construction

Page 38: Improving the Quality of Existing Software - DevIntersection April 2016

Common Refactorings

• Extract Interface / Apply Strategy Pattern• Parameterize Method• Form Template Method

Page 39: Improving the Quality of Existing Software - DevIntersection April 2016

OCP Fail

Page 40: Improving the Quality of Existing Software - DevIntersection April 2016

OCP OK

Page 41: Improving the Quality of Existing Software - DevIntersection April 2016

OCP Failpublic bool IsSpecialCustomer(Customer c){ if(c.Country == “US” && c.Balance < 50) return false; if(c.Country == “DE” && c.Balance < 25) return false; if(c.Country == “UK” && c.Balance < 35) return false; if(c.Country == “FR” && c.Balance < 27) return false; if(c.Country == “BG” && c.Balance < 29) return false;

if(c.Age < 18 || c.Age > 65) return false; if(c.Income < 50000 && c.Age < 30) return false; return true;}

Page 42: Improving the Quality of Existing Software - DevIntersection April 2016

OCP OK

private IEnumerable<ICustomerRule> _rules;

public bool IsSpecialCustomer(Customer customer){ foreach(var rule in _rules) { if(rule.Evaluate(customer) == false) return false; } return true;}

Page 43: Improving the Quality of Existing Software - DevIntersection April 2016
Page 44: Improving the Quality of Existing Software - DevIntersection April 2016

Liskov Substitution Principle

The Liskov Substitution Principle states that Subtypes must be substitutable for their base types.

Agile Principles, Patterns, and Practices in C#

Named for Barbara Liskov, who first described the principle in 1988.

Page 45: Improving the Quality of Existing Software - DevIntersection April 2016

Common Refactorings

• Collapse Hierarchy• Pull Up / Push Down Field• Pull Up / Push Down Method

Page 46: Improving the Quality of Existing Software - DevIntersection April 2016

Liskov Substitution Fail

foreach(var employee in employees){ if(employee is Manager) { Helpers.PrintManager(employee as Manager); break; } Helpers.PrintEmployee(employee);}

Page 47: Improving the Quality of Existing Software - DevIntersection April 2016

Liskov Substitution OK

foreach(var employee in employees){ employee.Print(); // or Helpers.PrintEmployee(employee);}

Page 48: Improving the Quality of Existing Software - DevIntersection April 2016

Nulls Break Polymorphism

foreach(var employee in employees){ if(employee == null) { // print not found message break; } Helpers.PrintEmployee(employee);} http://ardalis.com/nulls-break-

polymorphism

Page 49: Improving the Quality of Existing Software - DevIntersection April 2016
Page 50: Improving the Quality of Existing Software - DevIntersection April 2016

Interface Segregation Principle

The Interface Segregation Principle states that Clients should not be forced to depend on methods they do not use.

Agile Principles, Patterns, and Practices in C#

Corollary:Prefer small, cohesive interfaces to “fat” interfaces

Page 51: Improving the Quality of Existing Software - DevIntersection April 2016

Common Refactorings

• Extract Interface

Page 52: Improving the Quality of Existing Software - DevIntersection April 2016

Keep Interfaces Small and Focused

Page 53: Improving the Quality of Existing Software - DevIntersection April 2016

Membership Provider

Page 54: Improving the Quality of Existing Software - DevIntersection April 2016

ISP Fail (sometimes)

public IRepository<T>{ T GetById(int id); IEnumerable<T> List(); void Create(T item); void Update(T item); void Delete(T item);}

Page 55: Improving the Quality of Existing Software - DevIntersection April 2016

ISP OK (i.e. to support CQRS)public IRepository<T> : IReadRepository<T>, IWriteRepository<T>{ }public IReadRepository<T>{ T GetById(int id); IEnumerable<T> List();}public IWriteRepository<T> void Create(T item); void Update(T item); void Delete(T item);}

Existing implementations of IRepository<T> are unaffected by pulling out smaller interfaces!No existing code breaks!

Page 56: Improving the Quality of Existing Software - DevIntersection April 2016
Page 57: Improving the Quality of Existing Software - DevIntersection April 2016

Dependency Inversion Principle

High-level modules should not depend on low-level modules. Both should depend on abstractions.

Abstractions should not depend on details. Details should depend on abstractions.

Agile Principles, Patterns, and Practices in C#

Page 58: Improving the Quality of Existing Software - DevIntersection April 2016

Dependency Inversion Principle

• Depend on Abstractions• Interfaces, not concrete types

• Inject Dependencies into Classes

• Structure Solution so Dependencies Flow Toward Core• Onion Architecture (a.k.a. Ports and Adapters, a.k.a. Hexagonal Architecture)

Page 59: Improving the Quality of Existing Software - DevIntersection April 2016

Application Layers

Page 60: Improving the Quality of Existing Software - DevIntersection April 2016

Data Access Evolution

No separation of concerns:

Data access logic baked directly into UIASP.NET Data Source ControlsClassic ASP scripts

Data access logic in UI layer via codebehindASP.NET Page_Load eventASP.NET Button_Click event

User Interface

Database

Compile Time

Runtime

Page 61: Improving the Quality of Existing Software - DevIntersection April 2016

Data Access : Helper Classes

Calls to data made through a utility

Example: Data Access Application Block (SqlHelper)

Logic may still live in UI layer

Or a Business Logic Layer may make calls to a Data Access Layer which might then call the helper

User Interface

Database

Compile Time

Runtime

Helper Class

Page 62: Improving the Quality of Existing Software - DevIntersection April 2016

What’s Missing? Abstraction!

No way to abstract away data access

Tight coupling

Leads to Big Ball of Mud system

Solution:Depend on interfaces, not

concrete implementationsWhat should we call such

interfaces? Repositories!

User Interface

Database

Compile Time

Runtime

CoreIFooRepository

InfrastructureSqlFooRepository

Page 63: Improving the Quality of Existing Software - DevIntersection April 2016

DIP Architecture (aka Ports and Adapters)

Page 64: Improving the Quality of Existing Software - DevIntersection April 2016

Common Dependencies• Framework• Third Party Libraries• Database• File System• Email• Web Services• System Resources (Clock)• Configuration• The new Keyword• Static methods• Thread.Sleep• Random

See also responsibilities:• Persistence• Validation• Notification• Error Handling• Logging• Class Selection /

Construction• Formatting• Parsing• Mapping

Page 65: Improving the Quality of Existing Software - DevIntersection April 2016

Common Refactorings

• Extract Class• Extract Interface / Apply Strategy Pattern• Extract Method• Introduce Service Locator / Container

Page 66: Improving the Quality of Existing Software - DevIntersection April 2016

DIP Fail

Page 67: Improving the Quality of Existing Software - DevIntersection April 2016

Hidden Dependencies

• Checkout Depends on an available SMTP server, but the class doesn’t reflect this

• Follow the Explicit Dependencies Principle• http://deviq.com/explicit-dependencies-principle/

Page 68: Improving the Quality of Existing Software - DevIntersection April 2016

Some Improvement (Façade)

Page 69: Improving the Quality of Existing Software - DevIntersection April 2016

DIP OK (Strategy Pattern / DI)

Page 70: Improving the Quality of Existing Software - DevIntersection April 2016

DIP OK (Strategy Pattern / DI)

Page 71: Improving the Quality of Existing Software - DevIntersection April 2016

Improving Quality Across the Industry

Page 72: Improving the Quality of Existing Software - DevIntersection April 2016

Self-Improvement and Quality

• How fast can you produce:• Code you believe to be of high quality• Code that maybe gets the job done, but you believe to be of low

quality

• Which one can you produce more quickly?• Why?• How can we develop our skills and our tools so that

building quality is natural and easier than not doing so?

Page 73: Improving the Quality of Existing Software - DevIntersection April 2016

Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 90

2

4

6

8

10

12

14

16

User Stories Completed

High Quality Low Quality

Page 74: Improving the Quality of Existing Software - DevIntersection April 2016

Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 90

2

4

6

8

10

12

14

16

18

20

User Stories Completed

High Quality Low Quality

Page 75: Improving the Quality of Existing Software - DevIntersection April 2016

Summary•Maintain / Improve Application Code• Follow DRY/SOLID Principles•Use Characterization Tests to “fix”

behavior•Apply Common Refactorings•Re-run Tests After (and during)

Refactorings•Be Explicit About Class Dependencies• Train and Practice to Write Better Code

Faster

Page 76: Improving the Quality of Existing Software - DevIntersection April 2016

Learn More• DevIQ.com – Take Pride in Your Code• Ardalis.com• @ardalis• Pluralsight:• SOLID Principles of OO Design• N-Tier Architecture in C#• Refactoring Fundamentals• Domain-Driven Design Fundamentals• Design Pattern Library• Pair Programming

Page 77: Improving the Quality of Existing Software - DevIntersection April 2016

Books

Refactoring http://amzn.to/110tscA

Refactoring to Patterns http://amzn.to/Vq5Rj2

Working Effectively with Legacy Code http://amzn.to/VFFYbn

Code Complete http://amzn.to/Vq5YLv

Clean Code http://amzn.to/YjUDI0

Page 78: Improving the Quality of Existing Software - DevIntersection April 2016

© DEVintersection. All rights reserved.http://www.DEVintersection.com

Please use Event Board to fill out a session evaluation.

Questions?

Thank you!