SlideShare a Scribd company logo
1 of 31
Download to read offline
Silicon Valley
                          Code Camp

 Contract-First Development with
                     >


Microsoft Code Contracts
    and Microsoft Pex

     Foothill College, October 8nd 2011
Theo Jungeblut
• Senior Software Developer at
  Omnicell Inc. in Mountain View
• Has been designing and
  implementing .NET based
  applications , components and
  frameworks for more than 8 years
• Previously worked in factory
  automation with focus on
  component based software and
  framework development for 3 ½
  years
                                     theo@csharp-lighthouse.com
• Degree in Software Engineering
  and Network Communications         www.csharp-lighthouse.com
Overview
•   How to measure Code Quality
•   Principles and Practices
•   Microsoft Code Contracts
•   How is your Code Coverage?
•   Microsoft Pex & Moles
•   Time for Source Code
•   Summary
•   Q&A
•   References
Does writing Clean Code
make us more efficient?
Clean Code???

  We are here for
Code Contracts, Pex
   and Mole!!
Clean Code is maintainable

     Source code must be:
     • readable & well structured
     • extensible
     • testable
Clean Code is maintainable

     Source code must be:
     • readable & well structured
     • extensible
     • testable
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
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();
                                             }
                                          }
Separation of Concerns
         (SoC)

 Single Responsibility
       Principle
         (SRP)
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
.NET Tools and their Impact
Tool name        Positive Impact           Negative Impact
Resharper        compiling ++++            VS responsiveness --
FxCop            code quality ++           compiling time -
StyleCop         code consistency +++      compiling time -
StyleCop plugin compiling time +++         VS responsiveness --
for Resharper
Ghost Doc        automated docs            potentially worse doc
Spell Checker    fewer spelling errors ++ performance --
Code Contracts   testability, quality ++   compiling time --
Pex & Moles      automated test ++         compiling time --
•   Design-by-Contract programming
•   Improved testability
•   Static verification
•   API documentation integration with
    Sandcastle
    http://msdn.microsoft.com/en-us/devlabs/dd491992
The Basic Idea
public class Logger : ILogger{
  public void Log(string message) {
       . Validation
       Input
       1.) Parameter Assignment
       2.) Execute method logics
   .   Optional State Validation/Invariants
       3.) Possible Return Statement
   .   Result Validation
  }}
Method Overview




Code Contracts User Manual, 5.3 Contract Ordering, page 22
Runtime Checking Levels




Code Contracts User Manual, 6.2.1 Runtime Checking Level, page 24
Argument Validation




Code Contracts User Manual,
5.1 Argument Validation and
Contracts, page 18
How is your Code Coverage?
How is your Code Coverage?


• Writing enough Unit Tests takes Time

• Writing basic Unit Tests is not Fun
Microsoft Pex & Moles


• Pex automatically generates test suites with
  high code coverage.

• Moles allows to replace any .NET method with
  a delegate.


          http://research.microsoft.com/en-us/projects/pex/
Microsoft Sandcastle

“Sandcastle produces accurate, MSDN style,
comprehensive documentation by reflecting over
the source assemblies and optionally integrating
XML Documentation Comments. Sandcastle has the
following key features:
• Works with or without authored comments
• Supports Generics and .NET”

          http://sandcastle.codeplex.com/
Time for Visual Studio
as well all love code,
       right !?
Summary Code Contracts
• Code Contracts:
  – Better readable source code (SoC, SRP, DRY)
  – Static Analysis for Code Contracts
  – Impacts the compile time

• Pex & Mole:
  – Generated paramized Unit tests
  – Moles allows mocking also of static

• Conclusion ??
Q&A
                                         Downloads,
                                         Feedback & Comments:
                                                  theo@csharp-lighthouse.com
                                                   www.csharp-lightouse.com
                                                   www.speakerrate.com/theoj
Graphic by Nathan Sawaya courtesy of brickartist.com
References
Code Contracts
•   See Dino Esposito’s MSM Magazine series on Code Contracts
•   http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx
•   http://research.microsoft.com/en-us/projects/contracts/
•   http://www.codeproject.com/KB/cs/CodeContracts.aspx
•   http://sachabarber.net/?p=811
•   http://blogs.msdn.com/b/somasegar/archive/2009/02/23/devlabs-code-contracts-for-net.aspx
•   http://designcoderelease.blogspot.com/2011/01/pex-moles.html
•   http://www.agilitylux.com/practices/code-contracts-by-example/

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

Sandcastle
•   http://sandcastle.codeplex.com/
•   http://stackoverflow.com/questions/3579655/does-sandcastle-support-code-contracts
Clean Code
Why Clean Code matters

    Foothill College, October 9nd 2011
