SlideShare a Scribd company logo
1 of 53
SolCal
                 CodeCamp


Clean Code I
  Design Patterns
 and Best Practices
    San Diego, June 24rd 2012
Theo Jungeblut
• Senior Software Developer at
  AppDynamics in San Francisco
• architects decoupled solutions
  tailored to business needs and crafts
  maintainable code to last
• worked in healthcare and factory
  automation, building mission critical
  applications, framework &
  platforms for 8+ years
• degree in Software Engineering
  and Network Communications              theo@designitright.net
• enjoys cycling, running and eating      www.designitright.net
                                          www.speakerrate.com/theoj
Overview
•   Why Clean Code
•   Clean Code Developer Initiative
•   Principles and Practices
•   Code Comparison
•   Q&A
Does writing Clean Code
make us more efficient?
The only valid Measurement of Code
               Quality
What is Clean Code?
Clean Code is maintainable

     Source code must be:
     • readable & well structured
     • extensible
     • testable
Software
 Engineering
      vs.
Craftsmanship
Software
 Engineering
     AND
Craftsmanship
The “Must Read”-Book(s)
 by Robert C Martin


A Handbook of Agile
Software
Craftsmanship

“Even bad code can
function. But if code
isn’t clean, it can bring a
development
organization to its
knees.”
Code Maintainability *
    Principles                   Patterns                   Containers


       Why?                        How?                        What?


  Extensibility                Clean Code                   Tool reuse

* from: Mark Seemann’s “Dependency Injection in .NET” presentation Bay.NET 05/2011
Clean Code Developer




Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
Clean Code Developer – 1st Iteration
     by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de




Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
Keep it simple, stupid
        (KISS)
KISS-Principle – “Keep It Simple Stupid”
   by Kelly Johnson




                      http://blogs.smarter.com/blogs/Lego%20Brick.jpg
The Power of Simplicity




                                                       Graphic by Nathan Sawaya courtesy of brickartist.com




Graphic by Nathan Sawaya courtesy of brickartist.com

                                                                                http://www.geekalerts.com/lego-iphone/
Chaos build from simplicity




Graphic by Nathan Sawaya courtesy of brickartist.com
Don’t repeat yourself
        (DRY)
Don’t repeat yourself (DRY)
        by Andy Hunt and Dave Thomas in their book “The Pragmatic Programmer”

// Code Copy and Paste Method                                                    // DRY Method
public Class Person                                                              public Class Person
 {                                                                                {
   public string FirstName { get; set;}                                             public string FirstName { get; set;}
   public string LastName { get; set;}                                              public string LastName { get; set;}

    public Person(Person person)                                                     public Person(Person person)
    {                                                                                {
      this.FirstName = string.IsNullOrEmpty(person.FirstName)                          this.FirstName = person.FirstName.CloneSecured();
                 ? string.Empty : (string) person.FirstName.Clone();                   this.LastName = person.LastName.CloneSecured();
                                                                                     }
        this.LastName = string.IsNullOrEmpty(person.LastName)
                  ? string.Empty : (string) person.LastName.Clone();                 public object Clone()
    }                                                                                {
                                                                                       return new Person(this);
    public object Clone()                                                            }
    {                                                                            }
      return new Person(this);
    }
}                                         public static class StringExtension
                                           {
                                             public static string CloneSecured(this string original)
                                             {
                                               return string.IsNullOrEmpty(original) ? string.Empty : (string)original.Clone();
                                             }
                                          }
Clean Code Developer – 1st Iteration
     by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de




Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
Clean Code Developer – 2nd Iteration
     by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de




Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
Separation of Concerns
         (SoC)

 Single Responsibility
       Principle
         (SRP)
The Product




http://www.technicopedia.com/8865.html
Component / Service




          http://www.technicopedia.com/8865.html
Class, Struct, Enum etc.




http://technicbricks.blogspot.com/2009/06/tbs-techpoll-12-results-2009-1st.html
Separation of Concerns (SoC)
     probably by Edsger W. Dijkstra in 1974

• “In computer science,
separation of concerns (SoC) is
the process of separating a
computer program into distinct
features that overlap in
functionality as little as possible.

