18
Working Effectively with Legacy Code Robert Haken software architect, HAVIT, s.r.o. [email protected], @RobertHaken Microsoft MVP: Development, MCT, MCSD

ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

Working Effectively withLegacy Code

Robert Hakensoftware architect, HAVIT, [email protected], @RobertHakenMicrosoft MVP: Development, MCT, MCSD

Page 2: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

OCHUTNÁVKA

Legacy Code

Page 3: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

LegacyCode?

„Codewithouttests.“ [Michael Feathers]

„Sourcecode inherited from someone else.“

„Sourcecode inherited from an older version of the software.“

„Veškerý kód, s kterým aktuálně nejste spokojeni (nebo byste alespoň neměli být).“ [Robert Haken]

Page 4: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

LegacyCode

Non-uniformcodingstyle

Nesrozumitelný

Málo/bez testů

BadDesign

CodeSmell

Page 5: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

RefactoringMindset

LEGACY:

„Do not touchworkingcode, unlessneeded.“

Plannedrefactoring

NOW:

„Leavethecodein bettercondition thanyoufoundit.“ [TheBoy Scout Rule]

Refactoring as yougo.

Page 6: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

Předpoklady

Sdílené vlastnictví kódu

Source CodeManagement

Continuous Integration builds + runsTests

Page 7: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

RefactoringJustification

Quality

CleanCode

Professionalism

RightThing

Economics

Page 8: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

DEMO

Roslyn CodeAnalysis + baseline

Page 9: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

CodeAnalyzers

Microsoft.AnalyzerPowerPack(RoslynTeam)

System.Runtime.[CSharp.]Analyzers

StyleCopAnalyzers

SonarLint [SonarQube]

VS Perf-tip:

Tools / Options / Text Editor / C# / Advanced / Enable solution wide analysis = OFF

Page 10: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

Refactoring

Continuous, As yougoSmallstepsIDE/ToolingsupportedsafestepsComprehensionRefactoring(Rename, Extract, …)Podpořeno testy

– Pomáhají porozumět kódu– GuardConditions, Contract.Requires– Debug.Assert, InvalidOperationException, …– Unit-Tests– IntegrationTests

Page 11: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

"Good" Unit Test

• automated+ repeatable

• fully isolated

• consistent in itsresults

• runsquickly

• full control of the unit under test(alldependencies)

• relevanttomorrow

• easyto implement

• able to run it at the push of a button

• if fails=> easy to detect what was expected

Page 12: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

DEMO

Poor Man‘s TestabilityExtract dependant call to virtual method

Page 13: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

"Good" Unit Test

• automated+ repeatable

• fully isolated

• consistent in itsresults

• runsquickly

• full control of the unit under test(alldependencies)

• relevanttomorrow

• easyto implement

• able to run it at the push of a button

• if fails=> easy to detect what was expected

Page 14: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

DEMO

Mocking

Page 15: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

"Good" Unit Test

• automated+ repeatable

• fully isolated

• consistent in itsresults

• runsquickly

• full control of the unit under test(alldependencies)

• relevanttomorrow

• easyto implement

• able to run it at the push of a button

• if fails=> easy to detect what was expected

Page 16: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

DEMO

Dependency Injection

Page 17: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

Tips& Tricks

[assembly::InternalsVisibleTo(MyTestAssembly)]

[Obsolete]

TreatWarningsas Errors

Ambient Context(ale ne ServiceLocatornebo public container!)

ServiceFactories

Analyze/ AnalyzeSolutionfor CodeClones

Test / AnalyzeCodeCoverage

Page 18: ASP.NET Core Dependency Injection & Unit Testing...Oct 08, 2016  · RefactoringMindset LEGACY: „Do not touchworking code, unlessneeded.“ Plannedrefactoring NOW: „Leavethecodein

Q & A