SlideShare a Scribd company logo
1 of 25
Why certain code is hard to
test?
Tech Talk Series


Kosala Nuwan Perera
Tech Lead
@kosalanuwan
Why certain code is really hard to test?
Why certain code is really hard to test?
In order to test;
 First, we need to instantiate an OBJECT, Duh!

Seems obvious;
But we put lot of code in the constructor.
public class Document
{
    private readonly string html;
    public Document(string url)
    {
        HtmlClient client = new HtmlClient();
        html = client.Get(url);
    }
}




                                          How would you test this?
public class Document
{
    private readonly string html;
    public Document(HtmlClient client, string url)
    {
        html = client.Get(url);
    }                                       Document doesn’t care
}
                                                about client


       Passing but
       not saving
         locally
public class Document                           public class Printer
{                                               {
    private readonly string html;                   public void Print(Document html)
    public Document(string html)                    {
    {                                                   // do the printing work here.
        this.html = html;                           }
    }                                           }
}

public class DocumentFactory
{
    private readonly HtmlClient client;
    public DocumentFactory(HtmlClient client)
    {
        this.client = client;
    }

    public Document Build(string url)
    {
        return new Document(client.Get(url));
    }
}
Why certain code is really hard to test?
Testing is all about;
Instantiating small portion of application, poke in, and assert.

But, the fact is;
Hard to construct objects where we start skipping tests.
Service locator lies!
What it tries to solve?
a.k.a. Context, Repository, Locator, Manager, Environment…
Get rid of “new” keyword.
Inversion of Control.
Better than having a Singleton, coz singletons are hard to test.

But, this is where Service Locator lies;
Encourages ambiguous API.
Don’t know what to mock in tests.
Violates Law of Demeter.
public class House
{
    public House(Locator locator)
    {
        // What needs to be mocked?
    }
}



                                      How do we mock this
                                        Service Locator?
         This is
        where API
          lies
public class House
{
    public House(Door d, Roof r, Window w)
    {
        // What needs to be mocked?
    }
}




                                     We know what are needed
Service locator lies!
Mixing responsibilities;
Object lookup and Object creation (Factory).

Not only that;
Anything that depends on Service Locator now depends on
everything else.
That’s where it violates Law of Demeter.
Law of Demeter explained!
How do you pay for your Goods?
Give the money?
Or Give the wallet?
public class Goods
{
    AccountsReceivable ar;

    public void Purchase(Customer c)
    {
        Money m = c.GetWallet().GetMoney();
        ar.RecordSale(this, m);
    }
}



                                      Law of Demeter: Violated
public class Goods
{
    AccountsReceivable ar;

    public void Purchase(Money m)
    {
        ar.RecordSale(this, m);
    }
}

[TestClass]                               Law of Demeter: Obeyed
public class GoodsTest
{
    [TestMethod]
    public Purchase_The_Right_Way()
    {
        AccountsReceivable ar = new MockAR();
        Goods g = new Goods(ar);
        g.Purchase(new Money(24, LKR));
        assertEquels(24, ar.GetSales());
    }
}
Law of Demeter explained!
How do you pay for your Goods?
Give the money?
Give the wallet?

Law of Demeter says;
Ask only objects you directly need.
a.GetX().GetY()… is Writing Shy Code.. Transitive Navigation, a
Chaos.
serviceLocator.GetService() is breaking LOD.
DI is what saves you from all of these.
Myths about DI!
DI makes refactoring really hard!
If a child object needs a new parameter, then I have to pass it
through all of its parents.

But;
Parent is not making a Child, it only asks for Child.
So when child ask for a dependency, Parent is not aware.
public class HouseFactory
{
    public House Build()
    {
        Color color = Color.Red;
        DoorKnob knob = new DoorKnob(color);
        Window window = new SmallWindow();
        Door door = new Door(knob, window);

        return new House(door);
    }
}
                                    House doesn’t even know
                                    that Door has a DoorKnob
Myths about DI!
DI makes refactoring really hard!
If a child object needs a new parameter, then I have to pass it
through all of its parents.

But;
Parent is not making a Child, it only asks for Child.
So when child ask for a dependency, Parent is not aware.

So;
This makes easier to construct objects.
Few rules
When Constructing objects;
Only inject objects what are …
       Lifetime is >= injectee

Rest of the objects …
       Pass it via Methods