•A concern is any piece of
interest or focus in a program.
Typically, concerns are
synonymous with features or
behaviors. “
 http://en.wikipedia.org/wiki/Separati
 on_of_Concerns
Single Responsibility Principle (SRP)
      by Robert C Martin


“Every object should have a single responsibility, and that
responsibility should be entirely encapsulated by the class.”
    http://en.wikipedia.org/wiki/Single_responsibility_principle




public class Logger : ILogger
{
  public Logger(ILoggingSink loggingSink)
  {}

     public void Log(string message)
     {}
}
                                                                   http://www.ericalbrecht.com
Read, Read, Read
Clean Code Developer – 2nd Iteration
     by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de




Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
SolCal
                 CodeCamp


Clean Code III
      Software
   Craftsmanship
    San Diego, June 23rd 2012
Clean Code Developer – 3rd Iteration
     by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de




Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
Information Hiding Principle
           (IHP)
Information Hiding Principle (IHP)
         by David Parnas (1972)


“.. information hiding is the principle of
segregation of the design decisions on a
computer program that are most likely to
change, ..”
 http://en.wikipedia.org/wiki/Information_hiding
Interfaces / Contracts
• Decouple Usage and Implementation through introduction of a contract
• Allows to replace implementation without changing the consumer

public interface ILogger              public class Logger : ILogger
{                                     {
  void Log(string message);             public Logger(ILoggingSink loggingSink)
}                                       {}

                                          public void Log(string message)
                                          {}
                                      }
Liskov Substitution Principle
           (LSP)
Liskov Substitution Principle (LSP)
by Barbara Liskov, Jannette Wing (1994)


“Liskov’s notion of a behavioral subtype
defines a notion of substitutability for
mutable objects”
 http://en.wikipedia.org/wiki/Liskov_substitution_principle
Dependency Inversion Principle
           (DIP)
Dependency Inversion Principle (DIP)
  by Robert C. Martin


• “High-level modules should not depend on
  low-level modules. Both should depend on
  abstractions.
• Abstractions should not depend upon details.
  Details should depend upon abstractions.”
  http://en.wikipedia.org/wiki/Dependency_inversion_principle
Clean Code Developer – 3rd Iteration
     by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de




Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
Clean Code Developer – 4th Iteration
     by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de




Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
Open Closed Principle
       (OCP)
Open/Closed Principle (OCP)
by Bertrand Meyer (1988)


An implementation is open for extension
but closed for modification




           http://en.wikipedia.org/wiki/Open/closed_principle
Law of Demeter
     (LoD)
Law of Demeter (LoD)
           Northeastern University (1987)

“
• Each unit should have only limited knowledge
  about other units: only units “closely” related
  to the current unit.
• Each unit should only talk to its friends;
  don’t talk to strangers
• Only talk to your immediate friends.”
  http://en.wikipedia.org/wiki/Law_Of_Demeter
S               Single Responsibility Principle

   O               Open/Closed Principle

   L               Liskov Substitution Principle

   I               Interface Segregation Principle

   D               Dependency Inversion Principle

Robert C Martin:   http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod
Clean Code Developer – 4th Iteration
     by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de




Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
Silicon Valley Code Camp Oct. 6th – 7th




http://www.siliconvalley-codecamp.com
Clean Code Developer – 5th Iteration
     by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de




Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
Summary Clean Code
Maintainability is achieved through:
• Readability (Coding Guidelines)
• Simplification and Specialization
  (KISS, SoC, SRP, OCP, )
• Decoupling (LSP, DIP, IHP, Contracts,
  LoD, CoP, IoC or SOA)
• Avoiding Code Bloat (DRY, YAGNI)
• Quality through Testability
  (all of them!)
Q&A
                                         Downloads,
                                         Feedback & Comments:
                                                   theo@designitright.net
                                                   www.designitright.net
                                                   www.speakerrate.com/theoj
Graphic by Nathan Sawaya courtesy of brickartist.com
http://clean-code-developer.com
                                          References…
