Bra kod luktar inte, den doftar Data-Tjej, 2014-02-06 - Linköping Daniel Deogun Twitter: @DanielDeogun
Daniel Deogun Senior konsult & konsultchef på Omegapoint i Stockholm! Arbetat i drygt 6 år med patientkritisk mjukvara (pacemaker- och ICD-system) Arbetat med agila utvecklingsprocesser sedan 2005 (Scrum och Kanban)! Arbetat med Domain Driven Design (DDD) och Test Driven Development (TDD) i ca 9 år! En av Omegapoints kursledare i DDD och TDD! Nuvarande uppdrag! Lead developer på Hi3G Access AB
Bakgrund Varför uppstår... [1] [1] http://upload.wikimedia.org/wikipedia/commons/9/9d/graduation_thinker_lumaxart.jpg By lumaxart (EbonyG00052_LuMaxArt) [CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0) or CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0)], via Wikimedia Commons
Bakgrund Varför uppstår... Null Pointer Exception [1] [1] http://upload.wikimedia.org/wikipedia/commons/9/9d/graduation_thinker_lumaxart.jpg By lumaxart (EbonyG00052_LuMaxArt) [CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0) or CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0)], via Wikimedia Commons
Bakgrund Varför uppstår... Null Pointer Exception [1] Wrong Argument Order [1] http://upload.wikimedia.org/wikipedia/commons/9/9d/graduation_thinker_lumaxart.jpg By lumaxart (EbonyG00052_LuMaxArt) [CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0) or CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0)], via Wikimedia Commons
Bakgrund Varför uppstår... Illegal Input Null Pointer Exception [1] Wrong Argument Order [1] http://upload.wikimedia.org/wikipedia/commons/9/9d/graduation_thinker_lumaxart.jpg By lumaxart (EbonyG00052_LuMaxArt) [CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0) or CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0)], via Wikimedia Commons
Bakgrund Varför uppstår... Illegal Input Null Pointer Exception [1] Wrong Argument Order Oväntade beteenden [1] http://upload.wikimedia.org/wikipedia/commons/9/9d/graduation_thinker_lumaxart.jpg By lumaxart (EbonyG00052_LuMaxArt) [CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0) or CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0)], via Wikimedia Commons
Bakgrund Varför uppstår... Illegal Input Null Pointer Exception [1] Wrong Argument Order Oväntade beteenden Komplex defensiv kod [1] http://upload.wikimedia.org/wikipedia/commons/9/9d/graduation_thinker_lumaxart.jpg By lumaxart (EbonyG00052_LuMaxArt) [CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0) or CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0)], via Wikimedia Commons
Requirements Möte Ett möte har en titel, start- och sluttid, en sammankallande och minst en inbjuden deltagare. Ett mötesrum får inte ha multipla möten under samma tidsperiod. När ett rum bokats skall samtliga inbjudna deltagare notifieras.
Code Review...
Code Review...
Code Review...
Code Review...
Code Review...
Code Review...
Code Review...
The Parameter Order Bug If a method signature contains multiple arguments of the same type, then there s a risk that parameter values get interchanged upon method invocation Example,!! Then the following invocations are valid from a complier perspective but semantically incorrect! setname(string first, String last) setname( Daniel, Deogun ); setname( Deogun, Daniel );
Strongly Typed Arguments One way to avoid the parameter order bug is to use strongly typed arguments! Example,!!! Hence, invoking the method with incorrect parameter order will yield a compiler error! setname(firstname first, LastName last)
Let s apply Strongly Typed Arguments
Let s apply Strongly Typed Arguments
Let s apply Strongly Typed Arguments
Let s Take a Look At... Dependencies
Definition of Dependency Merriam-Webster! [de pen den cy] noun \-dən(t)-sē\ : the quality of being dependent! [de pen dent] adjective \di-ˈpen-dənt\ : needing someone or something else for support, help, etc.
Dependency in Code Given a class A and class B! When class A needs logic in class B to execute! Then class A has a dependency to class B! A depends on B
Identifying Dependencies
Identifying Dependencies
Identifying Dependencies
Identifying Dependencies
Identifying Dependencies
Identifying Dependencies
Identifying Dependencies
Identifying Dependencies
Identifying Dependencies
Identifying Dependencies
Why does one need to study dependencies? Studying dependencies helps one to understand how tightly coupled the code is! Dependencies may complicate testing and code reuse! So how should one treat dependencies?
Dependency Injection The basic idea of Dependency Injection (DI) is to separate the assembly of an object from the client with the dependency By this way, the client becomes unaware of the implementation and only needs to focus on the API Assembler builds Object injects Client depends on
Let s apply Dependency Injection
Let s apply Dependency Injection
Let s apply Dependency Injection
Let s apply Dependency Injection
Let s apply Dependency Injection
Let s Take a Look At... Design by Contract
Design By Contract The term was coined by Bertrand Meyer when designing the Eiffel programming language back in 1986! The main idea of DbC is the collaboration between software elements where mutual obligations and benefits exist! Let s look at an example...
Design By Contract Assume we have the following method! public void register(listener listener) What do we expect to happen when A. register(new Listener()) B. register(null)! If the call to register returns without an exception, what can be assumed?
Design By Contract If we use DbC, then the expectations and assumptions are expressed as Require and Ensurements! Hence, the method register could look like
Design By Contract If we use DbC, then the expectations and assumptions are expressed as Require and Ensurements! Hence, the method register could look like
Let s apply Design By Contract
Let s apply Design By Contract
Let s apply Design By Contract
Let s apply Design By Contract
Let s apply Design By Contract
Wow, Hold Your Horses
Wow, Hold Your Horses
Wow, Hold Your Horses
Wow, Hold Your Horses
Let s Take a Look At... Law of Demeter
Law of Demeter The Law of Demeter was proposed at Northeastern University back in 1987! The fundamental idea may be expressed as Each unit should only talk to its friends; don't talk to strangers.! - http://en.wikipedia.org/wiki/law_of_demeter
Law of Demeter Hence, one should never reach through an object for another object s functionality! Damian Guy tweeted back in 2009! Every time a mock returns a mock, a fairy dies! - Twitter @damianguy 2009 Oct 19 Dan Bergh Johnsson has written an excellent post on his blog Dear Junior - Letters to a junior programmer on how this affect testability ( Demeter Saves Mocking Fairies 16 nov 2009)
Let s Apply Law of Demeter
Let s Apply Law of Demeter
Let s Apply Law of Demeter
Let s Apply Law of Demeter
Let s Apply Law of Demeter
Let s Apply Law of Demeter
Let s Take a Look At... Tell, Don t Ask
Tell, Don t Ask Martin Fowler summarizes the Tell, Don t Ask principle in the following way! Tell-Don't-Ask is a principle that helps people remember that object-orientation is about bundling data with the functions that operate on that data. It reminds us that rather than asking an object for data and acting on that data, we should instead tell an object what to do. This encourages to move behavior into an object to go with the data.! - http://martinfowler.com/bliki/telldontask.html
Let s Apply Tell, Don t Ask
Let s Apply Tell, Don t Ask
Let s Apply Tell, Don t Ask
Let s Apply Tell, Don t Ask
Let s Apply Tell, Don t Ask
Let s Apply Tell, Don t Ask
Requirements Möte Ett möte har en titel, start- och sluttid, en sammankallande och minst en inbjuden deltagare. Ett mötesrum får inte ha multipla möten under samma tidsperiod. När ett rum bokats skall samtliga inbjudna deltagare notifieras.
Let s Refactor...
Let s Refactor...
Let s Refactor...
Så vad har vi lärt oss?
Så vad har vi lärt oss? Strongly Typed Objects
Så vad har vi lärt oss? Dependency Injection Strongly Typed Objects
Så vad har vi lärt oss? Dependency Injection Design by Contract Strongly Typed Objects
Så vad har vi lärt oss? Dependency Injection Design by Contract Strongly Typed Objects Law of Demeter
Så vad har vi lärt oss? Dependency Injection Design by Contract Strongly Typed Objects Tell, Don t Ask Law of Demeter
Is this a Silver Bullit? Detta till trots så kan oväntade beteenden förekomma!! Vanliga problem
Is this a Silver Bullit? Detta till trots så kan oväntade beteenden förekomma!! Vanliga problem Strongly Typed Objects Många typer Konstiga typhierarkier kan uppstå
Is this a Silver Bullit? Detta till trots så kan oväntade beteenden förekomma!! Vanliga problem Dependency Injection Felaktiga beroenden Onödiga tillstånd Shared mutable state
Is this a Silver Bullit? Detta till trots så kan oväntade beteenden förekomma!! Vanliga problem Design by Contract Avsaknad av kontrakt Svaga kontrakt Felaktiga kontrakt
Is this a Silver Bullit? Detta till trots så kan oväntade beteenden förekomma!! Vanliga problem Law of Demeter Duplicerade API:er
Is this a Silver Bullit? Detta till trots så kan oväntade beteenden förekomma!! Vanliga problem Tell, Don t Ask Kan upplevas som svårt
Is this a Silver Bullit? Detta till trots så kan oväntade beteenden förekomma!! Vanliga problem Generellt Felaktig exekveringsordning
Resultat
Resultat
Q & A http://commons.wikimedia.org/wiki/file:query-road.png
Tack! Twitter: @DanielDeogun