Few rules
Paranoid programming;
Null checking as preconditions.
public class House
{
    public House(Door door)
    {
        Preconditions.CheckNotNull(door);
        this.door = door;
    }

    public void Paint(Color color)
    {
        // Paint the house, nothing to do with Doors.
    }
}
                                            Doesn’t allow me to pass
    [TestMethod]
    void House_Painted_In_Red()
                                                     Null 
    {
        House house = new House(null);
        house.Paint(Color.Red);
        assertEquels(Color.Red, house.GetColor());
    }
Few rules
Paranoid programming;
Null checking as preconditions.

What prefer is;
An application with tests that proves it works.
Rather than having preconditions.
You’re API with External calls, this might not applicable.

What you really saying is;
You don’t trust yourself.
Few rules
In production code;
 I don’t want to pass in Null.
 Pass me a Null Value Object rather.
 Avoid null checks.
 Make sure you get real objects which you can call methods.

But, in Tests;
The opposite of Production code.
In summary
We have Two Piles;
Objects – Business logic, this is where you write code, most bugs
are found.
New Keywords – Providers, Factories, Builders etc. responsibility
to construct the objects, DI.

Separating Two Piles;
Tests become small and easy to write.
When wiring is wrong; It often blows up.
System failures; Bug in a class or missing unit test.
Follow me!
t: @kosalanuwan
w: kosalanuwan.tumblr.com
e: kpe@exilesoft.com

More Related Content

What's hot

JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJWORKS powered by Ordina
 
Javascript Templating
Javascript TemplatingJavascript Templating
Javascript Templatingbcruhl
 
Secrets of JavaScript Libraries
Secrets of JavaScript LibrariesSecrets of JavaScript Libraries
Secrets of JavaScript Librariesjeresig
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency InjectionIndrit Selimi
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic courseTran Khoa
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptFu Cheng
 
Advanced JavaScript - Internship Presentation - Week6
Advanced JavaScript - Internship Presentation - Week6Advanced JavaScript - Internship Presentation - Week6
Advanced JavaScript - Internship Presentation - Week6Devang Garach
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic JavascriptBunlong Van
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript BasicsMindfire Solutions
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy CodeRowan Merewood
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptForziatech
 
Kotlin a problem solver - gdd extended pune
Kotlin   a problem solver - gdd extended puneKotlin   a problem solver - gdd extended pune
Kotlin a problem solver - gdd extended puneHardik Trivedi
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesDoris Chen
 

What's hot (20)

Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
 
Javascript Templating
Javascript TemplatingJavascript Templating
Javascript Templating
 
Secrets of JavaScript Libraries
Secrets of JavaScript LibrariesSecrets of JavaScript Libraries
Secrets of JavaScript Libraries
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic course
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Advanced JavaScript - Internship Presentation - Week6
Advanced JavaScript - Internship Presentation - Week6Advanced JavaScript - Internship Presentation - Week6
Advanced JavaScript - Internship Presentation - Week6
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic Javascript
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
 
J query
J queryJ query
J query
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
 
Kotlin a problem solver - gdd extended pune
Kotlin   a problem solver - gdd extended puneKotlin   a problem solver - gdd extended pune
Kotlin a problem solver - gdd extended pune
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best Practices
 

Similar to Why certain code is hard to test?

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
 
Unit Testing for Great Justice
Unit Testing for Great JusticeUnit Testing for Great Justice
Unit Testing for Great JusticeDomenic Denicola
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Workhorse Computing
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctlyDror Helper
 
Rise of the Machines: PHP and IoT - ZendCon 2017
Rise of the Machines: PHP and IoT - ZendCon 2017Rise of the Machines: PHP and IoT - ZendCon 2017
Rise of the Machines: PHP and IoT - ZendCon 2017Colin O'Dell
 
.NET Database Toolkit
.NET Database Toolkit.NET Database Toolkit
.NET Database Toolkitwlscaudill
 
Navigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupDror Helper
 
Refactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartRefactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartGabriele Lana
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityGeorgePeterBanyard
 
Scrum Gathering 2012 Shanghai_工程实践与技术卓越分会场:how to write unit test for new cod...
Scrum Gathering 2012 Shanghai_工程实践与技术卓越分会场:how to write unit test for new cod...Scrum Gathering 2012 Shanghai_工程实践与技术卓越分会场:how to write unit test for new cod...
Scrum Gathering 2012 Shanghai_工程实践与技术卓越分会场:how to write unit test for new cod...LetAgileFly
 
