Silicon Valley                      Code Camp Clean CodeWhy Clean Code matters    Foothill College, October 9nd 2011
Theo Jungeblut• Senior Software Developer at  Omnicell Inc. in Mountain View• Has been designing and  implementing .NET ba...
Overview• Why Clean Code• Tools       •   Resharper       •   FxCop, StyleCop & StyleCop plugin for Resharper       •   Gh...
Does writing Clean Codemake us more efficient?
What is Clean Code?
Clean Code is maintainable     Source code must be:     • readable & well structured     • extensible     • testable
Software Engineering      vs.Craftsmanship
The “Must Read”-Book(s) by Robert C MartinA Handbook of AgileSoftwareCraftsmanship“Even bad code canfunction. But if codei...
Code Maintainability *    Principles                   Patterns                   Containers       Why?                   ...
.NET Tools and their ImpactTool name        Positive Impact           Negative ImpactResharper        compiling ++++      ...
Resharper“The single most impacting development        addition to Visual Studio”     Features:       –   Code Analysis   ...
FxCop / Static Code Analysis  Code Analysis:    – Correctness    – Library design    – Internationalization and localizati...
Style Cop with R# Integration   Code Consistency & Readability:     – Automated check of C# coding       standard     – En...
Ghost Doc• Save keystrokes and time• Simplify documenting your code• Benefit of the base class documentation          http...
Spell Checker  • Spelll chicking for literals and comments in VShttp://visualstudiogallery.msdn.microsoft.com/7c8341f1-eba...
•   Design-by-Contract programming•   Improved testability•   Static verification•   API documentation integration with   ...
Microsoft Pex & Moles• Pex automatically generates test suites with  high code coverage.• Moles allows to replace any .NET...
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.deGraphic by M...
Keep it simple, stupid        (KISS)
KISS-Principle – “Keep It Simple Stupid”   by Kelly Johnson                      http://blogs.smarter.com/blogs/Lego%20Bri...
The Power of Simplicity                                                       Graphic by Nathan Sawaya courtesy of brickar...
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 P...
Clean Code Developer – 2nd Iteration     by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.deGraphic by M...
Separation of Concerns         (SoC) Single Responsibility       Principle         (SRP)
The Producthttp://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)...
Single Responsibility Principle (SRP)      by Robert C Martin“Every object should have a single responsibility, and thatre...
Source Code Conventions
“Clean Code” –Guidelines *              by Robert C. Martin•   use meaningful, pronounceable, searchable Names•   write co...
The “Must Read”-Book(s) by Robert C MartinA Handbook of AgileSoftwareCraftsmanship“Even bad code canfunction. But if codei...
TheKrzysztof Cwalina,Read”-Book(s)  by     “Must Brad AbramsFramework DesignGuidelines“teachesdevelopers thebest practices...
Clean Code Developer – 3rd Iteration     by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.deGraphic by M...
Information Hiding Principle           (IHP)
Information Hiding Principle (IHP)         by David Parnas (1972)“.. information hiding is the principle ofsegregation of ...
Liskov Substitution Principle           (LSP)
Liskov Substitution Principle (LSP)by Barbara Liskov, Jannette Wing (1994)“Liskov’s notion of a behavioral subtypedefines ...
Interfaces / Contracts• Decouple Usage and Implementation through introduction of a contract• Allows to replace implementa...
Dependency Inversion Principle           (DIP)
Dependency Inversion Principle (DIP)  by Robert C. Martin• “High-level modules should not depend on  low-level modules. Bo...
Clean Code Developer – 4th Iteration     by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.deGraphic by M...
Open Closed Principle       (OCP)
Open/Closed Principle (OCP)by Bertrand Meyer (1988)An implementation is open for extensionbut closed for modification     ...
Law of Demeter     (LoD)
Law of Demeter (LoD)           Northeastern University (1987)“• Each unit should have only limited knowledge  about other ...
S               Single Responsibility Principle   O               Open/Closed Principle   L               Liskov Substitut...
Clean Code Developer – 5th Iteration     by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.deGraphic by M...
Component-Oriented   Programming       (CoP)
Different Ways of doing something similarhttp://www.ericalbrecht.com                                                      ...
Why Reusable Components rockhttp://www.ericalbrecht.com/technic/8020/8020all.jpg
Inversion of Control        (IoC)
Inversion of Control (IoC)by Martin Fowler 1994      Logger logger = new Logger();
Inversion of Control (IoC)by Martin Fowler 1994      Logger logger = new Logger();
Inversion of Control –Constructor Injection http://www.martinfowler.com/articles/injection.html  public class ContactManag...
Dependency Injection Container & more• Typically support all types of Inversion of Control mechanisms   • Constructor Inje...
The “Must Read”-Book(s) by Mark SeemannDependencyInjection is a set ofsoftware designprinciples andpatterns thatenable us ...
Summary Clean CodeMaintainability is achieved through:• Readability (Coding Guidelines)• Simplification and Specialization...
Q&A                                         Downloads,                                         Feedback & Comments:       ...
http://clean-code-developer.com                                          References…http://michael.hoennig.de/2009/08/08/c...
… more ReferencesResharperhttp://www.jetbrains.com/resharper/FxCop / Code Analysishttp://msdn.microsoft.com/en-us/library/...
Please fill out theonline feedback, and…… thanks for you attention!        And visit and support the             Bay.net U...
Clean Code at Silicon Valley Code Camp 2011 (02/17/2012)
Upcoming SlideShare
Loading in...5
×