http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod
http://www.manning.com/seemann/
http://en.wikipedia.org/wiki/Keep_it_simple_stupid
http://picocontainer.org/patterns.html
http://en.wikipedia.org/wiki/Separation_of_concerns
http://en.wikipedia.org/wiki/Single_responsibility_principle
http://en.wikipedia.org/wiki/Information_hiding
http://en.wikipedia.org/wiki/Liskov_substitution_principle
http://en.wikipedia.org/wiki/Dependency_inversion_principle
http://en.wikipedia.org/wiki/Open/closed_principle
http://en.wikipedia.org/wiki/Law_Of_Demeter
http://en.wikipedia.org/wiki/Don't_repeat_yourself
http://en.wikipedia.org/wiki/You_ain't_gonna_need_it
http://en.wikipedia.org/wiki/Component-oriented_programming
http://en.wikipedia.org/wiki/Service-oriented_architecture
http://www.martinfowler.com/articles/injection.html
http://www.codeproject.com/KB/aspnet/IOCDI.aspx
http://msdn.microsoft.com/en-us/magazine/cc163739.aspx
http://msdn.microsoft.com/en-us/library/ff650320.aspx
http://msdn.microsoft.com/en-us/library/aa973811.aspx
http://msdn.microsoft.com/en-us/library/ff647976.aspx
http://msdn.microsoft.com/en-us/library/cc707845.aspx
http://msdn.microsoft.com/en-us/library/bb833022.aspx
http://unity.codeplex.com/
http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=5&tabid=11
… more References
Resharper
http://www.jetbrains.com/resharper/

FxCop / Code Analysis
http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx
http://blogs.msdn.com/b/codeanalysis/
http://www.binarycoder.net/fxcop/index.html

Code Contracts
http://msdn.microsoft.com/en-us/devlabs/dd491992
http://research.microsoft.com/en-us/projects/contracts/

Pex & Mole
http://research.microsoft.com/en-us/projects/pex/

StyleCop
http://stylecop.codeplex.com/

Ghostdoc
http://submain.com/products/ghostdoc.aspx

Spellchecker
http://visualstudiogallery.msdn.microsoft.com/
7c8341f1-ebac-40c8-92c2-476db8d523ce//
                                                               Lego (trademarked in capitals as LEGO)
Please fill out the
feedback, and…
                          www.speakerrate.com/theoj




… thanks for you attention!
            And visit and support the

                      www.sandiegodotnet.com

More Related Content

What's hot

Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
Theo Jungeblut
 
Clean Code Part i - Design Patterns and Best Practices -
Clean Code Part i - Design Patterns and Best Practices -Clean Code Part i - Design Patterns and Best Practices -
Clean Code Part i - Design Patterns and Best Practices -
Theo Jungeblut
 
Clean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipClean Code III - Software Craftsmanship
Clean Code III - Software Craftsmanship
Theo Jungeblut
 

What's hot (20)

Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
 
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...Contract First Development with Microsoft Code Contracts and Microsoft Pex at...
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...
 
Clean code
Clean codeClean code
Clean code
 
Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)
Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)
Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)
 
Clean Code Part i - Design Patterns and Best Practices -
Clean Code Part i - Design Patterns and Best Practices -Clean Code Part i - Design Patterns and Best Practices -
Clean Code Part i - Design Patterns and Best Practices -
 
Mental models, complexity and software
Mental models, complexity and softwareMental models, complexity and software
Mental models, complexity and software
 
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
 
Clean Code 2
Clean Code 2Clean Code 2
Clean Code 2
 
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
 
Clean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipClean Code III - Software Craftsmanship
Clean Code III - Software Craftsmanship
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Clean code & design patterns
Clean code & design patternsClean code & design patterns
Clean code & design patterns
 
Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
SOLID design principles applied in Java
SOLID design principles applied in JavaSOLID design principles applied in Java
SOLID design principles applied in Java
 
WordCamp US: Clean Code
WordCamp US: Clean CodeWordCamp US: Clean Code
WordCamp US: Clean Code
 
Binding android piece by piece
Binding android piece by pieceBinding android piece by piece
Binding android piece by piece
 
