SlideShare ist ein Scribd-Unternehmen logo
1 von 28
SESSION DETAILS
• 10-15 MINUTES OF BACKGROUND • 50-55 MINUTES OF HANDS-ON CODING
SESSION DETAILS: YOU NEED THIS STUFF
• SEED CODE IS WRITTEN IN C#
• GET IT FROM GITHUB:
https://github.com/KatasForLegacy
Code/kCSharp/archive/Step0.zip
• OR FROM ONE OF THE FLASH DRIVES:
kCSharp-Step0.zip
• ANY VERSION OF VISUAL STUDIO 2012/2013
THAT CAN RUN CONSOLE APPS AND UNIT TESTS
• AN NUNIT TEST RUNNER OR THE ABILITY TO
QUICKLY SWITCH OUT NUNIT REFERENCE FOR
MSTEST. I RECOMMEND NCRUNCH.
• WHEN WE ARE DONE CODING, I’M HOPING WE
WILL HAVE 5-10 MINUTES FOR Q&A
• THE FIRST 5 SUGGESTIONS FOR ADDITIONS OR
IMPROVEMENTS TO THIS SESSION/CODE WILL
WIN THE FLASH DRIVES
LEGACY
EXPECTATION REALITY
LEGACY
PERCEPTION - COBOL
LEGACY
PERCEPTION - COBOL REALITY – CODE W/O UNIT TESTS
DEPENDENCY
EXPECTATION REALITY
DEPENDENCY – THE HORRID TRUTH
KILLER
EXPECTATION
“
”
I’VE COMPLETELY SLAUGHTERED ALL THE
LEGACY DEPENDENCIES IN OUR
CODEBASE.
NOW I’LL ENJOY SOME LUNCH.
KILLER
EXPECTATION REALITY
PUT IT ALL TOGETHER & WHADDAYA GET
•A CODE KATA
• A CODE KATA IS AN EXERCISE IN PROGRAMMING
WHICH HELPS A PROGRAMMER HONE THEIR
SKILLS THROUGH PRACTICE AND REPETITION
• THE WORD KATA IS TAKEN FROM JAPANESE ARTS
MOST TRADITIONALLY MARTIAL ARTS
• WHY? BECAUSE HUMANS LEARN BY DOING
DEPENDENCY KATA
• DEPENDENCY KATA: CODE CAMP EDITION
• WRITTEN IN C#
• NOW WITH LESS TOOL DEPENDENCIES AND RESTRUCTURED TO WORK WITHOUT THEM
• ORIGINAL SOURCE CODE AVAILABLE ON GITHUB AT HTTPS://GITHUB.COM/DUBMUN/DEPENDENCYKATA
DEPENDENCY KATA: INITIAL STRUCTURE
DEPENDENCY KATA: FIRST THINGS FIRST
• LET’S START BY GETTING THE EXISTING TEST
RUNNING
• RUNNING THE INTEGRATION TEST SHOWS THAT
IT HANGS
• WE NEED TO BEGIN BY ABSTRACTING AND
BREAKING THE DEPENDENCY ON
Console.Readline()
• CREATE AN INTERFACE FIRST
public interface IConsoleAdapter
{
string GetInput();
}
• CREATE A CLASS THAT IMPLEMENTS THE
INTERFACE
public class ConsoleAdapter :
IConsoleAdapter
DEPENDENCY KATA: FIRST THINGS FIRST
• IMPLEMENT METHOD TO HANDLE THE DEPENDENCY
public class ConsoleAdapter :
IConsoleAdapter
{
public string GetInput()
{
return Console.ReadLine();
}
}
• CREATE NEW CONSTRUCTOR FOR DOITALL THAT ACCEPTS
ICONSOLEADAPTER AND SET A PRIVATE VARIABLE
private IConsoleAdapter _consoleAdapter;
public doItAll(IConsoleAdapter
consoleAdapter)
{
_consoleAdapter = consoleAdapter;
}
• THEN REPLACE 6 CALLS TO Console.ReadLine()
WITH _consoleAdapter.GetInput()
DEPENDENCY KATA: FIRST THINGS FIRST
• NOW WE HAVE SOME BROKEN INSTANTIATIONS
• IN THE CONSOLE APP INSTANTIATE AND PASS IN
OUR NEW HANDLER
var doItAll = new DoItAll(
new ConsoleAdapter());
• IN THE INTEGRATION TEST WE NEED TO DO
SOMETHING DIFFERENT OR OUR TEST WILL STILL
FAIL.
• CREATE A NEW IMPLEMENTATION OF
IConsoleAdaper
public class FakeConsoleAdapter :
IConsoleAdapter
{
public string GetInput()
{
return string.Empty;
}
}
DEPENDENCY KATA: FIRST THINGS FIRST
• WHEN THE TEST IS RUN WE NOW GET A
MEANINGFUL EXCEPTION. ALL OF THE
DEPENDENCIES THAT CAUSED THE HANG ARE
GONE.
• THE TEST IS NOW FAILING BECAUSE THE
PASSWORD IS EMPTY. THIS IS A MEANINGFUL
CASE BUT LET’S JUST UPDATE OUR FAKE FOR
NOW.
return “fakeInput”;
• THE TEST SHOULD BE GREEN NOW!
DEPENDENCY KATA: FIRST THINGS FIRST
• WHEN THE TEST IS RUN WE NOW GET A
MEANINGFUL EXCEPTION. ALL OF THE
DEPENDENCIES THAT CAUSED THE HANG ARE
GONE.
• THE TEST IS NOW FAILING BECAUSE THE
PASSWORD IS EMPTY. THIS IS A MEANINGFUL
CASE BUT LET’S JUST UPDATE OUR FAKE FOR
NOW.
return “fakeInput”;
• THE TEST SHOULD BE GREEN NOW!
• LET’S ADD AN ASSERT FOR GOOD MEASURE
[Test, Category("Integration")]
public void DoItAll_Does_ItAll()
{
var doItAll = new DoItAll(new
FakeConsoleAdapter());
Assert.DoesNotThrow(() =>
doItAll.Do());
}
DEPENDENCY KATA: BETTER COVERAGE
• WE DON'T HAVE COVERAGE SOME OF THE CODE
STILL AND NO QUANTIFIABLE RESULTS TO TEST
• LET’S COPY OUR EXISTING TEST AND RENAME IT
DoItAll_Fails_ToWriteToDB
• THEN CHANGE THE ASSERT
StringAssert.Contains(
"Database.SaveToLog
Exception:", doItAll.Do());
• THIS WILL FAIL TO BUILD BECAUSE OUR METHOD IS
CURRENTLY VOID. LET’S CHANGE THAT
• CHANGE DO()’S RETURN TYPE TO STRING
• ADD A RETURN STATEMENT IN 2 LOCATIONS:
• AT THE END OF THE USING STATEMENT
• AT THE END OF THE METHOD
• NOW CREATE VARIABLES TO HOLD THE MESSAGES
AT VARIOUS POINTS FOR RETURN
private const string
_passwordsDontMatch =
"The passwords don't match.";
DEPENDENCY KATA: BETTER COVERAGE
• NOW CREATE VARIABLES TO HOLD THE MESSAGES
AT VARIOUS POINTS FOR RETURN
private const string
_passwordsDontMatch =
"The passwords don't match.";
AND
var errorMessage = string.Format(
"{0} - Database.SaveToLog
Exception: rn{1}",
message, ex.Message);
• RETURN THOSE VALUES AS APPROPRIATE
• OUR NEW TEST SHOULD NOW PASS
DEPENDENCY KATA: BETTER ABSTRACTION
• DO WORK TO ABSTRACT CONSOLE COMPLETELY
COMPLETELY
• ADD A NEW METHOD STUB TO
IConsoleAdapter:
Void SetOutput(string output);
• UPDATE ConsoleAdapter IMPLEMENTATION
public void SetOutput(string output)
{
Console.WriteLine(output);
}
• UPDATE FakeConsoleAdapter
IMPLEMENTATION
public void SetOutput(string
output)
{}
• UPDATE DoItAll IMPLEMENTATION REPLACING
6 INSTANCES OF Console.WriteLine WITH
_consoleAdapter.SetOutput
• OUR TESTS SHOULD STILL PASS
DEPENDENCY KATA: REFACTOR
• DoItAll.Do() IS TRYING TO DO TOO MUCH
• EXTRACT LOGGING FUNCTIONALITY BY CREATING
A NEW INTERFACE
public interface ILogger
{
string LogMessage(string message);
}
• NOW EXTRACT THE CODE IN THE TRY/CATCH
BLOCK IN DoItAll.Do() INTO THE
IMPLEMENTATION OF
ILogger.LogMessage()
• MAKE SURE ALL PATHS RETURN A MESSAGE
• NOW UPDATE THE CONSTRUCTOR OF DoItAll
WITH AN ILogger SAND REPLACE TRY/CATCH:
message =
_logger.LogMessage(message);
DEPENDENCY KATA: REFACTOR
• CLIENT NO LONGER BUILDS BECAUSE OF THE
UPDATED CONSTRUCTOR SO UPDATE IT
• NOW CREATE A NEW FAKE FOR TESTING:
public class FakeLogger : ILogger
{
public string LogMessage(
string message)
{
return string.Empty;
}
}
• UPDATE THE DoItAll MINSTANTIATIONS IN
THE TESTS TO INCLUDE new FakeLogger()
• THE FIRST TEST PASSES BUT THE OTHER FAILS
BECAUSE IT DEPENDS ON IMPLEMENTATION-
SPECIFIC DETAILS
DEPENDENCY KATA: REFACTOR
• COPY THE SECOND TEST AND RENAME THE COPY
DoItAll_Succeeds_WithMockLogging
• UPDATE THE ASSERT:
Assert.AreEqual(
string.Empty, doItAll.Do());
• TEST WILL PASS
• LET THE FORMER TEST DEPEND ON
DatabaseLogger AND IT WILL PASS AS WELL
DEPENDENCY KATA: WHAT’S NEXT?
• THERE ARE STILL PLENTY OF THING ABOUT THIS LEGACY CODE I WOULD CHANGE
• WHAT WOULD YOU DO NEXT?
THANKS TO OUR SPONSORS!
To connect to wireless
1. Choose Uguest in the wireless list
2. Open a browser. This will open a Uof U website
3. Choose Login
Legacy Dependency Killer - Utah Code Camp 2014

Weitere ähnliche Inhalte

Was ist angesagt?

Mastering PowerShell Testing with Pester
Mastering PowerShell Testing with PesterMastering PowerShell Testing with Pester
Mastering PowerShell Testing with PesterMark Wragg
 
Karate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter ThomasKarate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter Thomasintuit_india
 
Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...
Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...
Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...DevOpsGroup
 
Setting Up CircleCI Workflows for Your Salesforce Apps
Setting Up CircleCI Workflows for Your Salesforce AppsSetting Up CircleCI Workflows for Your Salesforce Apps
Setting Up CircleCI Workflows for Your Salesforce AppsDaniel Stange
 
Karate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made SimpleKarate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made SimpleVodqaBLR
 
Vaugham Hong - Embedding JavaScript V8
Vaugham Hong - Embedding JavaScript V8Vaugham Hong - Embedding JavaScript V8
Vaugham Hong - Embedding JavaScript V8Allen Pike
 
Automate right start from API
Automate right start from APIAutomate right start from API
Automate right start from APIRoman Liubun
 
Karate, the black belt of HTTP API testing?
Karate, the black belt of HTTP API testing?Karate, the black belt of HTTP API testing?
Karate, the black belt of HTTP API testing?Bertrand Delacretaz
 
Karate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testingKarate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testingRoman Liubun
 
Ruby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 AppsRuby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 AppsNascenia IT
 
Discover the real user experience with Alyvix - Icinga Camp Milan 2019
Discover the real user experience with Alyvix - Icinga Camp Milan 2019Discover the real user experience with Alyvix - Icinga Camp Milan 2019
Discover the real user experience with Alyvix - Icinga Camp Milan 2019Icinga
 
Learnings Building Alexa Skills with PHP
Learnings Building Alexa Skills with PHPLearnings Building Alexa Skills with PHP
Learnings Building Alexa Skills with PHPRalf Eggert
 
Axon Server went RAFTing
Axon Server went RAFTingAxon Server went RAFTing
Axon Server went RAFTingJ On The Beach
 
Testing in Ballerina Language
Testing in Ballerina LanguageTesting in Ballerina Language
Testing in Ballerina LanguageLynn Langit
 
Pretenders talk at PyconUK 2012
Pretenders talk at PyconUK 2012Pretenders talk at PyconUK 2012
Pretenders talk at PyconUK 2012txels
 
WordPress Development: Tracking Your Code With Version Control
WordPress Development: Tracking Your Code With Version ControlWordPress Development: Tracking Your Code With Version Control
WordPress Development: Tracking Your Code With Version ControlSterling Hamilton
 
A Deep Dive into the W3C WebDriver Specification
A Deep Dive into the W3C WebDriver SpecificationA Deep Dive into the W3C WebDriver Specification
A Deep Dive into the W3C WebDriver SpecificationPeter Thomas
 

Was ist angesagt? (20)

Mastering PowerShell Testing with Pester
Mastering PowerShell Testing with PesterMastering PowerShell Testing with Pester
Mastering PowerShell Testing with Pester
 
Come si applica l'OCP
Come si applica l'OCPCome si applica l'OCP
Come si applica l'OCP
 
Karate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter ThomasKarate for Complex Web-Service API Testing by Peter Thomas
Karate for Complex Web-Service API Testing by Peter Thomas
 
Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...
Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...
Pester & PSScriptAnalyser - Power Test your PowerShell with PowerShell - Futu...
 
Git Tutorial (Part 2: Git Merge)
Git Tutorial (Part 2: Git Merge)Git Tutorial (Part 2: Git Merge)
Git Tutorial (Part 2: Git Merge)
 
Setting Up CircleCI Workflows for Your Salesforce Apps
Setting Up CircleCI Workflows for Your Salesforce AppsSetting Up CircleCI Workflows for Your Salesforce Apps
Setting Up CircleCI Workflows for Your Salesforce Apps
 
Karate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made SimpleKarate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made Simple
 
Vaugham Hong - Embedding JavaScript V8
Vaugham Hong - Embedding JavaScript V8Vaugham Hong - Embedding JavaScript V8
Vaugham Hong - Embedding JavaScript V8
 
Automate right start from API
Automate right start from APIAutomate right start from API
Automate right start from API
 
Karate, the black belt of HTTP API testing?
Karate, the black belt of HTTP API testing?Karate, the black belt of HTTP API testing?
Karate, the black belt of HTTP API testing?
 
Karate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testingKarate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testing
 
Ruby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 AppsRuby conf 2016 - Secrets of Testing Rails 5 Apps
Ruby conf 2016 - Secrets of Testing Rails 5 Apps
 
Discover the real user experience with Alyvix - Icinga Camp Milan 2019
Discover the real user experience with Alyvix - Icinga Camp Milan 2019Discover the real user experience with Alyvix - Icinga Camp Milan 2019
Discover the real user experience with Alyvix - Icinga Camp Milan 2019
 
Learnings Building Alexa Skills with PHP
Learnings Building Alexa Skills with PHPLearnings Building Alexa Skills with PHP
Learnings Building Alexa Skills with PHP
 
Axon Server went RAFTing
Axon Server went RAFTingAxon Server went RAFTing
Axon Server went RAFTing
 
Testing in Ballerina Language
Testing in Ballerina LanguageTesting in Ballerina Language
Testing in Ballerina Language
 
Pretenders talk at PyconUK 2012
Pretenders talk at PyconUK 2012Pretenders talk at PyconUK 2012
Pretenders talk at PyconUK 2012
 
WordPress Development: Tracking Your Code With Version Control
WordPress Development: Tracking Your Code With Version ControlWordPress Development: Tracking Your Code With Version Control
WordPress Development: Tracking Your Code With Version Control
 
TDD, BDD and mocks
TDD, BDD and mocksTDD, BDD and mocks
TDD, BDD and mocks
 
A Deep Dive into the W3C WebDriver Specification
A Deep Dive into the W3C WebDriver SpecificationA Deep Dive into the W3C WebDriver Specification
A Deep Dive into the W3C WebDriver Specification
 

Ähnlich wie Legacy Dependency Killer - Utah Code Camp 2014

Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptxStefan Oprea
 
Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!Eric Phan
 
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014FalafelSoftware
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingSteven Smith
 
What’s new in .NET
What’s new in .NETWhat’s new in .NET
What’s new in .NETDoommaker
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFXHendrik Ebbers
 
Кирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, CiklumКирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, CiklumAlina Vilk
 
Dsug 05 02-15 - ScalDI - lightweight DI in Scala
Dsug 05 02-15 - ScalDI - lightweight DI in ScalaDsug 05 02-15 - ScalDI - lightweight DI in Scala
Dsug 05 02-15 - ScalDI - lightweight DI in ScalaUgo Matrangolo
 
Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)Peter Kofler
 
