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

Connascence - coupling for the lazy

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →
Avatar for Liam Brady Liam Brady
October 15, 2015

Connascence - coupling for the lazy

A brief description of connascence and its possible uses in Ruby

Avatar for Liam Brady

Liam Brady

October 15, 2015

Other Decks in Technology

Transcript

  1. “I’m really disappointed that there’s not more talk about connascence

    in the general programming public” - Jim Weirich
  2. “The problem with the term coupling is that everybody has

    a different idea of what it is. [With connascence] we’ve got a firmer understanding of the meanings...” - Jim Weirich
  3. Definition of Connascence What the dictionary says 1. “That which

    is born or produced with another” 2. “The act of growing together” A programmer-centric definition • Code that must change together in order to maintain the correctness of the system. • A design metric that helps us look at coupling in a system in a more structured way.
  4. Why I like connascence... • Holistic • Reduces cognitive load

    • Communication • Directed • Cheat sheet
  5. Types of Connascence • Name: when multiple components must agree

    on the name of an entity. • Type: when multiple components must agree on the type of an entity. • Meaning: when multiple components must agree on the meaning of specific values. • Position: when multiple components must agree on the order of values. • Algorithm: when multiple components must agree on a particular algorithm. • Execution (order): when the order of execution of multiple components is important. • Timing: when the timing of the execution of multiple components is important. • Value: when the value of two components are related • Identity: when multiple components must reference the entity. N.B. ‘component’ is a relative concept
  6. As degree of connascence between components increases, things become more

    tightly coupled. Favour lower degrees of connascence. We should always be moving left on the graph. Rules of Connascence - The Rule of Degree
  7. Higher degrees of connascence are acceptable if the elements involved

    are closely related. For example, positional arguments in private methods are less problematic than in public methods. Rules of Connascence - The Rule of Locality
  8. Connascence of Name class Cat def speak(vocalization) puts vocalization end

    end Cat.new.speak('meow') class Dog def speak(vocalization) puts vocalization end end Cat.new.speak('meow')
  9. Connascence of Position class Cat def beg(vocalization, repeat) repeat.times {puts

    vocalization} end end Cat.new.beg('meow', 4) class Cat def beg(vocalization, repeat) repeat.times {puts vocalization} end end Cat.new.beg(4, ‘meow’)
  10. Replacing Conn. of Position with Conn. of Name class Cat

    def beg(vocalization, repeat) number.times {puts vocalization} end end Cat.new.beg('meow', 4) class Cat def beg(vocalization:, repeat:) repeat.times {puts vocalization} end end Cat.new.beg(repeat: 4, vocalization: 'meow')
  11. Connascence of Position in the wild # ActionDispatch::Router::Mapper::Mapping def self.build(scope,

    set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, options) # some code omitted new set, ast, defaults, controller, default_action, scope[:module], to, formatted, scope_constraints, scope[:blocks] || [], via, options_constraints, anchor, options end • Rule of locality says connascence of position above can be considered acceptable because methods are used once and only locally.
  12. Replacing Conn. of Meaning with Conn. of Name class Cat

    attr_accessor :age end cat = Cat.new cat.age = 9 if (8..14).cover?(cat.age) human.stroke(cat) end class Cat SENIOR_RANGE = (8..14) attr_accessor :age def senior? SENIOR_RANGE.cover?(age) end end cat = Cat.new cat.age = 9 cat.senior?
  13. What hasn’t connascence caught on? • Complex names and principles

    are appealing • Influencers haven’t picked up on it
  14. How you can use connascence tomorrow... • Reduce the degree

    of connascence • Increase locality • Strong connascence with things that are unlikely to change is okay