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

Coupling, Cohesion, Connascence

Coupling, Cohesion, Connascence

making better design choices writing code

josh_robb

June 12, 2014
Tweet

More Decks by josh_robb

Other Decks in Technology

Transcript

  1. The Plan Design? What is it? What is good design?

    A look at two old ideas One more old idea. Wrap up
  2. What is Good Design? Simplicity? Modularity? SOLID? Loosely Coupled/High Cohesion

    Can understand 1 piece without understanding others.
  3. Responsive Design “The more experience a programmer has, the more

    design ideas he knows. The temptation is to put these design ideas in the system now because you just know you’ll need them eventually. http://pragprog.com/magazines/2009-09/responsive-design
  4. Responsive Design “The more experience a programmer has, the more

    design ideas he knows. The temptation is to put these design ideas in the system now because you just know you’ll need them eventually. http://pragprog.com/magazines/2009-09/responsive-design
  5. Responsive Design “Over-designing early leads to delaying feedback from real

    usage of the system, makes adding features more complicated, and makes adapting the design more difficult.
  6. Responsive Design “By the same token, under-designing makes adding features

    more complicated, increases defects, and makes adapting the design more difficult.
  7. Responsive Design Some design needs to be done in advance

    of coding, but over the life of the project most design will be done in response to the changing needs of the system and the changing and growing understanding of the developers.”
  8. Cohesion “Attempting to divide a cohesive module would only result

    in increased coupling and decreased readability.”
  9. Coupling “In a very real sense, isolated testing done test-first

    exposes design mistakes before they're made. It translates coupling distributed throughout the module into mock setup centralized in the test, and it does that before the coupling is even written down.” Gary Bernhardt
  10. “Mock setup exposes coupling, remember; not cohesion or other design

    properties.” Gary (again) Read the whole thing! https://www.destroyallsoftware.com/blog/2014/test-isolation-is-about-avoiding- mocks Coupling
  11. SOLID Principles TOO ABSTRACT/High Level How do I write: this

    line of code? this method this class this feature?
  12. SOLID Principles SOLID is a useful didactic tool but a

    useless decision making tool. Josh Robb (right now)
  13. SOLID Principles Single Reason to Change principle There should never

    be more than one reason for a class to change Uncle Bob
  14. Liskov “What is wanted here is something like the following

    substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T”
  15. Liskov “What is wanted here is something like the following

    substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T”
  16. How do I anticipate change? You can’t! Trying to is

    a really bad mistake! “Duplication is far cheaper than the wrong abstraction” Sandi Metz
  17. SOLID - Recap “In short: pop cultural ideas, such as

    testability, readability and elegance, are easy to follow but hard to communicate across observers. Then again, object-oriented design principles tend to be easy to communicate, but hard to follow.
  18. SOLID - Recap “Internalizing the reason for failure easily drives

    them into a vicious loop of self-blame: “this should work, I’ve been told that SOLID/GRASP is a great idea, so it must be my fault that I don’t see how and why this still fails.”
  19. “Comparing techniques by means of encapsulation and connascence” Communications of

    the ACM Special issue on analysis and modeling in software development CACM Volume 35 Issue 9, Sept. 1992
  20. Connascence 1. The common birth of two or more at

    the same time; 2. That which is born or produced with another. 3. The act of growing together.
  21. Connascence “Connascence is a software quality metric to allow reasoning

    about the complexity caused by dependency relationships in object oriented design”
  22. Tries to answer design questions: e.g. “Prefer delegation over inheritance”

    But why does delegation introduce less coupling then inheritance.
  23. Inheritance is evil? abstract class ProfileRequest { Profile Profile {

    get; set; } } class CreateProfileRequest : ProfileRequest {} class UpdateProfileRequest : ProfileRequest{ int ProfileId { get; set; } }
  24. Inheritance is evil? “It's often best to wait a bit

    longer than is comfortable, hoping that tolerating interim duplication will produce code that gives you better information about the problem as a whole.
  25. Inheritance is evil? “when you DRY out duplication, you're declaring

    that these two bits of identical code really do represent identical concepts. If you're wrong about this it can be very difficult to extend code later. - Sandi Metz
  26. Taxonomy • Name • Position • Meaning • Algorithm •

    Type • Execution • Timing • Value • Identity • Degree • Locality • Stability Principles
  27. Strength/Degree • Name • Position • Meaning • Algorithm •

    Type • Execution • Timing • Value • Identity Weaker Stronger Degree
  28. Con of Name “Doesn’t R# fix this?” (or IDEA or

    Eclipse #lolwat) Well - yes - thats the point - it’s the weakest
  29. Con of Algorithm Routes Map: Url -> Controller/Action /login ->

    AuthController.Login() Views Generate Url. <a href=”/login”>Log In</a>
  30. Con of Algorithm ASP.NET MVC kinda gets this right by

    transforming Con of Algorithm -> Con of Meaning (Action/Controller name strings).
  31. Con of Type //Do some stuff and finally throw new

    Exception(); EnforcePaymentStatusPolicy.Execute(blackboard);
  32. Static vs Dynamic No time but Dynamic Connacense: • Execution

    (order matters) • Timing (timing matters) • Value (related sets of values) • Identity (GetHashCode/UnitofWork)
  33. Ease of Changing Name of a type <-- easy Name

    of a method <-- easy parameters of a method/type <-- harder contract of a method <-- hard.
  34. Summary Design is Fractal. Design simply today, knowing you can

    add complexity needed tomorrow. No project ever failed because the design wasn’t complex enough