Kill the mutants - A better way to test your tests
Kill the mutants - A better way to test your testsKill the mutants - A better way to test your tests
Kill the mutants - A better way to test your testsRoy van Rijn
 
Kill the mutants and test your tests - Roy van Rijn
Kill the mutants and test your tests - Roy van RijnKill the mutants and test your tests - Roy van Rijn
Kill the mutants and test your tests - Roy van RijnNLJUG
 
ASP.Net 5 and C# 6
ASP.Net 5 and C# 6ASP.Net 5 and C# 6
ASP.Net 5 and C# 6Andy Butland
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!Ortus Solutions, Corp
 
How Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran MizrahiHow Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran MizrahiRan Mizrahi
 
Chapter i c#(console application and programming)
Chapter i c#(console application and programming)Chapter i c#(console application and programming)
Chapter i c#(console application and programming)Chhom Karath
 

Ähnlich wie Legacy Dependency Killer - Utah Code Camp 2014 (20)

Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptx
 
Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!
 
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Introduction to mysql part 3
Introduction to mysql part 3Introduction to mysql part 3
Introduction to mysql part 3
 
2013-01-10 iOS testing
2013-01-10 iOS testing2013-01-10 iOS testing
2013-01-10 iOS testing
 
Tdd iPhone For Dummies
Tdd iPhone For DummiesTdd iPhone For Dummies
Tdd iPhone For Dummies
 