Functional Dependency Injection in C#
Functional Dependency Injection in C#Functional Dependency Injection in C#
Functional Dependency Injection in C#
 
Object-oriented design principles
Object-oriented design principlesObject-oriented design principles
Object-oriented design principles
 
The Open Web
The Open WebThe Open Web
The Open Web
 

Viewers also liked

Accidentally Manager – A Survival Guide for First-Time Engineering Managers
Accidentally Manager – A Survival Guide for First-Time Engineering ManagersAccidentally Manager – A Survival Guide for First-Time Engineering Managers
Accidentally Manager – A Survival Guide for First-Time Engineering Managers
Theo Jungeblut
 
Clean Code III - Software Craftsmanship at SoCal Code Camp San Diego (07/27/2...
Clean Code III - Software Craftsmanship at SoCal Code Camp San Diego (07/27/2...Clean Code III - Software Craftsmanship at SoCal Code Camp San Diego (07/27/2...
Clean Code III - Software Craftsmanship at SoCal Code Camp San Diego (07/27/2...
Theo Jungeblut
 
Building an Online-Recommendation Engine with MongoDB
Building an Online-Recommendation Engine with MongoDBBuilding an Online-Recommendation Engine with MongoDB
Building an Online-Recommendation Engine with MongoDB
MongoDB
 
Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principles
Edorian
 

Viewers also liked (15)

Accidentally Manager – A Survival Guide for First-Time Engineering Managers
Accidentally Manager – A Survival Guide for First-Time Engineering ManagersAccidentally Manager – A Survival Guide for First-Time Engineering Managers
Accidentally Manager – A Survival Guide for First-Time Engineering Managers
 
02 integrate highchart
02 integrate highchart02 integrate highchart
02 integrate highchart
 
Clean Code III - Software Craftsmanship at SoCal Code Camp San Diego (07/27/2...
Clean Code III - Software Craftsmanship at SoCal Code Camp San Diego (07/27/2...Clean Code III - Software Craftsmanship at SoCal Code Camp San Diego (07/27/2...
Clean Code III - Software Craftsmanship at SoCal Code Camp San Diego (07/27/2...
 
Clean code
Clean codeClean code
Clean code
 
Building an Online-Recommendation Engine with MongoDB
Building an Online-Recommendation Engine with MongoDBBuilding an Online-Recommendation Engine with MongoDB
Building an Online-Recommendation Engine with MongoDB
 
05 integrate redis
05 integrate redis05 integrate redis
05 integrate redis
 
Introduction to rest.li
Introduction to rest.liIntroduction to rest.li
Introduction to rest.li
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
 
Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOS...
Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOS...Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOS...
Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOS...
 
Clean Code: Stop wasting my time
Clean Code: Stop wasting my timeClean Code: Stop wasting my time
Clean Code: Stop wasting my time
 
Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principles
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean code
Clean codeClean code
Clean code
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
Powerpoint on environmental issues
Powerpoint on environmental issuesPowerpoint on environmental issues
Powerpoint on environmental issues
 

Similar to Clean Code Part I - Design Patterns at SoCal Code Camp

Similar to Clean Code Part I - Design Patterns at SoCal Code Camp (20)

2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Seven Ineffective Coding Habits of Many Programmers
Seven Ineffective Coding Habits of Many ProgrammersSeven Ineffective Coding Habits of Many Programmers
Seven Ineffective Coding Habits of Many Programmers
 
The software design principles
The software design principlesThe software design principles
The software design principles
 
designpatterns_blair_upe.ppt
designpatterns_blair_upe.pptdesignpatterns_blair_upe.ppt
designpatterns_blair_upe.ppt
 
Microservices Application Tracing Standards and Simulators - Adrians at OSCON
Microservices Application Tracing Standards and Simulators - Adrians at OSCONMicroservices Application Tracing Standards and Simulators - Adrians at OSCON
Microservices Application Tracing Standards and Simulators - Adrians at OSCON
 
Andrei Iacob - SOLID: Strategies for Implementing Object–Oriented Design Prin...
Andrei Iacob - SOLID: Strategies for Implementing Object–Oriented Design Prin...Andrei Iacob - SOLID: Strategies for Implementing Object–Oriented Design Prin...
Andrei Iacob - SOLID: Strategies for Implementing Object–Oriented Design Prin...
 
Introduction to Object oriented Design
Introduction to Object oriented DesignIntroduction to Object oriented Design
Introduction to Object oriented Design
 
Patterns for the People
Patterns for the PeoplePatterns for the People
Patterns for the People
 
1. Mini seminar intro
1. Mini seminar intro1. Mini seminar intro
1. Mini seminar intro
 
Objective c
Objective cObjective c
Objective c
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
 
Twins: OOP and FP
Twins: OOP and FPTwins: OOP and FP
Twins: OOP and FP
 
Great Wide Open: Crash Course Open Source Cloud Computing - 2014
Great Wide Open: Crash Course Open Source Cloud Computing - 2014Great Wide Open: Crash Course Open Source Cloud Computing - 2014
Great Wide Open: Crash Course Open Source Cloud Computing - 2014
 
Interop - Crash Course In Open Source Cloud Computing
Interop - Crash Course In Open Source Cloud ComputingInterop - Crash Course In Open Source Cloud Computing
Interop - Crash Course In Open Source Cloud Computing
 
SCAM 2012 Keynote Slides on Cooperative Testing and Analysis by Tao Xie
SCAM 2012 Keynote Slides on Cooperative Testing and Analysis by Tao XieSCAM 2012 Keynote Slides on Cooperative Testing and Analysis by Tao Xie
SCAM 2012 Keynote Slides on Cooperative Testing and Analysis by Tao Xie
 
Object
ObjectObject
Object
 
Practical Chaos Engineering
Practical Chaos EngineeringPractical Chaos Engineering
Practical Chaos Engineering
 
Worse is better, for better or for worse - Kevlin Henney
Worse is better, for better or for worse - Kevlin HenneyWorse is better, for better or for worse - Kevlin Henney
Worse is better, for better or for worse - Kevlin Henney
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 

More from Theo Jungeblut

More from Theo Jungeblut (6)

Cut your Dependencies with - Dependency Injection for South Bay.NET User Grou...
Cut your Dependencies with - Dependency Injection for South Bay.NET User Grou...Cut your Dependencies with - Dependency Injection for South Bay.NET User Grou...
Cut your Dependencies with - Dependency Injection for South Bay.NET User Grou...
 
Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...
Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...
Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...
 
Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...
Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...
Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...
 
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Cut your Dependencies with Dependency Injection for East Bay.NET User Group Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
 
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckCut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
 
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
Cut your Dependencies - Dependency Injection at Silicon Valley Code CampCut your Dependencies - Dependency Injection at Silicon Valley Code Camp
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
 

Recently uploaded

+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...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

+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...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
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)
 
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...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 

Clean Code Part I - Design Patterns at SoCal Code Camp

  • 1. SolCal CodeCamp Clean Code I Design Patterns and Best Practices San Diego, June 24rd 2012
  • 2. Theo Jungeblut • Senior Software Developer at AppDynamics in San Francisco • architects decoupled solutions tailored to business needs and crafts maintainable code to last • worked in healthcare and factory automation, building mission critical applications, framework & platforms for 8+ years • degree in Software Engineering and Network Communications theo@designitright.net • enjoys cycling, running and eating www.designitright.net www.speakerrate.com/theoj
  • 3. Overview • Why Clean Code • Clean Code Developer Initiative • Principles and Practices • Code Comparison • Q&A
  • 4. Does writing Clean Code make us more efficient?
  • 5. The only valid Measurement of Code Quality
  • 7. Clean Code is maintainable Source code must be: • readable & well structured • extensible • testable
  • 8. Software Engineering vs. Craftsmanship
  • 9. Software Engineering AND Craftsmanship
  • 10. The “Must Read”-Book(s) by Robert C Martin A Handbook of Agile Software Craftsmanship “Even bad code can function. But if code isn’t clean, it can bring a development organization to its knees.”
  • 11. Code Maintainability * Principles Patterns Containers Why? How? What? Extensibility Clean Code Tool reuse * from: Mark Seemann’s “Dependency Injection in .NET” presentation Bay.NET 05/2011
  • 12. Clean Code Developer Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  • 13. Clean Code Developer – 1st Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  • 14. Keep it simple, stupid (KISS)
  • 15. KISS-Principle – “Keep It Simple Stupid” by Kelly Johnson http://blogs.smarter.com/blogs/Lego%20Brick.jpg
  • 16. The Power of Simplicity Graphic by Nathan Sawaya courtesy of brickartist.com Graphic by Nathan Sawaya courtesy of brickartist.com http://www.geekalerts.com/lego-iphone/
  • 17. Chaos build from simplicity Graphic by Nathan Sawaya courtesy of brickartist.com
  • 19. Don’t repeat yourself (DRY) by Andy Hunt and Dave Thomas in their book “The Pragmatic Programmer” // Code Copy and Paste Method // DRY Method public Class Person public Class Person { { public string FirstName { get; set;} public string FirstName { get; set;} public string LastName { get; set;} public string LastName { get; set;} public Person(Person person) public Person(Person person) { { this.FirstName = string.IsNullOrEmpty(person.FirstName) this.FirstName = person.FirstName.CloneSecured(); ? string.Empty : (string) person.FirstName.Clone(); this.LastName = person.LastName.CloneSecured(); } this.LastName = string.IsNullOrEmpty(person.LastName) ? string.Empty : (string) person.LastName.Clone(); public object Clone() } { return new Person(this); public object Clone() } { } return new Person(this); } } public static class StringExtension { public static string CloneSecured(this string original) { return string.IsNullOrEmpty(original) ? string.Empty : (string)original.Clone(); } }
  • 20. Clean Code Developer – 1st Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  • 21. Clean Code Developer – 2nd Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  • 22. Separation of Concerns (SoC) Single Responsibility Principle (SRP)
  • 24. Component / Service http://www.technicopedia.com/8865.html
  • 25. Class, Struct, Enum etc. http://technicbricks.blogspot.com/2009/06/tbs-techpoll-12-results-2009-1st.html
  • 26. Separation of Concerns (SoC) probably by Edsger W. Dijkstra in 1974 • “In computer science, separation of concerns (SoC) is the process of separating a computer program into distinct features that overlap in functionality as little as possible. •A concern is any piece of interest or focus in a program. Typically, concerns are synonymous with features or behaviors. “ http://en.wikipedia.org/wiki/Separati on_of_Concerns
  • 27. Single Responsibility Principle (SRP) by Robert C Martin “Every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class.” http://en.wikipedia.org/wiki/Single_responsibility_principle public class Logger : ILogger { public Logger(ILoggingSink loggingSink) {} public void Log(string message) {} } http://www.ericalbrecht.com
  • 29. Clean Code Developer – 2nd Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  • 30. SolCal CodeCamp Clean Code III Software Craftsmanship San Diego, June 23rd 2012
  • 31. Clean Code Developer – 3rd Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  • 33. Information Hiding Principle (IHP) by David Parnas (1972) “.. information hiding is the principle of segregation of the design decisions on a computer program that are most likely to change, ..” http://en.wikipedia.org/wiki/Information_hiding
  • 34. Interfaces / Contracts • Decouple Usage and Implementation through introduction of a contract • Allows to replace implementation without changing the consumer public interface ILogger public class Logger : ILogger { { void Log(string message); public Logger(ILoggingSink loggingSink) } {} public void Log(string message) {} }
  • 36. Liskov Substitution Principle (LSP) by Barbara Liskov, Jannette Wing (1994) “Liskov’s notion of a behavioral subtype defines a notion of substitutability for mutable objects” http://en.wikipedia.org/wiki/Liskov_substitution_principle
  • 38. Dependency Inversion Principle (DIP) by Robert C. Martin • “High-level modules should not depend on low-level modules. Both should depend on abstractions. • Abstractions should not depend upon details. Details should depend upon abstractions.” http://en.wikipedia.org/wiki/Dependency_inversion_principle
  • 39. Clean Code Developer – 3rd Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  • 40. Clean Code Developer – 4th Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  • 42. Open/Closed Principle (OCP) by Bertrand Meyer (1988) An implementation is open for extension but closed for modification http://en.wikipedia.org/wiki/Open/closed_principle
  • 43. Law of Demeter (LoD)
  • 44. Law of Demeter (LoD) Northeastern University (1987) “ • Each unit should have only limited knowledge about other units: only units “closely” related to the current unit. • Each unit should only talk to its friends; don’t talk to strangers • Only talk to your immediate friends.” http://en.wikipedia.org/wiki/Law_Of_Demeter
  • 45. S Single Responsibility Principle O Open/Closed Principle L Liskov Substitution Principle I Interface Segregation Principle D Dependency Inversion Principle Robert C Martin: http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod
  • 46. Clean Code Developer – 4th Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  • 47. Silicon Valley Code Camp Oct. 6th – 7th http://www.siliconvalley-codecamp.com
  • 48. Clean Code Developer – 5th Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de Graphic by Michael HĂśnnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  • 49. Summary Clean Code Maintainability is achieved through: • Readability (Coding Guidelines) • Simplification and Specialization (KISS, SoC, SRP, OCP, ) • Decoupling (LSP, DIP, IHP, Contracts, LoD, CoP, IoC or SOA) • Avoiding Code Bloat (DRY, YAGNI) • Quality through Testability (all of them!)
  • 50. Q&A Downloads, Feedback & Comments: theo@designitright.net www.designitright.net www.speakerrate.com/theoj Graphic by Nathan Sawaya courtesy of brickartist.com
  • 51. http://clean-code-developer.com References… http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/ http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod http://www.manning.com/seemann/ http://en.wikipedia.org/wiki/Keep_it_simple_stupid http://picocontainer.org/patterns.html http://en.wikipedia.org/wiki/Separation_of_concerns http://en.wikipedia.org/wiki/Single_responsibility_principle http://en.wikipedia.org/wiki/Information_hiding http://en.wikipedia.org/wiki/Liskov_substitution_principle http://en.wikipedia.org/wiki/Dependency_inversion_principle http://en.wikipedia.org/wiki/Open/closed_principle http://en.wikipedia.org/wiki/Law_Of_Demeter http://en.wikipedia.org/wiki/Don't_repeat_yourself http://en.wikipedia.org/wiki/You_ain't_gonna_need_it http://en.wikipedia.org/wiki/Component-oriented_programming http://en.wikipedia.org/wiki/Service-oriented_architecture http://www.martinfowler.com/articles/injection.html http://www.codeproject.com/KB/aspnet/IOCDI.aspx http://msdn.microsoft.com/en-us/magazine/cc163739.aspx http://msdn.microsoft.com/en-us/library/ff650320.aspx http://msdn.microsoft.com/en-us/library/aa973811.aspx http://msdn.microsoft.com/en-us/library/ff647976.aspx http://msdn.microsoft.com/en-us/library/cc707845.aspx http://msdn.microsoft.com/en-us/library/bb833022.aspx http://unity.codeplex.com/ http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=5&tabid=11
  • 52. … more References Resharper http://www.jetbrains.com/resharper/ FxCop / Code Analysis http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx http://blogs.msdn.com/b/codeanalysis/ http://www.binarycoder.net/fxcop/index.html Code Contracts http://msdn.microsoft.com/en-us/devlabs/dd491992 http://research.microsoft.com/en-us/projects/contracts/ Pex & Mole http://research.microsoft.com/en-us/projects/pex/ StyleCop http://stylecop.codeplex.com/ Ghostdoc http://submain.com/products/ghostdoc.aspx Spellchecker http://visualstudiogallery.msdn.microsoft.com/ 7c8341f1-ebac-40c8-92c2-476db8d523ce// Lego (trademarked in capitals as LEGO)
  • 53. Please fill out the feedback, and… www.speakerrate.com/theoj … thanks for you attention! And visit and support the www.sandiegodotnet.com

Editor's Notes

  1. A object or any type implementing subtype or a certain interface can be replaced with another object implementing the same base type or interface.