Please fill out the
online feedback, and…


… thanks for you attention!

        And visit and support the
             Bay.net User Group
                    http://baynetug.org

More Related Content

What's hot

Clean Code - Design Patterns and Best Practices at Silicon Valley Code Camp
Clean Code - Design Patterns and Best Practices at Silicon Valley Code CampClean Code - Design Patterns and Best Practices at Silicon Valley Code Camp
Clean Code - Design Patterns and Best Practices at Silicon Valley Code CampTheo Jungeblut
 
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)Theo Jungeblut
 
Clean Code Part II - Dependency Injection at SoCal Code Camp
Clean Code Part II - Dependency Injection at SoCal Code CampClean Code Part II - Dependency Injection at SoCal Code Camp
Clean Code Part II - Dependency Injection at SoCal Code CampTheo Jungeblut
 
Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best PracticesTheo 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 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...Theo Jungeblut
 
Clean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipClean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipTheo Jungeblut
 
Mental models, complexity and software
Mental models, complexity and softwareMental models, complexity and software
Mental models, complexity and softwareArturo Herrero
 
Clean code & design patterns
Clean code & design patternsClean code & design patterns
Clean code & design patternsPascal Larocque
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design PrinciplesAndreas Enbohm
 
principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class designNeetu Mishra
 
Object-oriented design principles
Object-oriented design principlesObject-oriented design principles
Object-oriented design principlesXiaoyan Chen
 
WordCamp US: Clean Code
WordCamp US: Clean CodeWordCamp US: Clean Code
WordCamp US: Clean Codemtoppa
 
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-principlesEdorian
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Gianluca Padovani
 

What's hot (20)

Clean Code - Design Patterns and Best Practices at Silicon Valley Code Camp
Clean Code - Design Patterns and Best Practices at Silicon Valley Code CampClean Code - Design Patterns and Best Practices at Silicon Valley Code Camp
Clean Code - Design Patterns and Best Practices at Silicon Valley Code Camp
 
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
 
Clean Code Part II - Dependency Injection at SoCal Code Camp
Clean Code Part II - Dependency Injection at SoCal Code CampClean Code Part II - Dependency Injection at SoCal Code Camp
Clean Code Part II - Dependency Injection at SoCal Code Camp
 
Clean Code 2
Clean Code 2Clean Code 2
Clean Code 2
 
Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - 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 -
Clean Code Part i - Design Patterns and Best Practices -
 
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
Clean codeClean code
Clean code
 
Clean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipClean Code III - Software Craftsmanship
Clean Code III - Software Craftsmanship
 
Mental models, complexity and software
Mental models, complexity and softwareMental models, complexity and software
Mental models, complexity and software
 
SOLID design principles applied in Java
SOLID design principles applied in JavaSOLID design principles applied in Java
SOLID design principles applied in Java
 
Clean code & design patterns
Clean code & design patternsClean code & design patterns
Clean code & design patterns
 
Binding android piece by piece
Binding android piece by pieceBinding android piece by piece
Binding android piece by piece
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
 
SOLID principles
SOLID principlesSOLID principles
SOLID principles
 
principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class design
 
Object-oriented design principles
Object-oriented design principlesObject-oriented design principles
Object-oriented design principles
 
WordCamp US: Clean Code
WordCamp US: Clean CodeWordCamp US: Clean Code
WordCamp US: Clean Code
 
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
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 

Viewers also liked

Lydia's gate newsletter spring 2013
Lydia's gate newsletter spring 2013Lydia's gate newsletter spring 2013
Lydia's gate newsletter spring 2013Nycole Kelly
 
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...Theo Jungeblut
 
7i solutions in short
7i solutions in short7i solutions in short
7i solutions in shortfho1962
 
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 CampTheo Jungeblut
 
7i server app-oap-vl2
7i server app-oap-vl27i server app-oap-vl2
7i server app-oap-vl2fho1962
 
PRSA Pitch_NYU PR MBA Pitch
PRSA Pitch_NYU PR MBA PitchPRSA Pitch_NYU PR MBA Pitch
PRSA Pitch_NYU PR MBA PitchNicole Starling
 
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 OsnabrueckTheo Jungeblut
 
The simple life of quintina 2
The simple life of quintina 2The simple life of quintina 2
The simple life of quintina 2quinv
 
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
 
How to Grow Your Business Faster - Marketing Growth Hacks, Tools, Structure.
How to Grow Your Business Faster - Marketing Growth Hacks, Tools, Structure.How to Grow Your Business Faster - Marketing Growth Hacks, Tools, Structure.
How to Grow Your Business Faster - Marketing Growth Hacks, Tools, Structure.David Folwell
 
