Designing and Maintaining Software (DAMS) Louis Rose
Problem
Current Architecture Likeable
Shareable
Liking and sharing foods are primary business concerns, so shouldn’t be implemented as delegators
Food
Pizza
Target Architecture Food
Likeable
Shareable
Pizza
Target Architecture Food
Burger
Recap: Ruby Modules
A module is a group of methods & constants module Likeable def likes @likes ||= 0 end def like! @likes += 1 end end
Including a module appends the methods & constants module Likeable def likes @likes ||= 0 end def like! @likes += 1 end end
class Pizza include Likeable end p = Pizza.new p.likes # returns 0 p.like! p.likes # returns 1
Object#extend includes a module for a specific instance module Likeable def likes @likes ||= 0 end def like! @likes += 1 end end
class Chef end c = Chef.new c.likes # throws NoMethodError c.extend(Likeable) c.likes # returns 0 c2 = Chef.new c.likes # throws NoMethodError
Classes are objects too, so can be extended module Likeable def likes @likes ||= 0 end def like! @likes += 1 end end
class Company extend Likeable end Company.likes # returns 0 Company.like! Company.likes # returns 1 c = Company.new c.likes # throws NoMethodError
Initial solutions
Current Architecture Likeable
Shareable
Liking and sharing foods are primary business concerns, so shouldn’t be implemented as delegators
Food
Pizza
Target Architecture Food
Likeable
Shareable
Pizza
Ship as separate gems module Likeable def likes @likes ||= 0 end def like! @likes += 1 end end
require "foodstore" require "foodstore-social" class Pizza < Food include Likeable end class Salad < Food end
Problem: how to configure an optional module? module Shareable def share self.class.networks.each do |m| puts "Shared via #{m}" end end end
require "foodstore" require "foodstore-social" class Pizza < Food include Shareable end
Solution! But, pizza is now coupled to implementation of Shareable :-( require "foodstore" require "foodstore-social" module Shareable def share self.class.networks.each do |m| puts "Shared via #{m}" end end end
class Pizza < Food include Shareable def self.networks %w(twitter facebook) end end
We want this, but it’s not valid Ruby module Shareable def share self.class.networks.each do |m| puts "Shared via #{m}" end end end
require "foodstore" require "foodstore-social" class Pizza < Food # not valid Ruby include Shareable(%w(twitter facebook)) end
Object#extend to the rescue!
Use extend to add a config method to the class module Shareable attr_reader :social_networks def shareable_via(social_networks) @social_networks = social_networks include InstanceMethods end module InstanceMethods def share self.class.social_networks.each do |m| puts "Shared via #{m}" end end end end
require "foodstore" require "foodstore-shareable" class Pizza < Food extend Shareable shareable_via %w(twitter facebook) end
Problem: forget to call the config method? require "foodstore" require "foodstore-shareable" class Pizza < Food extend Shareable end Pizza.new.share # NoMethodError: undefined method `share'
Problem: plug-ins now have an inconsistent API require "foodstore" require "foodstore-shareable" class Pizza < Food include Likeable extend Shareable shareable_via %w(twitter facebook) end
A plug-in API
What responsibilities does Shareable have? module Shareable attr_reader :social_networks def shareable_via(social_networks) @social_networks = social_networks include InstanceMethods end module InstanceMethods def share self.class.social_networks.each do |m| puts "Shared via #{m}" end end end end
What responsibilities does Shareable have? module Shareable attr_reader :social_networks def shareable_via(social_networks) @social_networks = social_networks include InstanceMethods end module InstanceMethods def share self.class.social_networks.each do |m| puts "Shared via #{m}" end end end end
Extract the plug-in configuration logic module Shareable def self.configure(pluggable, networks) pluggable.social_networks = networks end module ClassMethods attr_accessor :social_networks end module InstanceMethods def share self.class.social_networks.each do |m| puts "Shared via #{m}" end end end end
module Pluggable def plugin(mod, options) extend mod::ClassMethods include mod::InstanceMethods mod.configure(self, options) end end require "foodstore" require "foodstore-shareable" class Pizza < Food extend Pluggable plugin Likeable plugin Shareable, %w(twitter facebook) end
A final flourish
Avoid tight coupling… require "foodstore" require "foodstore-shareable" class Pizza < Food extend Pluggable plugin Likeable plugin Shareable, %w(twitter facebook) end
… by referencing plug-ins by name rather than by constant require "foodstore" require "foodstore-shareable" class Pizza < Food extend Pluggable plugin :likeable plugin :shareable, %w(twitter facebook) end
Adjust plug-in setup to locate by name module Pluggable def plugin(mod_name, *options) mod = Plugins.locate(mod_name) extend mod::ClassMethods if defined?(mod::ClassMethods) include mod::InstanceMethods if defined?(mod::InstanceMethods) mod.configure(self, *options) end end
Create Plugins module module Pluggable def plugin(mod_name, *options) mod = Plugins.locate(mod_name) extend mod::ClassMethods if defined?(mod::ClassMethods) include mod::InstanceMethods if defined?(mod::InstanceMethods) mod.configure(self, *options) end end module Plugins def self.locate(name) registry.fetch(name) end def self.registry @registry ||= {} end end
How to register a plug-in? module Plugins def self.locate(name) registry.fetch(name) end def self.registry @registry ||= {} end def self.register_plugin(name, mod) registry[name] = mod end end
A finished plug-in module Shareable Plugins.register_plugin :shareable, Shareable def self.configure(pluggable, *methods) pluggable.social_networks = methods end module ClassMethods attr_accessor :social_networks end module InstanceMethods def share self.class.sharing_methods.each do |m| puts "Shared via #{m}" end end end end
What did we achieve?
Old Architecture Likeable
Shareable
Liking and sharing foods are primary business concerns, so shouldn’t be implemented as delegators
Food
Pizza
New Architecture Plugins
Pluggable
Food
Likeable
Shareable
Pizza
New Architecture Plugins
Pluggable
Food
Likeable
Shareable
Pizza
Summary A plug-in architecture allows (possibly as-yetunforeseen) extensions to be made without redeploying core components. Ruby modules provide various avenues for implementing plug-in architectures, though some fairly complicated Ruby code is needed for sophisticated solutions.
Designing and Maintaining Software (DAMS) - GitHub
Plug-ins. Designing and Maintaining Software (DAMS). Louis Rose. Page 2. Problem. Page 3. Current Architecture. Shareable. Likeable. Food. Pizza. Liking and sharing foods are primary business concerns, so shouldn't be implemented as delegators. Page 4. Target Architecture. Shareable. Likeable. Food. Pizza. Page 5 ...
ASTs are tree data structures that can be analysed for meaning (following JLJ in SYAC 2014/15) ... More Cohesive. Avoids Duplication. Clearer. More Extensible.
Open-source. Influenced by Perl, Smalltalk, Eiffel, Ada and Lisp. Dynamic. Purely object-oriented. Some elements of functional programming. Duck-typed class Numeric def plus(x) self.+(x) end end y = 5.plus(6) https://www.ruby-lang.org/en/about · http
Page 1. Getting Lean. Designing and Maintaining Software (DAMS). Louis Rose. Page 2. Lean software⦠Has no extra parts. Solves the problem at hand and no more. Is often easier to change (i.e., is more habitable). Page 3. The Advice I Want to Give.
Why not duplicate? Designing and Maintaining Software (DAMS). Louis Rose. Page 2. Habitable Software. Leaner. Less Complex. Loosely Coupled. More Cohesive. Avoids Duplication. Clearer. More Extensible ??? Page 3. Bad Practice. Page 4. Don't Repeat Yo
âWe have tried to demonstrate that it is almost always incorrect to begin the decomposition of a system into modules on the basis of a flowchart. We propose instead that one begins with a list of difficult design decisions or design decisions which
Tools: Vagrant. Designing and Maintaining Software (DAMS). Louis Rose. Page 2. Bugs that appear in production and that can't be reproduced by a developer on their machine are really hard to fix. Problem: âIt works on my machineâ. Page 3. Why does
Clear Documentation. Designing and Maintaining Software (DAMS). Louis Rose. Page 2. Bad documentation. Misleading or contradictory find_customer(id). CustomerGateway. Used to look up a customer by their customer number. Page 3. Bad documentation. Red
%w.rack tilt date INT TERM..map{|l|trap(l){$r.stop}rescue require l};. $u=Date;$z=($u.new.year + 145).abs;puts "== Almost Sinatra/No Version has taken the stage on #$z for development with backup from Webrick". $n=Module.new{extend. Rack;a,D,S,q=Rack
R&D: sketch habitable solutions on paper, using UML. 4. Evaluate solutions and implement the best, using TDD. Probably start again at 3. 5. Give to the product owner to validate. Probably start again at 1. 6. Put into production for customers to eval
Habitable Software. Leaner. Less Complex. Loosely Coupled. More Cohesive. Avoids Duplication. Clearer. More Extensible ??? Page 3. Lean. âPerfection is finally achieved not when there is no longer anything to add, but when there is no longer anythi
Fixes issue #42. Users were being redirected to the home page after login, which is less useful than redirecting to the page they had originally requested before being redirected to the login form. * Store requested path in a session variable. * Redi
What is it? Several pieces of data are often used together. Why is it problematic? Behaviour that operates on the clump has no home. (and consequently is often duplicated). When does it arise? High cohesion of the clump has not been detected. D
Observers. Designing and Maintaining Software (DAMS). Louis Rose. Page 2. Page 3. Delivery people need to know when pizzas are ready class Pizza def initialize(delivery_person). @delivery_person = delivery_person end def bake cook # blocking call. @d
âWe want the reading of code to be easy, even it makes the writing harder. (Of course, there's no way to write code without also reading it, soâ¦)â - Bob Martin. Clean Code. Prentice Hall, 2009. Page 5. Page 6. User Experience. âA person of av
Getting loose coupling. Designing and Maintaining Software (DAMS). Louis Rose ... should not depend on low-level modules. Both should depend on abstractions.â âAbstractions should not depend on details. Details should depend on abstractions.â -
Automatically detect similar fragments of code. class StuffedCrust def title. "Stuffed Crust " +. @toppings.title +. " Pizza" end def cost. @toppings.cost + 6 end end class DeepPan def title. "Deep Pan " +. @ingredients.title +. " Pizza" end def cost
Ruby Testing Frameworks. 3 popular options are: RSpec, Minitest and Test::Unit. We'll use RSpec, as it has the most comprehensive docs. Introductory videos are at: http://rspec.info ...
Clear Names. Designing and Maintaining Software (DAMS). Louis Rose. Page 2. Naming is hard. âThere are only two hard things in Computer. Science: cache invalidation and naming things.â - Phil Karlton http://martinfowler.com/bliki/TwoHardThings.ht
Coupling Between Objects. Counts the number of other classes to which a class is coupled (other than via inheritance). CBO(c) = |d â C - (1cl U Ancestors(C))| uses(c, d) V uses(d, c). - Chidamber and Kemerer. A metrics suite for object-oriented des
Reducing duplication. Designing and Maintaining Software (DAMS). Louis Rose. Page 2. Tactics. Accentuate similarities to find differences. Favour composition over inheritance. Know when to reach for advanced tools. (metaprogramming, code generation).
When we are testing the way that a unit behaves when a condition is met, use a stub to setup the condition. Solution: use stubs for queries class Subscription ... def bill(amount) unless payments.exists(subscription_id: id) payments.charge(subscripti
Getting Cohesion. Designing and Maintaining Software (DAMS). Louis Rose. Page 2. Single Responsibility. Principle. A class should have only one reason to change. - Martin and Martin. Chapter 8, Agile Principles, Patterns and Practices in C#, Prentice
Size != Complexity. âImagine a small (50 line) program comprising. 25 consecutive "IF THEN" constructs. Such a program could have as many as 33.5 million distinct control paths.â - Thomas J. McCabe. IEEE Transactions on Software Engineering, 2:4,