Slide 1

Slide 1 text

Ship 10 Times Faster With These Designs CONFOO 2021 | FEB 25, 2021 @afilina

Slide 2

Slide 2 text

Anna Filina / @afilina • Coding since 1997. • PHP since 2003. • Legacy archaeology. • Test automation. • Talks and workshops. • YouTube videos. • Filina Consulting.

Slide 3

Slide 3 text

Your competition will hate you. By the time they release one feature with a ton of bugs, you would have released 10 features with no bugs. A good design makes a world of difference in terms of ease of change, teamwork, testability, reliability and overall quality of life. I will present a software design approach that allowed my teams to ship unbelievably fast while keeping the quality above industry standards.

Slide 4

Slide 4 text

Waiting for all the information before starting work.

Slide 5

Slide 5 text

Knowns • Payment Provider • Charge money • Refund money of a previous transaction

Slide 6

Slide 6 text

Payment Provider charge

Slide 7

Slide 7 text

Payment Provider charge (Money, Instrument) refund

Slide 8

Slide 8 text

Payment Provider charge (Money, Instrument) refund (Money, PaymentId)

Slide 9

Slide 9 text

Payment Provider charge (Money, Instrument) : PaymentId refund (Money, PaymentId)

Slide 10

Slide 10 text

Payment Provider charge (Money, Instrument) : PaymentId refund (Money, PaymentId) : RefundId

Slide 11

Slide 11 text

interface PaymentProvider { public function charge(Money $money, Instrument $instrument): PaymentId; }

Slide 12

Slide 12 text

Are we able to use this interface?

Slide 13

Slide 13 text

Submit Form field Form field POST /payments/capture

Slide 14

Slide 14 text

Validate input Controller Charge via PaymentProvider Request Response Send output

Slide 15

Slide 15 text

final class CapturePaymentHandler { public function handle(ServerRequestInterface $request): ResponseInterface { } }

Slide 16

Slide 16 text

final class CapturePaymentHandler { public function handle(ServerRequestInterface $request): ResponseInterface { $paymentId = $this->paymentProvider->charge($money, $instrument); } }

Slide 17

Slide 17 text

final class CapturePaymentHandler { public function handle(ServerRequestInterface $request): ResponseInterface { $money = Money::fromRequest($request); $instrument = Instrument::fromRequest($request); $paymentId = $this->paymentProvider->charge($money, $instrument); } }

Slide 18

Slide 18 text

final class CapturePaymentHandler { public function handle(ServerRequestInterface $request): ResponseInterface { $money = Money::fromRequest($request); $instrument = Instrument::fromRequest($request); $paymentId = $this->paymentProvider->charge($money, $instrument); return $this->createSuccessResponse($paymentId); } }

Slide 19

Slide 19 text

{ "instrument": { //... }, "money": { "currency": "USD", "amount": 1050 } } { "paymentId": "CC-0001" }

Slide 20

Slide 20 text

FE/BE Contract • Is input viable? • Is output viable? • Talk use cases.

Slide 21

Slide 21 text

Waiting for all the information before starting work. Postpone decisions using interfaces. Write code for the things that you do know. Validate assumptions using code. Uncover new information or use cases.

Slide 22

Slide 22 text

Can't have multiple developers on one feature.

Slide 23

Slide 23 text

class CapturePaymentHandler { public function handle(ServerRequestInterface $request): ResponseInterface { $money = Money::fromRequest($request); $instrument = Instrument::fromRequest($request); $paymentId = $this->paymentProvider->charge($money, $instrument); return $this->createSuccessResponse($paymentId); } }

Slide 24

Slide 24 text

class CapturePaymentHandler { public function handle(ServerRequestInterface $request): ResponseInterface { $money = Money::fromRequest($request); $instrument = Instrument::fromRequest($request); $paymentId = $this->paymentProvider->charge($money, $instrument); return $this->createSuccessResponse($paymentId); } }

Slide 25

Slide 25 text

class CapturePaymentHandler { public function handle(ServerRequestInterface $request): ResponseInterface { $money = Money::fromRequest($request); $instrument = Instrument::fromRequest($request); $paymentId = $this->paymentProvider->charge($money, $instrument); return $this->createSuccessResponse($paymentId); } }

Slide 26

Slide 26 text

interface PaymentProvider { /** * @throws ChargeFailed */ public function charge(Money $money, Instrument $instrument): PaymentId; }

Slide 27

Slide 27 text

$money = Money::fromRequest($request); $instrument = Instrument::fromRequest($request); try { $paymentId = $this->paymentProvider->charge($money, $instrument); } catch (ChargeFailed $exception) { return $this->createErrorResponse($exception->getMessage()); } return $this->createSuccessResponse($paymentId);

Slide 28

Slide 28 text

Handlers + tests PaymentProvider implementation + tests Acceptance tests + wiring components Value objects + tests POST /payments/capture

Slide 29

Slide 29 text

Can't have multiple developers on one feature. Create separate classes with clear contracts.

Slide 30

Slide 30 text

We don't understand the domain.

Slide 31

Slide 31 text

charge($money, $instrument) Money int $amount string $currency charge(int $amount, string $currency, string $cardNumber, string $cardExpiry, string $postalCode, …) Instrument Card $card Address $address

Slide 32

Slide 32 text

PaymentProvider HttpClient send (Request) charge (Money, Instrument)

Slide 33

Slide 33 text

Money Instrument PaymentProvider Domain

Slide 34

Slide 34 text

Money Instrument PaymentProvider CapturePaymentHandler Application

Slide 35

Slide 35 text

Money Instrument PaymentProvider CapturePaymentHandler HttpClient Infrastructure

Slide 36

Slide 36 text

Create Small Classes & Methods • 10 statements per method. • Classes that can fit in your head.

Slide 37

Slide 37 text

We don't understand the domain. Separate the domain from the other layers. Create small classes and methods.

Slide 38

Slide 38 text

Bugs.

Slide 39

Slide 39 text

Money Instrument PaymentProvider CapturePaymentHandler HttpClient Unit tests Unit tests Integration tests Acceptance tests

Slide 40

Slide 40 text

final class CapturePaymentHandler { private StripePaymentProvider $paymentProvider; public function __construct() { $this->paymentProvider = new StripePaymentProvider(); } }

Slide 41

Slide 41 text

final class CapturePaymentHandler { private PaymentProvider $paymentProvider; public function __construct(PaymentProvider $paymentProvider) { $this->paymentProvider = $paymentProvider; } } final class CapturePaymentHandler { private StripePaymentProvider $paymentProvider; public function __construct() { $this->paymentProvider = new StripePaymentProvider(); } }

Slide 42

Slide 42 text

final class CapturePaymentHandlerTest extends TestCase { protected function setUp(): void { $this->paymentProvider = $this->createMock(PaymentProvider::class); $this->capturePaymentHandler = new CapturePaymentHandler( $this->paymentProvider ); } }

Slide 43

Slide 43 text

Bugs. Just write tests. Learn to write even better tests.

Slide 44

Slide 44 text

@afilina