What’s new in .NET
What’s new in .NETWhat’s new in .NET
What’s new in .NET
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFX
 
Old code doesn't stink
Old code doesn't stinkOld code doesn't stink
Old code doesn't stink
 
Кирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, CiklumКирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, Ciklum
 
iOS testing
iOS testingiOS testing
iOS testing
 
Dsug 05 02-15 - ScalDI - lightweight DI in Scala
Dsug 05 02-15 - ScalDI - lightweight DI in ScalaDsug 05 02-15 - ScalDI - lightweight DI in Scala
Dsug 05 02-15 - ScalDI - lightweight DI in Scala
 
Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)
 
Kill the mutants - A better way to test your tests
Kill the mutants - A better way to test your testsKill the mutants - A better way to test your tests
Kill the mutants - A better way to test your tests
 
Kill the mutants and test your tests - Roy van Rijn
Kill the mutants and test your tests - Roy van RijnKill the mutants and test your tests - Roy van Rijn
Kill the mutants and test your tests - Roy van Rijn
 
ASP.Net 5 and C# 6
ASP.Net 5 and C# 6ASP.Net 5 and C# 6
ASP.Net 5 and C# 6
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
How Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran MizrahiHow Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran Mizrahi
 
Chapter i c#(console application and programming)
Chapter i c#(console application and programming)Chapter i c#(console application and programming)
Chapter i c#(console application and programming)
 

