6. Code Qualities
IN YOUR ZONE
6
Testability
No Redundancy
Encapsulation
Strong Cohesion
Correct Coupling
Readability
Focus
7. Write Tests First
•Best way to ensure Testability
•Helps avoid Redundancy – redundant test reveal redundant feature
•Helps you determine what can and cannot be Encapsulated
•Increases Cohesion - it is easier to write tests for classes with one
responsibility
•Promotes Decoupling naturally
•Tends to improve Readability – code is written with a clear idea of what is
supposed to do; refactoring
•Testing all the variations of a concept, they will tend to be Focused, because it
is easier to test them if they are
IN YOUR ZONE 7
9. Code by Intention
•Improves Testability - a well-defined intention is easier to test
•Eliminates Redundancies
•Promotes Encapsulation – less-complex methods encapsulate their
local variables from each other
•Increases Cohesion - if the intention is about one thing
•Assists Correct Coupling - the calling routine is written only to the
interface
•Improves Readability - methods are well-named
IN YOUR ZONE 9
10. Write Clearly
•Ensure that people understand what the method/class does
•Use intention revealing names
•Follow coding standards
•Improves Testability – clearer code is easier to understand and will
make clearer tests
IN YOUR ZONE 10
11. Encapsulate by Convention,
Reveal by Need
•Ease Testing – encapsulated code needs no unit tests
•Encapsulating Construction helps No Redundancy
•Ensures the greatest level of Encapsulation possible
•Leads to Correct Coupling – that which is hidden cannot possibly be
coupled to
IN YOUR ZONE 11
16. Pull Out Things That Vary
•Promotes Encapsulation of type behind an interface
•Increases Cohesion by eliminating varying function
•Helps keep Correct Coupling – eliminates the opportunity to couple to
state or private functions of the client class
IN YOUR ZONE 16
18. Treat Conceptually Similar
Things the Same Way
•Improves Testability – test individual pieces, how they react and how
you decide which pieces you have
•Improves Encapsulation – the different implementations of variations
•Improves Correct Coupling – client is no longer coupled to the specific
types
•Improves Focus – it relates the different implementations together
with their common interface
IN YOUR ZONE 18
21. Favor Composition over
Class Inheritance
•Improves Testability – separate classes can be tested apart from the
client classes
•Helps eliminate Redundancy – allows for reuse of pulled out objects
•Promotes the Encapsulation of type – variations are hidden
•Increase class Cohesion
•Eliminates the possibility of inheritance Coupling
IN YOUR ZONE 21
22. Design to Interfaces
•Helps make code Testable – there will tend to be correct coupling and
fewer side effects to test for
•Is inherently Encapsulating
•Could increase Cohesion – by seeking to design simple interfaces
•Avoid Identity Coupling – since implementation details are not
considered
•Promotes Readability – by promoting intention-revealing names for
classes and methods
IN YOUR ZONE 22
24. Separate Use from
Construction
•Promotes Testability – factories making all construction decisions can
return ‘test versions’ of objects
•Promotes Encapsulation of design
•Promotes Cohesion – separate users by builders
•Helps keep clients Decoupled from the specific classes they are using,
since they do not have to build them
•Aids Focus – the construction of objects are concentrated in one or
more related places
IN YOUR ZONE 24
25. Refactor Code as Needed
•It is part of TDD
•Often results in elimination of Redundancy of state or behavior – ‘Pull
up Method’, ‘Pull up Field’, ‘Form Template Method’
•Often strengthens Encapsulation
•Improves Cohesion – ‘Extract Method’, ‘Move Method’
•Improves Coupling – ‘Replace Conditional with Polymorphism’
•Tends to improve Readability – generally simplifies and beautifies code
IN YOUR ZONE 25
26. Limit Yourself to One Perspective
in a Class or Method
•Have a class/method operate either at the Conceptual level or
Implementation level helps understandability
•Aids Testability – one set of tests can focus on implementation while
another focuses on the way these implementations are used
•Improves Cohesion – limited to one perspective of a responsibility
IN YOUR ZONE 26
27. Other Best Practice
•Do Not Do More Than You Need:
• Save time to work on things you do need
• Smarter to implement at the point in the future when it is needed
• Allows you to focus on those things you do need to do
• If do you need it later, more of the system will be built that should
make writing the function easier
IN YOUR ZONE 27
28. Conclusion
Testability could be achieved with:
IN YOUR ZONE
28
• Write Tests First
• Code by Intention
• Write Clearly
• Encapsulate by Convention, Reveal by Need
• Avoid Redundancy
• Treat Conceptually Similar Things the Same Way
• Favor Composition over Class Inheritance
• Design to Interfaces
• Separate Use from Construction
• Refactor Code as Needed
• Limit Yourself to One Perspective in a Class or Method
29. Conclusion
No Redundancy could be achieved with:
IN YOUR ZONE
29
• Write Tests First
• Code by Intention
• Encapsulate by Convention, Reveal by Need
• Avoid Redundancy
• Favor Composition over Class Inheritance
• Refactor Code as Needed
30. Conclusion
Encapsulation could be achieved with:
IN YOUR ZONE
30
• Write Tests First
• Code by Intention
• Encapsulate by Convention, Reveal by Need
• Pull Out Things That Vary
• Treat Conceptually Similar Things the Same Way
• Favor Composition over Class Inheritance
• Design to Interfaces
• Separate Use from Construction
• Refactor Code as Needed
31. Conclusion
Strong Cohesion could be achieved with:
IN YOUR ZONE
31
• Write Tests First
• Code by Intention
• Pull Out Things That Vary
• Favor Composition over Class Inheritance
• Design to Interfaces
• Separate Use from Construction
• Refactor Code as Needed
• Limit Yourself to One Perspective in a Class or Method
32. Conclusion
Correct Coupling could be achieved with:
IN YOUR ZONE
32
• Write Tests First
• Code by Intention
• Encapsulate by Convention, Reveal by Need
• Pull Out Things That Vary
• Treat Conceptually Similar Things the Same Way
• Favor Composition over Class Inheritance
• Design to Interfaces
• Separate Use from Construction
• Refactor Code as Needed
33. Conclusion
Readability could be achieved with:
IN YOUR ZONE
33
• Write Tests First
• Code by Intention
• Write Clearly
• Design to Interfaces
• Refactor Code as Needed
34. Conclusion
Focus could be achieved with:
IN YOUR ZONE
34
• Write Tests First
• Encapsulate by Convention, Reveal by Need
• Avoid Redundancy
• Treat Conceptually Similar Things the Same Way
• Separate Use from Construction
35. Recommendation
IN YOUR ZONE
35
• Essential Skills for the Agile Developer: A Guide to Better Programming and Design (29 Aug
2011), by Alan Shalloway , Scott Bain, Ken Pugh, Amir Kolsky
• James Shore: Evolutionary Design Illustrated -
http://ndc2011.macsimum.no/mp4/Day1%20Wednesday/Track4%201740-1840.mp4
36. Remus-Constantin Langu | Senior Developer
Remus.Langu@endava.com
Skype remus.constantin.langu
thank you
IN YOUR ZONE 36