Responsible DI: Ditch the Frameworks
Responsible DI: Ditch the FrameworksResponsible DI: Ditch the Frameworks
Responsible DI: Ditch the Frameworkskenbot
 
Practicing Red, Green, Refactor!
Practicing Red, Green, Refactor!Practicing Red, Green, Refactor!
Practicing Red, Green, Refactor!XPDays
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingSteven Smith
 
TDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with AndroidTDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with AndroidCodemotion
 

Similar to Why certain code is hard to test? (20)

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
 
Javascript Best Practices
Javascript Best PracticesJavascript Best Practices
Javascript Best Practices
 
Unit Testing for Great Justice
Unit Testing for Great JusticeUnit Testing for Great Justice
Unit Testing for Great Justice
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
 
Rise of the Machines: PHP and IoT - ZendCon 2017
Rise of the Machines: PHP and IoT - ZendCon 2017Rise of the Machines: PHP and IoT - ZendCon 2017
Rise of the Machines: PHP and IoT - ZendCon 2017
 
.NET Database Toolkit
.NET Database Toolkit.NET Database Toolkit
.NET Database Toolkit
 
Navigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet Soup
 
DDDesign Challenges
DDDesign ChallengesDDDesign Challenges
DDDesign Challenges
 
Refactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartRefactoring In Tdd The Missing Part
Refactoring In Tdd The Missing Part
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing Insanity
 
Scrum Gathering 2012 Shanghai_工程实践与技术卓越分会场:how to write unit test for new cod...
Scrum Gathering 2012 Shanghai_工程实践与技术卓越分会场:how to write unit test for new cod...Scrum Gathering 2012 Shanghai_工程实践与技术卓越分会场:how to write unit test for new cod...
Scrum Gathering 2012 Shanghai_工程实践与技术卓越分会场:how to write unit test for new cod...
 
Responsible DI: Ditch the Frameworks
Responsible DI: Ditch the FrameworksResponsible DI: Ditch the Frameworks
Responsible DI: Ditch the Frameworks
 
Practicing Red, Green, Refactor!
Practicing Red, Green, Refactor!Practicing Red, Green, Refactor!
Practicing Red, Green, Refactor!
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
API Design
API DesignAPI Design
API Design
 
Getting Started With Testing
Getting Started With TestingGetting Started With Testing
Getting Started With Testing
 
Good Coding Practices with JavaScript
Good Coding Practices with JavaScriptGood Coding Practices with JavaScript
Good Coding Practices with JavaScript
 
TDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with AndroidTDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with Android
 

More from Kosala Nuwan Perera

Developer Best Practices - The secret sauce for coding modern software
Developer Best Practices - The secret sauce for coding modern softwareDeveloper Best Practices - The secret sauce for coding modern software
Developer Best Practices - The secret sauce for coding modern softwareKosala Nuwan Perera
 
Lean UX and Design winning mobile apps
Lean UX and Design winning mobile appsLean UX and Design winning mobile apps
Lean UX and Design winning mobile appsKosala Nuwan Perera
 
Native vs Web vs Hybrid Mobile Application Development
Native vs Web vs Hybrid Mobile Application DevelopmentNative vs Web vs Hybrid Mobile Application Development
Native vs Web vs Hybrid Mobile Application DevelopmentKosala Nuwan Perera
 
Automating mobile usability heuristics
Automating mobile usability heuristicsAutomating mobile usability heuristics
Automating mobile usability heuristicsKosala Nuwan Perera
 
Crafting features that matter - UX from a Modern Analyst's perspective
Crafting features that matter - UX from a Modern Analyst's perspectiveCrafting features that matter - UX from a Modern Analyst's perspective
Crafting features that matter - UX from a Modern Analyst's perspectiveKosala Nuwan Perera
 
Web-centric application architectures
Web-centric application architecturesWeb-centric application architectures
Web-centric application architecturesKosala Nuwan Perera
 
Emphasis on sprint delivery model with Feature Crews
Emphasis on sprint delivery model with Feature CrewsEmphasis on sprint delivery model with Feature Crews
Emphasis on sprint delivery model with Feature CrewsKosala Nuwan Perera
 
Design stunning user experience with expression blend
Design stunning user experience with expression blendDesign stunning user experience with expression blend
Design stunning user experience with expression blendKosala Nuwan Perera
 

