SeRP – Jon Wolski – What is Essential?



SeRP – Jon Wolski – What is Essential?

0 0


serp-presentation

Presenation slides for the Single Essential Responsibility Prinicple

On Github jonwolski / serp-presentation

SeRP

The Single Essential Responsibility Principle

Jon Wolski

Jon Wolski

DaveRamsey.com

Jon Wolski

DevelopWithPurpose.com

Jon Wolski

I ♥ DESIGN

Overview

System Rot Single Responsibility Principle Single Essential Responsibility Principle

First, a little foundation…

What is Essential?

Accidents & Essence

Accidents & Essence

Essence

The difficulties inherent in the nature of software

Accidents

those difficulties that today attend software production but are not inherent.

incidental

System Rot

Can software go bad?

It's code; it doesn't change unless you change it.

Insanity

Doing the same thing over and over again and expecting a different result

STUPIDITY

Doing the same thing over and over again and expecting the same result

ContextChanges

SRP

Single Responsibility Principle

A unit of code should do one thing and do it well

Example

  function calculate_tax_and_shipping(
      line_item, shipping_address, billing_address ) {
    
       var tax_amount = 
             line_item.price *
             line_item.qty * 
             get_tax_rate_for_state( billing_address.state );

       var shipping_amount = calculate_shipping_rate( 
               line_item.weight,
               line_item.qty,
               shipping_address
             );
       return tax_amount + shipping_amount;
  } 

Example

  function calculate_shipping( line_item, shipping_address ) {
			return calculate_shipping_rate( 
				 line_item.weight,
				 line_item.qty,
				 shipping_address
			);
  }
  
  function calculate_tax( line_item, billing_address ) {
	     return line_item.get_total() * 
             get_tax_rate_for_state( billing_address.state );
  } 

SRP restated

A unit of code should have only ONE reason to change A change should only effect ONE unit of code

Why SERP?

Where things go awry

Explosion of parts

  • User
  • UserModel
  • UserBusinessObject
  • UserService
  • UserDAO
  • UserValueObject
  • UserSerializer
  • UserManager

What is essential?

  • Persistence?
  • Serialization?
  • What are we modeling, anyway?

SERP

A unit of code should do ONE essential thing and do it completely.

Torches Ready?

A unit of code may handle accidentals to accomplish its one essential responsibility.

Example: persistence

  class User < ActiveRecord::Base
    has_many :accounts

    def someBusinessDomainMethods
      …
    end
  end

  @entity
  class Payment {
    public someBusinessDomainMethods() {
      …
    }
  }

Summary

  • Systems need maintenance due to changes of context
  • Use SRP to keep your code healthy
  • Distinguish between accidents and essence

Thank You

Jon Wolski jon.wolski@daveramsey.com

DevelopWithPurpose.com