Keypoints- Guide: writingtestablecode<br />Software craftsmanship in kraków<br />
Constructor does Real Work<br /><ul><li>Warning Signs:
„new” keyword in constructor or at field declaration.
Static method calls in a constructor or at field declaration.
Anything more then field assignment in constructors.
Object no fully initialized after the constructor finishes.
Control flow (conditionals or looping logic) in a constructor.
Class does complex object graph construction inside a constructor rather than using a factory or builder.
Adding or using initialization block. </li></li></ul><li>Constructor does Real Work<br /><ul><li>Fixing the flaw:
Upcoming SlideShare
Loading in …5
×

Key points guide writing testable code

827 views
777 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
827
On SlideShare
0
From Embeds
0
Number of Embeds
48
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Key points guide writing testable code

  1. 1. Keypoints- Guide: writingtestablecode<br />Software craftsmanship in kraków<br />
  2. 2. Constructor does Real Work<br /><ul><li>Warning Signs:
  3. 3. „new” keyword in constructor or at field declaration.
  4. 4. Static method calls in a constructor or at field declaration.
  5. 5. Anything more then field assignment in constructors.
  6. 6. Object no fully initialized after the constructor finishes.
  7. 7. Control flow (conditionals or looping logic) in a constructor.
  8. 8. Class does complex object graph construction inside a constructor rather than using a factory or builder.
  9. 9. Adding or using initialization block. </li></li></ul><li>Constructor does Real Work<br /><ul><li>Fixing the flaw:
  10. 10. Do not create collaborators in your constructor, but pass them in.
  11. 11. Use dependency injection framework to inject class’ dependencies.
  12. 12. Use Builder or a Factory class that create object graph.</li></ul>Don’t look for things! Ask for things!<br />
  13. 13. Digging into collaborators<br /><ul><li>Warning sings:
  14. 14. Objects are passed in but never used directly (only used to get access to other objects).
  15. 15. Law of Demeter violation (more then one dot in a method/property chaining).
  16. 16. Suspicious names: context, environment, principal, container or manager.</li></ul>Law of Demeter = Principle of Least Knowledge<br />
  17. 17. Digging into collaborators<br /><ul><li>Fixing the flaw:
  18. 18. Only talk to your immediate friends.
  19. 19. Inject (pass in) the more specific object that you really need.
  20. 20. Leave the object location and configuration responsibility to the caller i.e. the factory or DI framework.</li></li></ul><li>Brittle global state and singletons<br /><ul><li>Warning signs:
  21. 21. Adding or using singletons.
  22. 22. Adding or using static fields or static methods.
  23. 23. Adding or using static .initialization blocks.
  24. 24. Adding or using registries.
  25. 25. Adding or using service locators.</li></ul>Singleton is a global state!<br />
  26. 26. Brittle global state and singletons<br /><ul><li>Fixing the flaw:
  27. 27. Use Dependency Injection.
  28. 28. If you need global state use DI framework.
  29. 29. Create adapter for classes that expose static methods (you’ll be able to use test-doubles).</li></li></ul><li>Class doestoo much<br /><ul><li>Warning Signs:
  30. 30. Summing up what the class does includes the word „and”.
  31. 31. Class would be challenging for new team members to read and quickly „get it”.
  32. 32. Class has fields that are only used in some methods.
  33. 33. Class has static methods that only operate on parameters.</li></li></ul><li>Class doestoo much<br /><ul><li>Fixing the flaw:
  34. 34. Split up class by identifying individual responsibilities.
  35. 35. For legacy code create classes that wrap different responsibilities of legacy „god” class.</li>

×