More from Kosala Nuwan Perera (11)

Developer Best Practices - The secret sauce for coding modern software
Developer Best Practices - The secret sauce for coding modern softwareDeveloper Best Practices - The secret sauce for coding modern software
Developer Best Practices - The secret sauce for coding modern software
 
Lean UX and Design winning mobile apps
Lean UX and Design winning mobile appsLean UX and Design winning mobile apps
Lean UX and Design winning mobile apps
 
Native vs Web vs Hybrid Mobile Application Development
Native vs Web vs Hybrid Mobile Application DevelopmentNative vs Web vs Hybrid Mobile Application Development
Native vs Web vs Hybrid Mobile Application Development
 
Automating mobile usability heuristics
Automating mobile usability heuristicsAutomating mobile usability heuristics
Automating mobile usability heuristics
 
Fake It Before Make It
Fake It Before Make ItFake It Before Make It
Fake It Before Make It
 
Crafting features that matter - UX from a Modern Analyst's perspective
Crafting features that matter - UX from a Modern Analyst's perspectiveCrafting features that matter - UX from a Modern Analyst's perspective
Crafting features that matter - UX from a Modern Analyst's perspective
 
Code Quality for a Fresh Start
Code Quality for a Fresh StartCode Quality for a Fresh Start
Code Quality for a Fresh Start
 
Web-centric application architectures
Web-centric application architecturesWeb-centric application architectures
Web-centric application architectures
 
Emphasis on sprint delivery model with Feature Crews
Emphasis on sprint delivery model with Feature CrewsEmphasis on sprint delivery model with Feature Crews
Emphasis on sprint delivery model with Feature Crews
 
Design stunning user experience with expression blend
Design stunning user experience with expression blendDesign stunning user experience with expression blend
Design stunning user experience with expression blend
 
If you dont appear on google
If you dont appear on googleIf you dont appear on google
If you dont appear on google
 

