Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clean Code Developer Green Grade

Mark Paluch
September 18, 2013

Clean Code Developer Green Grade

Learn the principles and practices of Clean Code Development. This is the second session in the Clean Code Development track. Go on with the next level and dig into Clean Code.

Mark Paluch

September 18, 2013
Tweet

More Decks by Mark Paluch

Other Decks in Programming

Transcript

  1. Clean Code Developer A way to better code Session 04

    https://github.com/mp911de/CCD Mittwoch, 18. September 13
  2. Yellow Grade • Principles • Open Closed Principle (OCP) •

    Tell, don‘t ask • Law of Demeter • Practices • Continuous Integration • Static Code analysis • Inversion of Control Container (IoC) • Share experience • Count Bugs Mittwoch, 18. September 13
  3. Open Closed Principle (OCP) • Minimize risk of breaking a

    system through changes • Open for extension • Closed for modification • Violations happen mostly on conditional processing • Solution in most cases: Replace conditional with Strategy Mittwoch, 18. September 13
  4. Open Closed Principle (OCP) broken by • Switch Case •

    Checked Exceptions Mittwoch, 18. September 13
  5. Open Closed Principle, Bad Code public double calculatePrice() { const

    decimal Rebatte = 0.95; switch(customerType) { case CustomerType.NEW: return amount * singleUnitPrice; case CustomerType.EXISTING: return amount * singleUnitPrice * Rebatte; default: throw new IllegalArgumentException(); } } Mittwoch, 18. September 13
  6. Open Closed Principle, Good Code public interface ICalculator{ double calculatePrice(int

    amount, double singleUnitPrice); } private ICalculator calculator; public double calculatePrice () { return calculator.calculatePrice(amount, singleUnitPrice); } public class NewCustomer extends ICalculator { public double calculatePrice(int amount, double singleUnitPrice){ return amount * singleUnitPrice; } } public class ExistingCustomer extends ICalculator { const decimal Rebatte = 0.95; public double calculatePrice(int amount, double singleUnitPrice){ return amount * singleUnitPrice * Rebatte; } } Mittwoch, 18. September 13
  7. Tell, don‘t ask • Context: Object and Data flow •

    A target object should be told, what to do, data has to be provided • Opposite: A object should not ask for data by it self • Goal: Create „dumb“ data objects, which are passed to functions Mittwoch, 18. September 13
  8. Law of Demeter • Don‘t talk to Strangers • Reduce

    dependencies (cohesion) to other classes • Avoid train wrecks int margin = config.getPages().getMargins().getLeft(); Mittwoch, 18. September 13
  9. Law of Demeter Rules • Use/Talk only to • Methods

    of own class • Parameters of own method • Methods of associated classes • Methods of self created objects Mittwoch, 18. September 13
  10. Continuous Integration • Early Integration forces Bugs to occur earlier

    • Continuous Integration/assembly/check/build reduce Bugs and Risks • Automated Builds perform sanity and quality checks, can run automated tests • Provides safety about your work after every commit Mittwoch, 18. September 13
  11. Continuous Integration possibilities • A build server can do a

    lot more for you • Static code analysis • Package assembly • Automated deployment Mittwoch, 18. September 13
  12. Static Code analysis • Measure quality through a ruleset •

    Produce clean code • It‘s like an automated Code-Reviewer • Example: Sonar Dashboards • Dynamic Code analysis: Unit/Selenium Tests Mittwoch, 18. September 13
  13. Implementing Static Code analysis • Ideal with Continuous Integration •

    Define violations • Find violations • Fix violations Mittwoch, 18. September 13
  14. Inversion of Control Container • Decoupling of dependencies • Dependencies

    are „configured“ and not „coded“ • Dependencies are managed externally • Ideal also for configuration data • Frameworks needed, not sth. to quick hack • Provides Object lifecycle management, ... Mittwoch, 18. September 13
  15. Inversion of Control Containers • Java: Spring, J2EE (EJB‘s), Guice

    • PHP: Stubbles, Xconf, FLOW3 • .NET: Spring.NET, Unity, NInject Mittwoch, 18. September 13
  16. IoC Spring Example <bean id="addressChecker2WebService" class="org....JaxWsPortProxyFactoryBean"> <property name="serviceInterface" value="com...AddressChecker" />

    <property name="wsdlDocumentUrl" value="classpath:wsdl/ AddressChecker2Service.wsdl" /> <property name="namespaceUri" value="http://oneandone.com/AddressChecker" /> <property name="serviceName" value="AddressCheckerEJBBeanService" /> <property name="portName" value="AddressCheckerPort" /> <property name="endpointAddress" value="${addresschecker.soap.url}" /> <property name="customProperties" ref="webServiceProperties" /> </bean> Mittwoch, 18. September 13
  17. Share experience • Be a mentor • Share your knowledge

    • Tell, present, write • Study new technologies, speak about them • Support your co-workers Mittwoch, 18. September 13
  18. Counting Bugs • Measure Bugs after Release/Iteration/Sprint • Analyze their

    cause • Think about prevention • Don‘t measure coding Bugs while development, write Unit Tests Mittwoch, 18. September 13
  19. Image Credits Table of Contents: © by Michael Kappel http://michaelkappel.com

    Bands: © by Alexander Herzog Mittwoch, 18. September 13