Clean Code at Silicon Valley Code Camp 2011 (02/17/2012)

3,221

Published on

Clean Code presentation for the Silicon Valley Code Camp 2011 based on the Clean Code Developer initiative created in Germany by Ralf Westphal & Stefan Lieser. Showing how to improve Code Quality by applying
Best Practices and Design Pattern while utilizing existing tools for better maintainable code.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,221
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
68
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Clean Code at Silicon Valley Code Camp 2011 (02/17/2012)

  1. 1. Silicon Valley Code Camp Clean CodeWhy Clean Code matters Foothill College, October 9nd 2011
  2. 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. 3. Overview• Why Clean Code• Tools • Resharper • FxCop, StyleCop & StyleCop plugin for Resharper • GhostDoc & Spell Checker • Code Contracts, Pex & Moles• Clean Code Developer Initiative• Principles and Practices• Code Comparison• Q&A
  4. 4. Does writing Clean Codemake us more efficient?
  5. 5. What is Clean Code?
  6. 6. Clean Code is maintainable Source code must be: • readable & well structured • extensible • testable
  7. 7. Software Engineering vs.Craftsmanship
  8. 8. The “Must Read”-Book(s) by Robert C MartinA Handbook of AgileSoftwareCraftsmanship“Even bad code canfunction. But if codeisn’t clean, it can bring adevelopmentorganization to itsknees.”
  9. 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
  10. 10. .NET Tools and their ImpactTool name Positive Impact Negative ImpactResharper compiling ++++ VS responsiveness --FxCop code quality ++ compiling time -StyleCop code consistency +++ compiling time -StyleCop plugin compiling time +++ VS responsiveness --for ResharperGhost Doc automated docs potentially worse docSpell Checker fewer spelling errors ++ performance --Code Contracts testability, quality ++ compiling time --Pex & Moles automated test ++ compiling time --
  11. 11. Resharper“The single most impacting development addition to Visual Studio” Features: – Code Analysis – Quick Fixes – Code Templates – Code Generation – Code Cleanup – Many, many more… http://www.jetbrains.com/resharper/
  12. 12. FxCop / Static Code Analysis Code Analysis: – Correctness – Library design – Internationalization and localization – Naming conventions – Performance – Security http://msdn.microsoft.com/en-us/library/3z0aeatx.aspx
  13. 13. Style Cop with R# Integration Code Consistency & Readability: – Automated check of C# coding standard – Enforceable at check-in with TFS check-in Policy – Full Integration in Resharper with Style Cop plugin: – Code Analysis – Quick Fixes – Code Cleanup
  14. 14. Ghost Doc• Save keystrokes and time• Simplify documenting your code• Benefit of the base class documentation http://submain.com/products/ghostdoc.aspx
  15. 15. Spell Checker • Spelll chicking for literals and comments in VShttp://visualstudiogallery.msdn.microsoft.com/7c8341f1-ebac-40c8-92c2-476db8d523ce/
  16. 16. • Design-by-Contract programming• Improved testability• Static verification• API documentation integration with Sandcastle http://msdn.microsoft.com/en-us/devlabs/dd491992
  17. 17. 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/
  18. 18. Graphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  19. 19. Clean Code Developer – 1st Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.deGraphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  20. 20. Keep it simple, stupid (KISS)
  21. 21. KISS-Principle – “Keep It Simple Stupid” by Kelly Johnson http://blogs.smarter.com/blogs/Lego%20Brick.jpg
  22. 22. The Power of Simplicity Graphic by Nathan Sawaya courtesy of brickartist.comGraphic by Nathan Sawaya courtesy of brickartist.com http://www.geekalerts.com/lego-iphone/
  23. 23. Graphic by Nathan Sawaya courtesy of brickartist.com
  24. 24. Don’t repeat yourself (DRY)
  25. 25. Don’t repeat yourself (DRY) by Andy Hunt and Dave Thomas in their book “The Pragmatic Programmer”// Code Copy and Paste Method // DRY Methodpublic 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(); } }
  26. 26. Clean Code Developer – 2nd Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.deGraphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  27. 27. Separation of Concerns (SoC) Single Responsibility Principle (SRP)
  28. 28. The Producthttp://www.technicopedia.com/8865.html
  29. 29. Component / Service http://www.technicopedia.com/8865.html
  30. 30. Class, Struct, Enum etc.http://technicbricks.blogspot.com/2009/06/tbs-techpoll-12-results-2009-1st.html
  31. 31. Separation of Concerns (SoC) probably by Edsger W. Dijkstra in 1974• “In computer science,separation of concerns (SoC) isthe process of separating acomputer program into distinctfeatures that overlap infunctionality as little as possible.•A concern is any piece ofinterest or focus in a program.Typically, concerns aresynonymous with features orbehaviors. “ http://en.wikipedia.org/wiki/Separati on_of_Concerns
  32. 32. Single Responsibility Principle (SRP) by Robert C Martin“Every object should have a single responsibility, and thatresponsibility should be entirely encapsulated by the class.” http://en.wikipedia.org/wiki/Single_responsibility_principlepublic class Logger : ILogger{ public Logger(ILoggingSink loggingSink) {} public void Log(string message) {}} http://www.ericalbrecht.com
  33. 33. Source Code Conventions
  34. 34. “Clean Code” –Guidelines * by Robert C. Martin• use meaningful, pronounceable, searchable Names• write code readable top to bottom (Journal Style)• prefer Exceptions over returning Error Codes• explain yourself in Code• avoid redundant, misleading and noise Comments• don’t use a Comment when you can use a Method or Variable• Avoid commented-out code and Javadocs in NonPublic Code• Don’t Return or Pass Null• Keep Tests Clean and have only One Assert per Test• Classes and Methods should be small• Limit the scope of Data and use Copies of Data• Builds and Tests should only require 1 Step * Chapter extract: Robert C. Martin –” Clean Code”, Parson Education, Inc. 2008
  35. 35. The “Must Read”-Book(s) by Robert C MartinA Handbook of AgileSoftwareCraftsmanship“Even bad code canfunction. But if codeisn’t clean, it can bring adevelopmentorganization to itsknees.”
  36. 36. TheKrzysztof Cwalina,Read”-Book(s) by “Must Brad AbramsFramework DesignGuidelines“teachesdevelopers thebest practices fordesigning reusablelibraries for theMicrosoft .NETFramework.”
  37. 37. Clean Code Developer – 3rd Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.deGraphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  38. 38. Information Hiding Principle (IHP)
  39. 39. Information Hiding Principle (IHP) by David Parnas (1972)“.. information hiding is the principle ofsegregation of the design decisions on acomputer program that are most likely tochange, ..” http://en.wikipedia.org/wiki/Information_hiding
  40. 40. Liskov Substitution Principle (LSP)
  41. 41. Liskov Substitution Principle (LSP)by Barbara Liskov, Jannette Wing (1994)“Liskov’s notion of a behavioral subtypedefines a notion of substitutability formutable objects” http://en.wikipedia.org/wiki/Liskov_substitution_principle
  42. 42. Interfaces / Contracts• Decouple Usage and Implementation through introduction of a contract• Allows to replace implementation without changing the consumerpublic interface ILogger public class Logger : ILogger{ { void Log(string message); public Logger(ILoggingSink loggingSink)} {} public void Log(string message) {} }
  43. 43. Dependency Inversion Principle (DIP)
  44. 44. 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
  45. 45. Clean Code Developer – 4th Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.deGraphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  46. 46. Open Closed Principle (OCP)
  47. 47. Open/Closed Principle (OCP)by Bertrand Meyer (1988)An implementation is open for extensionbut closed for modification http://en.wikipedia.org/wiki/Open/closed_principle
  48. 48. Law of Demeter (LoD)
  49. 49. 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
  50. 50. S Single Responsibility Principle O Open/Closed Principle L Liskov Substitution Principle I Interface Segregation Principle D Dependency Inversion PrincipleRobert C Martin: http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod
  51. 51. Clean Code Developer – 5th Iteration by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.deGraphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/
  52. 52. Component-Oriented Programming (CoP)
  53. 53. Different Ways of doing something similarhttp://www.ericalbrecht.com http://www.julianaheng.com/transformers-rotf-bumblebee- and-sam-action-figures/ http://www.ericalbrecht.com
  54. 54. Why Reusable Components rockhttp://www.ericalbrecht.com/technic/8020/8020all.jpg
  55. 55. Inversion of Control (IoC)
  56. 56. Inversion of Control (IoC)by Martin Fowler 1994 Logger logger = new Logger();
  57. 57. Inversion of Control (IoC)by Martin Fowler 1994 Logger logger = new Logger();
  58. 58. Inversion of Control –Constructor Injection http://www.martinfowler.com/articles/injection.html public class ContactManager : IContactManager { public ContactManager(ILogger logger, IContactPersistence contactPersistence) { this.logger = logger; if (logger == null) { throw new ArgumentNullException("logger"); } … } }
  59. 59. Dependency Injection Container & more• Typically support all types of Inversion of Control mechanisms • Constructor Injection • Property (Setter) Injection • Interface Injection • Service Locator•.NET based DI-Container • Unity • Castle Windsor • StructureMap Related Technology: • Spring.NET • Managed Extensibility Framework (MEF) • Autofac • Windows Communication Foundation (WCF) • Puzzle.Nfactory • Ninject • PicoContainer.NET • and more
  60. 60. The “Must Read”-Book(s) by Mark SeemannDependencyInjection is a set ofsoftware designprinciples andpatterns thatenable us todevelop looselycoupled code. http://www.manning.com/seemann/
  61. 61. Summary Clean CodeMaintainability 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!)
  62. 62. Q&A Downloads, Feedback & Comments: theo@csharp-lighthouse.com www.csharp-lightouse.com www.speakerrate.com/theojGraphic by Nathan Sawaya courtesy of brickartist.com
  63. 63. http://clean-code-developer.com References…http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOodhttp://www.manning.com/seemann/http://en.wikipedia.org/wiki/Keep_it_simple_stupidhttp://picocontainer.org/patterns.htmlhttp://en.wikipedia.org/wiki/Separation_of_concernshttp://en.wikipedia.org/wiki/Single_responsibility_principlehttp://en.wikipedia.org/wiki/Information_hidinghttp://en.wikipedia.org/wiki/Liskov_substitution_principlehttp://en.wikipedia.org/wiki/Dependency_inversion_principlehttp://en.wikipedia.org/wiki/Open/closed_principlehttp://en.wikipedia.org/wiki/Law_Of_Demeterhttp://en.wikipedia.org/wiki/Dont_repeat_yourselfhttp://en.wikipedia.org/wiki/You_aint_gonna_need_ithttp://en.wikipedia.org/wiki/Component-oriented_programminghttp://en.wikipedia.org/wiki/Service-oriented_architecturehttp://www.martinfowler.com/articles/injection.htmlhttp://www.codeproject.com/KB/aspnet/IOCDI.aspxhttp://msdn.microsoft.com/en-us/magazine/cc163739.aspxhttp://msdn.microsoft.com/en-us/library/ff650320.aspxhttp://msdn.microsoft.com/en-us/library/aa973811.aspxhttp://msdn.microsoft.com/en-us/library/ff647976.aspxhttp://msdn.microsoft.com/en-us/library/cc707845.aspxhttp://msdn.microsoft.com/en-us/library/bb833022.aspxhttp://unity.codeplex.com/http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=5&tabid=11
  64. 64. … more ReferencesResharperhttp://www.jetbrains.com/resharper/FxCop / Code Analysishttp://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspxhttp://blogs.msdn.com/b/codeanalysis/http://www.binarycoder.net/fxcop/index.htmlCode Contractshttp://msdn.microsoft.com/en-us/devlabs/dd491992http://research.microsoft.com/en-us/projects/contracts/Pex & Molehttp://research.microsoft.com/en-us/projects/pex/StyleCophttp://stylecop.codeplex.com/Ghostdochttp://submain.com/products/ghostdoc.aspxSpellcheckerhttp://visualstudiogallery.msdn.microsoft.com/7c8341f1-ebac-40c8-92c2-476db8d523ce// Lego (trademarked in capitals as LEGO)
  65. 65. Please fill out theonline feedback, and…… thanks for you attention! And visit and support the Bay.net User Group http://baynetug.org
  1. Gostou de algum slide específico?

    Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

×