Recently uploaded

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Why certain code is hard to test?

  • 1. Why certain code is hard to test? Tech Talk Series Kosala Nuwan Perera Tech Lead @kosalanuwan
  • 2. Why certain code is really hard to test?
  • 3. Why certain code is really hard to test? In order to test; First, we need to instantiate an OBJECT, Duh! Seems obvious; But we put lot of code in the constructor.
  • 4. public class Document { private readonly string html; public Document(string url) { HtmlClient client = new HtmlClient(); html = client.Get(url); } } How would you test this?
  • 5. public class Document { private readonly string html; public Document(HtmlClient client, string url) { html = client.Get(url); } Document doesn’t care } about client Passing but not saving locally
  • 6. public class Document public class Printer { { private readonly string html; public void Print(Document html) public Document(string html) { { // do the printing work here. this.html = html; } } } } public class DocumentFactory { private readonly HtmlClient client; public DocumentFactory(HtmlClient client) { this.client = client; } public Document Build(string url) { return new Document(client.Get(url)); } }
  • 7. Why certain code is really hard to test? Testing is all about; Instantiating small portion of application, poke in, and assert. But, the fact is; Hard to construct objects where we start skipping tests.
  • 8. Service locator lies! What it tries to solve? a.k.a. Context, Repository, Locator, Manager, Environment… Get rid of “new” keyword. Inversion of Control. Better than having a Singleton, coz singletons are hard to test. But, this is where Service Locator lies; Encourages ambiguous API. Don’t know what to mock in tests. Violates Law of Demeter.
  • 9. public class House { public House(Locator locator) { // What needs to be mocked? } } How do we mock this Service Locator? This is where API lies
  • 10. public class House { public House(Door d, Roof r, Window w) { // What needs to be mocked? } } We know what are needed
  • 11. Service locator lies! Mixing responsibilities; Object lookup and Object creation (Factory). Not only that; Anything that depends on Service Locator now depends on everything else. That’s where it violates Law of Demeter.
  • 12. Law of Demeter explained! How do you pay for your Goods? Give the money? Or Give the wallet?
  • 13. public class Goods { AccountsReceivable ar; public void Purchase(Customer c) { Money m = c.GetWallet().GetMoney(); ar.RecordSale(this, m); } } Law of Demeter: Violated
  • 14. public class Goods { AccountsReceivable ar; public void Purchase(Money m) { ar.RecordSale(this, m); } } [TestClass] Law of Demeter: Obeyed public class GoodsTest { [TestMethod] public Purchase_The_Right_Way() { AccountsReceivable ar = new MockAR(); Goods g = new Goods(ar); g.Purchase(new Money(24, LKR)); assertEquels(24, ar.GetSales()); } }
  • 15. Law of Demeter explained! How do you pay for your Goods? Give the money? Give the wallet? Law of Demeter says; Ask only objects you directly need. a.GetX().GetY()… is Writing Shy Code.. Transitive Navigation, a Chaos. serviceLocator.GetService() is breaking LOD. DI is what saves you from all of these.
  • 16. Myths about DI! DI makes refactoring really hard! If a child object needs a new parameter, then I have to pass it through all of its parents. But; Parent is not making a Child, it only asks for Child. So when child ask for a dependency, Parent is not aware.
  • 17. public class HouseFactory { public House Build() { Color color = Color.Red; DoorKnob knob = new DoorKnob(color); Window window = new SmallWindow(); Door door = new Door(knob, window); return new House(door); } } House doesn’t even know that Door has a DoorKnob
  • 18. Myths about DI! DI makes refactoring really hard! If a child object needs a new parameter, then I have to pass it through all of its parents. But; Parent is not making a Child, it only asks for Child. So when child ask for a dependency, Parent is not aware. So; This makes easier to construct objects.
  • 19. Few rules When Constructing objects; Only inject objects what are … Lifetime is >= injectee Rest of the objects … Pass it via Methods
  • 20. Few rules Paranoid programming; Null checking as preconditions.
  • 21. public class House { public House(Door door) { Preconditions.CheckNotNull(door); this.door = door; } public void Paint(Color color) { // Paint the house, nothing to do with Doors. } } Doesn’t allow me to pass [TestMethod] void House_Painted_In_Red() Null  { House house = new House(null); house.Paint(Color.Red); assertEquels(Color.Red, house.GetColor()); }
  • 22. Few rules Paranoid programming; Null checking as preconditions. What prefer is; An application with tests that proves it works. Rather than having preconditions. You’re API with External calls, this might not applicable. What you really saying is; You don’t trust yourself.
  • 23. Few rules In production code; I don’t want to pass in Null. Pass me a Null Value Object rather. Avoid null checks. Make sure you get real objects which you can call methods. But, in Tests; The opposite of Production code.
  • 24. In summary We have Two Piles; Objects – Business logic, this is where you write code, most bugs are found. New Keywords – Providers, Factories, Builders etc. responsibility to construct the objects, DI. Separating Two Piles; Tests become small and easy to write. When wiring is wrong; It often blows up. System failures; Bug in a class or missing unit test.
  • 25. Follow me! t: @kosalanuwan w: kosalanuwan.tumblr.com e: kpe@exilesoft.com

Editor's Notes

  1. Example 1: Document class explained!How would you test this?We can use a test server
  2. Example 2: Mocked HtmlClient explained!Doesn’t care how instantiated, we just need it.We have to go thru same instantiate process when we create Document every time.Passing but not saving locally.Law of Demeter.
  3. Example 3: SOLUTION – Get rid of HtmlClient explained!Easy to test coz easy to construct.What preferable.Separating construction from work.
  4. Example 4: Testing House with Locator, what to override?Instead of passing a,b, & c, we now pass Service Locator.Suppose, House needs a “d”, somehow we pass it via Locator. So now some tests start failing, and we don’t know the reason.When we are using House, we need to give; House, Then Service Locator, And then rest of the application.
  5. Example 5: Testing House with Door, Roof, and Window!Now everything is clear.We know what are needed for our test.
  6. Example 6: LOD Violated – Purchase goods via Customer, Wallet, Money to record the sale.Not very interesting test, coz we have to Arrange Customer, Wallet, and put money etc.
  7. Example 7: LOD Obeyed – Purchase goods by passing Money.Greatly simplifies my tests.
  8. Example 8: Pass Color for DoorKnob doesn’t affect House.House doesn’t even know that Door has a DoorKnob.
  9. Factory is responsible to construct objects, not parent.So now it’s easier, not harder.
  10. Example 9: Preconditions.CheckNotNullMakes hard to instantiate objects
  11. Abandon new operator (99%).Not responsible constructing. Done by DI.Don’t ask anything that you don’t need. If its in constructor, it is shared by many places.