Cohesion Metrics

Designing and Maintaining Software (DAMS)
 Louis Rose

Recap: Cohesion

“A measure of the degree to which the
 elements of a module belong together.” - Yourdon & Constantine 
 Structured Design, 1979

Cohesion Metrics

Indicate the extent to which
 elements are used together
 (and hence belong together?)

Corollary There are lots of cohesion metrics!

LCOM Lack of Cohesion of Methods

An inverse metric: a lower score is better We are using the 4th revision: LCOM4 Partitions an element (e.g., class) into sets of
 related elements

- Briand, Daly and Wust
 A Unified Framework for Cohesion Measurement in Object-Oriented Systems
 Empirical Software Engineering, 3, 1998

LCOM4 Example class Pizza attr_reader :toppings, :oven

toppings

def title toppings.title end

title

def cost toppings.cost + 4 end

cost

def bake oven.bake(self) end end

bake

oven

LCOM4 Example class Pizza attr_reader :toppings, :oven

toppings

def title toppings.title end

title

def cost toppings.cost + 4 end

cost

def bake oven.bake(self) end end

bake

oven

LCOM4 Example class Pizza attr_reader :toppings, :oven

toppings

def title toppings.title end

title

def cost toppings.cost + 4 end

cost

def bake oven.bake(self) end end

bake

oven

LCOM4 Example class Pizza attr_reader :toppings, :oven

toppings

def title toppings.title end

title

def cost toppings.cost + 4 end

cost

def bake oven.bake(self) end end

bake

oven

LCOM4 Example class Pizza attr_reader :toppings, :oven

toppings

def title toppings.title end

title

def cost toppings.cost + 4 end

cost

def bake oven.bake(self) end end

bake

oven

LCOM4 Example class Pizza attr_reader :toppings, :oven

toppings

def title toppings.title end

title

def cost toppings.cost + 4 end

cost

def bake oven.bake(self) end end

bake

oven

LCOM4 Example class Pizza attr_reader :toppings, :oven

toppings

def title toppings.title end

title

def cost toppings.cost + 4 end

cost

def bake oven.bake(self) end end

bake

oven

LCOM4 Example class Pizza attr_reader :toppings, :oven

toppings

def title toppings.title end

title

def cost toppings.cost + 4 end

cost

def bake oven.bake(self) end end

bake

oven

LCOM4 Example class Pizza attr_reader :toppings, :oven

toppings

def title toppings.title end

title

def cost toppings.cost + 4 end

cost

def bake oven.bake(self) end end

oven

bake

LCOM4 Example @toppings

@oven

toppings

oven

class Pizza attr_reader :toppings, :oven def title toppings.title end def cost toppings.cost + 4 end def bake oven.bake(self) end end

title

cost

bake

LCOM4 Example @toppings

@oven

toppings

oven

class Pizza attr_reader :toppings, :oven def title toppings.title end def cost toppings.cost + 4 end def bake @oven.bake(self) end end

title

cost

bake

LCOM4 Example @toppings

@oven

toppings

oven

class Pizza attr_reader :toppings, :oven def title toppings.title end def cost toppings.cost + 4 end def bake @oven.bake(self) end end

title

cost

bake

LCOM4 Example @toppings

@oven

toppings

oven

class Pizza attr_reader :toppings, :oven def title toppings.title end def cost toppings.cost + 4 end def bake @oven.bake(self) end end

title

cost

bake

Divergent Change What is it?
 A class can change for several reasons Why is it problematic?
 Changes are harder to make with confidence When does it arise?
 👎 Violation of Single Responsibility Principle
 👍 You know that the triggering changes won’t occur

Data Clumps 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
 👍 Domain is not thoroughly understood (yet)

Primitive Obsession What is it?
 Using built-in types to represent domain concepts. Why is it problematic?
 Behaviour that operates on the domain object has
 no home (and consequently is often duplicated) When does it arise?
 👎 Fear of OO (often due to performance concerns)
 👍 If any extracted object would be very small

Feature Envy What is it?
 An object is more concerned with another object’s attributes or methods than its own. Why is it problematic?
 Behaviour probably belongs on the envied object
 (and consequently is often duplicated) When does it arise?
 👎 Violation of Tell, Don’t Ask principle
 👍 Behaviour is much more likely to change
 when the envying class changes

Summary Classes should have a single responsibility:
 a single reason to change
 
 Classes with low cohesion normally have
 more than one responsibility Extract classes to redistribute responsibilities

Designing and Maintaining Software (DAMS) - GitHub

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. Domain is not thoroughly understood (yet) ...

4MB Sizes 0 Downloads 104 Views

Recommend Documents

Designing and Maintaining Software (DAMS) - GitHub
ASTs are tree data structures that can be analysed for meaning (following JLJ in SYAC 2014/15) ... More Cohesive. Avoids Duplication. Clearer. More Extensible.

Designing and Maintaining Software (DAMS) - GitHub
%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

Designing and Maintaining Software (DAMS) - GitHub
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

Designing and Maintaining Software (DAMS) - GitHub
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

Designing and Maintaining Software (DAMS) - GitHub
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

Designing and Maintaining Software (DAMS) - GitHub
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