Before we Begin

Think about a piece of code you:

  1. Don't understand
  2. Want to change


Write it down.

We're going to work on that later!

Work Code

OSS Project

Scratch Refactoring to Understand: Unearthing your Domain

@barryosull

Scratch Refactoring to Understand: Unearthing your Domain

  • Web Developer
  • Contractor/Architect/Consultant
  • Specialise in Legacy Web Apps
  • Insanely good at Mario Kart
     
  • DDD IE Organiser
  • PHP Dublin Organiser

@barryosull

Barry O Sullivan

About me

  1. The difficulties of existing code
  2. Why it's difficult
  3. Scratch Refactoring
  4. Techniques
  5. Scratch Refactoring Session

Session Structure

Existing Code

How we want to write code

  1. Plan desired change to system
  2. Estimate it
  3. Look through the codebase
  4. Figure out where we need to change
  5. Make the change
  6. Verify it

How we actually write code

  1. Plan desired change to system
  2. Estimate it
  3. Look through the codebase
  4. Realise we don't understand it at all
  5.   ** PANIC **
  6. Guess at where we need to change
  7. Hack it in
  8. Attempt to verify it (happy path only)
  9. Fix inevitable bugs

Most Existing Code is Hard to Understand

System Understanding

Developer

Knowledge

Useful

Abstractions

Accidental

Complication

x

%

(

)

Scratch Refactoring

Refactor a piece of code without worrying about it running.
 

 

The goal is to explore the code and gain understanding, not to write working code.

Scratch Refactoring

Refactor

Trash it

Where it's from

Different Perspectives

DDD Perspectives

Factory

Service

Repo

Value
Object

Entity

Constraint

- See which concepts work and which don't

- Spotted what's muddying the water

- An idea on how to isolate concepts

- Able to plan an iterative step forward

The End Results

Real World Example

public function save($class_name, $post, $id)
$rc = new \ReflectionClass("\\Elections\\". $class_name . "Director");
$director = $rc->newInstanceArgs(array($id));

Party
Director

Party
Builder

Party

Factory

Repo

Entity

Constraint

HTTP

Techniques

  • Rename vars/methods/classes
  • Private method extraction
  • Extract Conditionals
  • Extract Factories
  • Extract Infrastructure
    • DB / API Calls / Filesystem
  • Extract Input
    • HTTP / Message / Console

Tactical Techniques

  • Extract ValueObjects
  • Extract Entities
  • Pretend a Class Exists
  • Create Class inline
  • Inline Abstractions
    • Abstract class / Method calls

Useful Perspectives

Why are you changing this code?

Domain Language / Concepts

What is Input & Output

Driver

Adapters

Driven

Adapters

Application / Domain

  • Lean on auto refactoring tools
  • Describe the behaviour not the implementation
  • Put like with like: Private vars, methods
  • Focus on the code that matters, isolate and ignore the rest
  • Do it in pairs/mobs
  • Take notes of discoveries and concepts
  • Scrap it all and start again, see where you end up!

+Scratch Refactoring Tips+

The Hands-on Session

Scratch Refactoring 

  1. Find a messy piece of code and scratch refactor it
  2. Choose a technique, try it out for 30mins
  3. Reset, choose another and try that for 30mins
  4. Review: What's signal and what's noise
  • Do it alone or in pairs
  • If you don't have a laptop/code, pair up with someone
  • Go mad, try out ideas and see where you get!
  • No wrong answers, you can always start again

Increase Understanding

Increase Developer Knowledge by

Finding Consistent Patterns and

Diagnosing Accidental Complication

Through Scratch Refactoring

THX

@barryosull

Made with Slides.com