Narcotic controlled drugs policy and procedurelast
Narcotic   controlled drugs policy and procedurelastNarcotic   controlled drugs policy and procedurelast
Narcotic controlled drugs policy and procedurelastKnikkos
 

Viewers also liked (14)

Artifact 9 & 10
Artifact 9 & 10Artifact 9 & 10
Artifact 9 & 10
 
Lydia's gate newsletter spring 2013
Lydia's gate newsletter spring 2013Lydia's gate newsletter spring 2013
Lydia's gate newsletter spring 2013
 
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...
 
7i solutions in short
7i solutions in short7i solutions in short
7i solutions in short
 
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
 
7i server app-oap-vl2
7i server app-oap-vl27i server app-oap-vl2
7i server app-oap-vl2
 
PRSA Pitch_NYU PR MBA Pitch
PRSA Pitch_NYU PR MBA PitchPRSA Pitch_NYU PR MBA Pitch
PRSA Pitch_NYU PR MBA Pitch
 
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
 
The simple life of quintina 2
The simple life of quintina 2The simple life of quintina 2
The simple life of quintina 2
 
Penitencia
PenitenciaPenitencia
Penitencia
 
Chaps 1 2_3 maketing
Chaps 1 2_3 maketingChaps 1 2_3 maketing
Chaps 1 2_3 maketing
 
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...
 
How to Grow Your Business Faster - Marketing Growth Hacks, Tools, Structure.
How to Grow Your Business Faster - Marketing Growth Hacks, Tools, Structure.How to Grow Your Business Faster - Marketing Growth Hacks, Tools, Structure.
How to Grow Your Business Faster - Marketing Growth Hacks, Tools, Structure.
 
Narcotic controlled drugs policy and procedurelast
Narcotic   controlled drugs policy and procedurelastNarcotic   controlled drugs policy and procedurelast
Narcotic controlled drugs policy and procedurelast
 

Similar to Contract First Development with Microsoft Code Contracts and Microsoft Pex at Silicon Valley Code Camp 2011 (2011-10-08)

TechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen LuongTechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen LuongGrokking VN
 
Writing code that writes code - Nguyen Luong
Writing code that writes code - Nguyen LuongWriting code that writes code - Nguyen Luong
Writing code that writes code - Nguyen LuongVu Huy
 
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Tudor Dragan
 
Atlassian Groovy Plugins
Atlassian Groovy PluginsAtlassian Groovy Plugins
Atlassian Groovy PluginsPaul King
 
Gae icc fall2011
Gae icc fall2011Gae icc fall2011
Gae icc fall2011Juan Gomez
 
Practical Chaos Engineering
Practical Chaos EngineeringPractical Chaos Engineering
Practical Chaos EngineeringSIGHUP
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworksMD Sayem Ahmed
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...AboutYouGmbH
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLsintelliyole
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developerscacois
 
Java - A broad introduction
Java - A broad introductionJava - A broad introduction
Java - A broad introductionBirol Efe
 
Going to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific LanguagesGoing to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific LanguagesGuillaume Laforge
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptxVijalJain3
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - GreachHamletDRC
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 

Similar to Contract First Development with Microsoft Code Contracts and Microsoft Pex at Silicon Valley Code Camp 2011 (2011-10-08) (20)

TechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen LuongTechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
 
Writing code that writes code - Nguyen Luong
Writing code that writes code - Nguyen LuongWriting code that writes code - Nguyen Luong
Writing code that writes code - Nguyen Luong
 
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
 
Atlassian Groovy Plugins
Atlassian Groovy PluginsAtlassian Groovy Plugins
Atlassian Groovy Plugins
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Gae icc fall2011
Gae icc fall2011Gae icc fall2011
Gae icc fall2011
 
Practical Chaos Engineering
Practical Chaos EngineeringPractical Chaos Engineering
Practical Chaos Engineering
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworks
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
 
C# for Java Developers
C# for Java DevelopersC# for Java Developers
C# for Java Developers
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
Next .NET and C#
Next .NET and C#Next .NET and C#
Next .NET and C#
 
Effective Object Oriented Design in Cpp
Effective Object Oriented Design in CppEffective Object Oriented Design in Cpp
Effective Object Oriented Design in Cpp
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developers
 
Java - A broad introduction
Java - A broad introductionJava - A broad introduction
Java - A broad introduction
 
Going to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific LanguagesGoing to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific Languages
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
Tech talks#6: Code Refactoring
Tech talks#6: Code RefactoringTech talks#6: Code Refactoring
Tech talks#6: Code Refactoring
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - Greach
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 

Recently uploaded

Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
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
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 

Recently uploaded (20)

Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
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
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 

