Leave No Trace: Test Driven Development Using the SOLID Principles

A presentation at That Conference in August 2013 in Wisconsin Dells, WI, USA by Jenna Pederson

Slide 1

Slide 1

Slide 2

Slide 2

@JENNAPEDERSON W W W. 6 1 2 S O F T W A R E F O U N D R Y. C O M L E AV E N O T R A C E : TEST DRIVEN DEVELOPMENT USING THE SOLID PRINCIPLES COPYRIGHT © 2014 JENNA PEDERSON. ALL RIGHTS RESERVED.

Slide 3

Slide 3

WHO AM I? • Independent, full-stack web developer • Code geek + process geek - agile, TDD, productivity, etc. • Co-Ambassador for the Twin Cities Geekettes • www.geekettes.io • @jennapederson • www.612softwarefoundry.com

Slide 4

Slide 4

AGENDA • What makes a design good? • Test Driven Development • SOLID Principles • Examples

Slide 5

Slide 5

W H AT M A K E S A D E S I G N G O O D ?

Slide 6

Slide 6

I T I S H A R D T O C H A N G E B E C A U S E E V E R Y C H A N G E A F F E C T S T O O M A N Y O T H E R PA R T S OF THE SYSTEM. RIGIDITY

Slide 7

Slide 7

W H E N Y O U M A K E A C H A N G E , U N E X P E C T E D PA R T S O F T H E S Y S T E M B R E A K . FRAGILITY

Slide 8

Slide 8

I T I S H A R D T O R E U S E I N A N O T H E R A P P L I C AT I O N B E C A U S E I T C A N N O T B E D I S E N TA N G L E D F R O M T H E C U R R E N T A P P L I C AT I O N . IMMOBILITY

Slide 9

Slide 9

MAKE IT WORK, MAKE IT BETTER W H AT I S T D D ?

Slide 10

Slide 10

Slide 11

Slide 11

Slide 12

Slide 12

A C L A S S S H O U L D H A V E O N E A N D O N LY ONE REASON TO CHANGE. SINGLE RESPONSIBILITY PRINCIPLE

Slide 13

Slide 13

B ER FEOSRREP S R P BEFO

Slide 14

Slide 14

AFTER SRP

Slide 15

Slide 15

AFTER SRP

Slide 16

Slide 16

A P P LY I N G S R P T O O U R T E S T I N G • Tests are more granular • Easier to understand and maintain • Fewer changes to tests • Failing tests point to only one responsibility instead of many

Slide 17

Slide 17

YOU SHOULD BE ABLE TO EXTEND A CLASS’ B E H A V I O R , W I T H O U T M O D I F Y I N G I T. THE OPEN CLOSED PRINCIPLE

Slide 18

Slide 18

B ER FEOSRREP O C P BEFO

Slide 19

Slide 19

A FR TE ESRR O BEFO P CP

Slide 20

Slide 20

A P P LY I N G O C P T O O U R T E S T I N G • Classes under test are open so we can: • inject mocked dependencies • override methods that make calls to external services

Slide 21

Slide 21

DERIVED CLASSES MUST BE S U B S T I T U TA B L E F O R T H E I R B A S E C L A S S E S . LISKOV SUBSTITUTION PRINCIPLE

Slide 22

Slide 22

B ER FEOSRREP L S P BEFO

Slide 23

Slide 23

A FR TE ESRR LPS P BEFO

Slide 24

Slide 24

A P P LY I N G L S P T O O U R T E S T I N G • Allows us to mock dependencies by creating substitutes (derived classes or mocks) • Helps keep tests isolated so we only test one responsibility • Less coupling to dependencies means we don’t need to know as much about the internals to write a test

Slide 25

Slide 25

M A K E F I N D G R A I N E D I N T E R F A C E S T H AT ARE CLIENT SPECIFIC. I N T E R FA C E S E G R E G AT I O N PRINCIPLE

Slide 26

Slide 26

B ER FEOSRREP I S P BEFO

Slide 27

Slide 27

A FR TE ESRR IPS P BEFO

Slide 28

Slide 28

A P P LY I N G I S P T O O U R T E S T I N G • Classes depend on narrower interfaces, making • tests smaller • mocking easier

Slide 29

Slide 29

DEPEND ON ABSTRACTIONS, NOT ON CONCRETIONS. DEPENDENCY INVERSION PRINCIPLE

Slide 30

Slide 30

B ER FEOSRREP D I P BEFO

Slide 31

Slide 31

A FR TE ESRR D BEFO P IP

Slide 32

Slide 32

A P P LY I N G D I P T O O U R T E S T I N G • Writing more decoupled code allows mocking and injecting of dependencies • If you skip these abstractions, your tests become more end-to-end tests because you are unable to swap out implementations for mocks.

Slide 33

Slide 33

N O W W H AT ? • Use the SOLID Principles to guide your way toward keeping your codebase clean, easier to extend, maintain, and reuse • SOLID is not the law • TDD does not guarantee good design • Don’t forget to engage your brain!

Slide 34

Slide 34

QUESTIONS?

Slide 35

Slide 35

H T T P S : / / W W W. T H AT C O N F E R E N C E . C O M / SPONSORS PLEASE THANK THE SPONSORS!

Slide 36

Slide 36

Slide 37

Slide 37

HTTPS://GITHUB.COM/JENNAPEDERSON/TDD-USING-THE-SOLID-PRINCIPLES CODE & RESOURCES S O L I D M O T I V AT I O N A L P H O T O C R E D I T S : H T T P : / / B I T. LY / M O T I V AT I O N A L _ S O L I D

Slide 38

Slide 38

@JENNAPEDERSON W W W. 6 1 2 S O F T W A R E F O U N D R Y. C O M THANK YOU!