Kürzlich hochgeladen

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 

Kürzlich hochgeladen (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 

Legacy Dependency Killer - Utah Code Camp 2014

  • 1.
  • 2. SESSION DETAILS • 10-15 MINUTES OF BACKGROUND • 50-55 MINUTES OF HANDS-ON CODING
  • 3. SESSION DETAILS: YOU NEED THIS STUFF • SEED CODE IS WRITTEN IN C# • GET IT FROM GITHUB: https://github.com/KatasForLegacy Code/kCSharp/archive/Step0.zip • OR FROM ONE OF THE FLASH DRIVES: kCSharp-Step0.zip • ANY VERSION OF VISUAL STUDIO 2012/2013 THAT CAN RUN CONSOLE APPS AND UNIT TESTS • AN NUNIT TEST RUNNER OR THE ABILITY TO QUICKLY SWITCH OUT NUNIT REFERENCE FOR MSTEST. I RECOMMEND NCRUNCH. • WHEN WE ARE DONE CODING, I’M HOPING WE WILL HAVE 5-10 MINUTES FOR Q&A • THE FIRST 5 SUGGESTIONS FOR ADDITIONS OR IMPROVEMENTS TO THIS SESSION/CODE WILL WIN THE FLASH DRIVES
  • 6. LEGACY PERCEPTION - COBOL REALITY – CODE W/O UNIT TESTS
  • 8. DEPENDENCY – THE HORRID TRUTH
  • 10. “ ” I’VE COMPLETELY SLAUGHTERED ALL THE LEGACY DEPENDENCIES IN OUR CODEBASE. NOW I’LL ENJOY SOME LUNCH.
  • 12. PUT IT ALL TOGETHER & WHADDAYA GET •A CODE KATA • A CODE KATA IS AN EXERCISE IN PROGRAMMING WHICH HELPS A PROGRAMMER HONE THEIR SKILLS THROUGH PRACTICE AND REPETITION • THE WORD KATA IS TAKEN FROM JAPANESE ARTS MOST TRADITIONALLY MARTIAL ARTS • WHY? BECAUSE HUMANS LEARN BY DOING
  • 13. DEPENDENCY KATA • DEPENDENCY KATA: CODE CAMP EDITION • WRITTEN IN C# • NOW WITH LESS TOOL DEPENDENCIES AND RESTRUCTURED TO WORK WITHOUT THEM • ORIGINAL SOURCE CODE AVAILABLE ON GITHUB AT HTTPS://GITHUB.COM/DUBMUN/DEPENDENCYKATA
  • 15. DEPENDENCY KATA: FIRST THINGS FIRST • LET’S START BY GETTING THE EXISTING TEST RUNNING • RUNNING THE INTEGRATION TEST SHOWS THAT IT HANGS • WE NEED TO BEGIN BY ABSTRACTING AND BREAKING THE DEPENDENCY ON Console.Readline() • CREATE AN INTERFACE FIRST public interface IConsoleAdapter { string GetInput(); } • CREATE A CLASS THAT IMPLEMENTS THE INTERFACE public class ConsoleAdapter : IConsoleAdapter
  • 16. DEPENDENCY KATA: FIRST THINGS FIRST • IMPLEMENT METHOD TO HANDLE THE DEPENDENCY public class ConsoleAdapter : IConsoleAdapter { public string GetInput() { return Console.ReadLine(); } } • CREATE NEW CONSTRUCTOR FOR DOITALL THAT ACCEPTS ICONSOLEADAPTER AND SET A PRIVATE VARIABLE private IConsoleAdapter _consoleAdapter; public doItAll(IConsoleAdapter consoleAdapter) { _consoleAdapter = consoleAdapter; } • THEN REPLACE 6 CALLS TO Console.ReadLine() WITH _consoleAdapter.GetInput()
  • 17. DEPENDENCY KATA: FIRST THINGS FIRST • NOW WE HAVE SOME BROKEN INSTANTIATIONS • IN THE CONSOLE APP INSTANTIATE AND PASS IN OUR NEW HANDLER var doItAll = new DoItAll( new ConsoleAdapter()); • IN THE INTEGRATION TEST WE NEED TO DO SOMETHING DIFFERENT OR OUR TEST WILL STILL FAIL. • CREATE A NEW IMPLEMENTATION OF IConsoleAdaper public class FakeConsoleAdapter : IConsoleAdapter { public string GetInput() { return string.Empty; } }
  • 18. DEPENDENCY KATA: FIRST THINGS FIRST • WHEN THE TEST IS RUN WE NOW GET A MEANINGFUL EXCEPTION. ALL OF THE DEPENDENCIES THAT CAUSED THE HANG ARE GONE. • THE TEST IS NOW FAILING BECAUSE THE PASSWORD IS EMPTY. THIS IS A MEANINGFUL CASE BUT LET’S JUST UPDATE OUR FAKE FOR NOW. return “fakeInput”; • THE TEST SHOULD BE GREEN NOW!
  • 19. DEPENDENCY KATA: FIRST THINGS FIRST • WHEN THE TEST IS RUN WE NOW GET A MEANINGFUL EXCEPTION. ALL OF THE DEPENDENCIES THAT CAUSED THE HANG ARE GONE. • THE TEST IS NOW FAILING BECAUSE THE PASSWORD IS EMPTY. THIS IS A MEANINGFUL CASE BUT LET’S JUST UPDATE OUR FAKE FOR NOW. return “fakeInput”; • THE TEST SHOULD BE GREEN NOW! • LET’S ADD AN ASSERT FOR GOOD MEASURE [Test, Category("Integration")] public void DoItAll_Does_ItAll() { var doItAll = new DoItAll(new FakeConsoleAdapter()); Assert.DoesNotThrow(() => doItAll.Do()); }
  • 20. DEPENDENCY KATA: BETTER COVERAGE • WE DON'T HAVE COVERAGE SOME OF THE CODE STILL AND NO QUANTIFIABLE RESULTS TO TEST • LET’S COPY OUR EXISTING TEST AND RENAME IT DoItAll_Fails_ToWriteToDB • THEN CHANGE THE ASSERT StringAssert.Contains( "Database.SaveToLog Exception:", doItAll.Do()); • THIS WILL FAIL TO BUILD BECAUSE OUR METHOD IS CURRENTLY VOID. LET’S CHANGE THAT • CHANGE DO()’S RETURN TYPE TO STRING • ADD A RETURN STATEMENT IN 2 LOCATIONS: • AT THE END OF THE USING STATEMENT • AT THE END OF THE METHOD • NOW CREATE VARIABLES TO HOLD THE MESSAGES AT VARIOUS POINTS FOR RETURN private const string _passwordsDontMatch = "The passwords don't match.";
  • 21. DEPENDENCY KATA: BETTER COVERAGE • NOW CREATE VARIABLES TO HOLD THE MESSAGES AT VARIOUS POINTS FOR RETURN private const string _passwordsDontMatch = "The passwords don't match."; AND var errorMessage = string.Format( "{0} - Database.SaveToLog Exception: rn{1}", message, ex.Message); • RETURN THOSE VALUES AS APPROPRIATE • OUR NEW TEST SHOULD NOW PASS
  • 22. DEPENDENCY KATA: BETTER ABSTRACTION • DO WORK TO ABSTRACT CONSOLE COMPLETELY COMPLETELY • ADD A NEW METHOD STUB TO IConsoleAdapter: Void SetOutput(string output); • UPDATE ConsoleAdapter IMPLEMENTATION public void SetOutput(string output) { Console.WriteLine(output); } • UPDATE FakeConsoleAdapter IMPLEMENTATION public void SetOutput(string output) {} • UPDATE DoItAll IMPLEMENTATION REPLACING 6 INSTANCES OF Console.WriteLine WITH _consoleAdapter.SetOutput • OUR TESTS SHOULD STILL PASS
  • 23. DEPENDENCY KATA: REFACTOR • DoItAll.Do() IS TRYING TO DO TOO MUCH • EXTRACT LOGGING FUNCTIONALITY BY CREATING A NEW INTERFACE public interface ILogger { string LogMessage(string message); } • NOW EXTRACT THE CODE IN THE TRY/CATCH BLOCK IN DoItAll.Do() INTO THE IMPLEMENTATION OF ILogger.LogMessage() • MAKE SURE ALL PATHS RETURN A MESSAGE • NOW UPDATE THE CONSTRUCTOR OF DoItAll WITH AN ILogger SAND REPLACE TRY/CATCH: message = _logger.LogMessage(message);
  • 24. DEPENDENCY KATA: REFACTOR • CLIENT NO LONGER BUILDS BECAUSE OF THE UPDATED CONSTRUCTOR SO UPDATE IT • NOW CREATE A NEW FAKE FOR TESTING: public class FakeLogger : ILogger { public string LogMessage( string message) { return string.Empty; } } • UPDATE THE DoItAll MINSTANTIATIONS IN THE TESTS TO INCLUDE new FakeLogger() • THE FIRST TEST PASSES BUT THE OTHER FAILS BECAUSE IT DEPENDS ON IMPLEMENTATION- SPECIFIC DETAILS
  • 25. DEPENDENCY KATA: REFACTOR • COPY THE SECOND TEST AND RENAME THE COPY DoItAll_Succeeds_WithMockLogging • UPDATE THE ASSERT: Assert.AreEqual( string.Empty, doItAll.Do()); • TEST WILL PASS • LET THE FORMER TEST DEPEND ON DatabaseLogger AND IT WILL PASS AS WELL
  • 26. DEPENDENCY KATA: WHAT’S NEXT? • THERE ARE STILL PLENTY OF THING ABOUT THIS LEGACY CODE I WOULD CHANGE • WHAT WOULD YOU DO NEXT?
  • 27. THANKS TO OUR SPONSORS! To connect to wireless 1. Choose Uguest in the wireless list 2. Open a browser. This will open a Uof U website 3. Choose Login