Contract First Development with Microsoft Code Contracts and Microsoft Pex at Silicon Valley Code Camp 2011 (2011-10-08)

  • 1. Silicon Valley Code Camp Contract-First Development with > Microsoft Code Contracts and Microsoft Pex Foothill College, October 8nd 2011
  • 2. Theo Jungeblut • Senior Software Developer at Omnicell Inc. in Mountain View • Has been designing and implementing .NET based applications , components and frameworks for more than 8 years • Previously worked in factory automation with focus on component based software and framework development for 3 ½ years theo@csharp-lighthouse.com • Degree in Software Engineering and Network Communications www.csharp-lighthouse.com
  • 3. Overview • How to measure Code Quality • Principles and Practices • Microsoft Code Contracts • How is your Code Coverage? • Microsoft Pex & Moles • Time for Source Code • Summary • Q&A • References
  • 4. Does writing Clean Code make us more efficient?
  • 5.
  • 6. Clean Code??? We are here for Code Contracts, Pex and Mole!!
  • 7. Clean Code is maintainable Source code must be: • readable & well structured • extensible • testable
  • 8. Clean Code is maintainable Source code must be: • readable & well structured • extensible • testable
  • 9. 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
  • 11. 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(); } }
  • 12. Separation of Concerns (SoC) Single Responsibility Principle (SRP)
  • 13. 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
  • 14. 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
  • 15. .NET Tools and their Impact Tool name Positive Impact Negative Impact Resharper compiling ++++ VS responsiveness -- FxCop code quality ++ compiling time - StyleCop code consistency +++ compiling time - StyleCop plugin compiling time +++ VS responsiveness -- for Resharper Ghost Doc automated docs potentially worse doc Spell Checker fewer spelling errors ++ performance -- Code Contracts testability, quality ++ compiling time -- Pex & Moles automated test ++ compiling time --
  • 16. Design-by-Contract programming • Improved testability • Static verification • API documentation integration with Sandcastle http://msdn.microsoft.com/en-us/devlabs/dd491992
  • 17. The Basic Idea public class Logger : ILogger{ public void Log(string message) { . Validation Input 1.) Parameter Assignment 2.) Execute method logics . Optional State Validation/Invariants 3.) Possible Return Statement . Result Validation }}
  • 18. Method Overview Code Contracts User Manual, 5.3 Contract Ordering, page 22
  • 19. Runtime Checking Levels Code Contracts User Manual, 6.2.1 Runtime Checking Level, page 24
  • 20. Argument Validation Code Contracts User Manual, 5.1 Argument Validation and Contracts, page 18
  • 21.
  • 22. How is your Code Coverage?
  • 23. How is your Code Coverage? • Writing enough Unit Tests takes Time • Writing basic Unit Tests is not Fun
  • 24. Microsoft Pex & Moles • Pex automatically generates test suites with high code coverage. • Moles allows to replace any .NET method with a delegate. http://research.microsoft.com/en-us/projects/pex/
  • 25. Microsoft Sandcastle “Sandcastle produces accurate, MSDN style, comprehensive documentation by reflecting over the source assemblies and optionally integrating XML Documentation Comments. Sandcastle has the following key features: • Works with or without authored comments • Supports Generics and .NET” http://sandcastle.codeplex.com/
  • 26. Time for Visual Studio as well all love code, right !?
  • 27. Summary Code Contracts • Code Contracts: – Better readable source code (SoC, SRP, DRY) – Static Analysis for Code Contracts – Impacts the compile time • Pex & Mole: – Generated paramized Unit tests – Moles allows mocking also of static • Conclusion ??
  • 28. Q&A Downloads, Feedback & Comments: theo@csharp-lighthouse.com www.csharp-lightouse.com www.speakerrate.com/theoj Graphic by Nathan Sawaya courtesy of brickartist.com
  • 29. References Code Contracts • See Dino Esposito’s MSM Magazine series on Code Contracts • http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx • http://research.microsoft.com/en-us/projects/contracts/ • http://www.codeproject.com/KB/cs/CodeContracts.aspx • http://sachabarber.net/?p=811 • http://blogs.msdn.com/b/somasegar/archive/2009/02/23/devlabs-code-contracts-for-net.aspx • http://designcoderelease.blogspot.com/2011/01/pex-moles.html • http://www.agilitylux.com/practices/code-contracts-by-example/ Pex & Mole • http://research.microsoft.com/en-us/projects/pex/ Sandcastle • http://sandcastle.codeplex.com/ • http://stackoverflow.com/questions/3579655/does-sandcastle-support-code-contracts
  • 30. Clean Code Why Clean Code matters Foothill College, October 9nd 2011
  • 31. Please fill out the online feedback, and… … thanks for you attention! And visit and support the Bay.net User Group http://baynetug.org