is what you’ve coded what you mean? - dave liddament · 2020. 5. 28. · first let’s talk about...

165
Is what you’ve coded what you mean? Dave Liddament (@daveliddament)

Upload: others

Post on 11-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Is what you’ve coded what you mean?

Dave Liddament (@daveliddament)

Page 2: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

A confession….

@DaveLiddament

Page 3: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

First let’s talk about bugs….

@DaveLiddament

Page 4: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Question 1: Who puts bugs in

their code?

@DaveLiddament

Page 5: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Question 2: When is the best

time to find a bug?

@DaveLiddament

Page 6: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Best time to find a bug?

@DaveLiddament

Page 7: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Best time to find a bug?

@DaveLiddament

Months into

operation

Page 8: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Best time to find a bug?

@DaveLiddament

Months into

operation

Page 9: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Best time to find a bug?

@DaveLiddament

Months into

operation

Feature is first used

Page 10: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Best time to find a bug?

@DaveLiddament

Months into

operation

Feature is first used

Page 11: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Best time to find a bug?

@DaveLiddament

Months into

operation

Feature is first used

Testing

Page 12: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Best time to find a bug?

@DaveLiddament

Months into

operation

Feature is first used

Testing

Page 13: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Best time to find a bug?

@DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Page 14: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Best time to find a bug?

@DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Page 15: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Best time to find a bug?

@DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Before writing code

Page 16: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Why do bugs happen?

Page 17: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code should do

Why do bugs happen?

Page 18: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code should do

What the code actually does

Why do bugs happen?

Page 19: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code should do

What the code actually does

What the developer thinks the code does

Why do bugs happen?

Page 20: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code should do

What the code actually does

What the developer thinks the code does

Why do bugs happen?

Page 21: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Before writing code

Why this talk?

Page 22: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code should do What the

code actually does

What the developer thinks the code does

How we reduce cost of bugs

Page 23: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Pay attention!

@DaveLiddament

Dave Liddament @daveliddament

Lamp Bristol

15+ years software development (PHP, Java, Python, C)

Organise PHP-SW user group and Bristol PHP Training

Page 24: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Before writing code

Page 25: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Before writing code

Obvious code

Page 26: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Before writing code

Obvious code

Run time analysis

Page 27: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Before writing code

Obvious code

Run time analysis

Static analysis

Page 28: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis

Page 29: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

function process($user) { // some implementation }

$a = 1; process($a);

Is this code valid?

Page 30: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

function process(User $user) { // some implementation }

$a = 1; process($a);

@DaveLiddament

Is this code valid?

Page 31: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

function process(User $user) { // some implementation }

$a = 1; process($a);

@DaveLiddament

Is this code valid?

Page 32: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

function process(User $user) { // some implementation }

$a = 1; process($a);

@DaveLiddament

Is this code valid?

Page 33: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

function process(User $user) { // some implementation }

$a = 1; process($a);

@DaveLiddament

Is this code valid?

Page 34: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Page 35: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Page 36: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

function process(User $user) { // some implementation }

$a = 1; process($a);

@DaveLiddament

Type hinting has helped

Page 37: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Before writing code

Page 38: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

More type hinting with PHP 7

function duplicateString ( string $value, int $times) :string

Page 39: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

More type hinting with PHP 7

function duplicateString ( string $value, int $times) :string

Page 40: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

More type hinting with PHP 7

function duplicateString ( string $value, int $times) :string

Page 41: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Is this code valid?

function getUser(int $id): User {…}

function process(User $user): void {…}

$a = getUser(12); process($a);

Page 42: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Is this code valid?

function getUser(int $id): User {…}

function process(User $user): void {…}

$a = getUser(12); process($a);

Page 43: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Is this code valid?

function getUser(int $id): User {…}

function process(User $user): void {…}

$a = getUser(12); process($a);

Page 44: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Is this code valid?

function getUser(int $id): User {…}

function process(User $user): void {…}

$a = getUser(12); process($a);

Page 45: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Is this code valid?

function getUser(int $id): User {…}

function process(User $user): void {…}

$a = getUser(12); process($a);

Page 46: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Is this code valid?

function getUser(int $id): User {…}

function process(User $user): void {…}

$a = getUser(12); process($a);

Page 47: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Is this code valid?

function getUser(int $id): User {…}

function process(User $user): void {…}

$a = getUser(12); process($a);

Page 48: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code actually does

What the developer thinks the code does

Win Win

Page 49: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code actually does

What the developer thinks the code does

Win Win

Page 50: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Language level validation

function getUser(int $id): User {…}

function process(User $user): void {…}

$a = getUser(“dave”); process($a);

Page 51: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Language level validation

function getUser(int $id): User {…}

function process(User $user): void {…}

$a = getUser(“dave”); process($a);

Page 52: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Language level validation

function getUser(int $id): User {…}

function process(User $user): void {…}

$a = getUser(“dave”); process($a);

Page 53: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis can cover language gapsclass User { public function getAccountNumber() :string {…} }

/** * @return User[] */ function getUsers(): array { … }

$users = getUsers(); foreach($users as $user) { $accountNumber = $user->getAccountNumber(); }

Page 54: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis can cover language gapsclass User { public function getAccountNumber() :string {…} }

/** * @return User[] */ function getUsers(): array { … }

$users = getUsers(); foreach($users as $user) { $accountNumber = $user->getAccountNumber(); }

Page 55: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis can cover language gapsclass User { public function getAccountNumber() :string {…} }

/** * @return User[] */ function getUsers(): array { … }

$users = getUsers(); foreach($users as $user) { $accountNumber = $user->getAccountNumber(); }

Page 56: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis can cover language gapsclass User { public function getAccountNumber() :string {…} }

/** * @return User[] */ function getUsers(): array { … }

$users = getUsers(); foreach($users as $user) { $accountNumber = $user->getAccountNumber(); }

Page 57: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis can cover language gapsclass User { public function getAccountNumber() :string {…} }

/** * @return User[] */ function getUsers(): array { … }

$users = getUsers(); foreach($users as $user) { $accountNumber = $user->getAccountNumber(); }

Page 58: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis can cover language gapsclass User { public function getAccountNumber() :string {…} }

/** * @return User[] */ function getUsers(): array { … }

$users = getUsers(); foreach($users as $user) { $accountNumber = $user->getAccountNumber(); }

Page 59: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis can cover language gapsclass User { public function getAccountNumber() :string {…} }

/** * @return User[] */ function getUsers(): array { … }

$users = getUsers(); foreach($users as $user) { $accountNumber = $user->getAccountNumber(); }

Page 60: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis can find errorsclass User { public function getAccountNumber() :string {…} }

/** * @return User[] */ function getUsers(): array { … }

$users = getUsers(); foreach($users as $user) { $accountNumber = $user->getSomething(); }

Page 61: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis can find errorsclass User { public function getAccountNumber() :string {…} }

/** * @return User[] */ function getUsers(): array { … }

$users = getUsers(); foreach($users as $user) { $accountNumber = $user->getSomething(); }

Page 62: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis can find errorsclass User { public function getAccountNumber() :string {…} }

/** * @return User[] */ function getUsers(): array { … }

$users = getUsers(); foreach($users as $user) { $accountNumber = $user->getSomething(); }

Page 63: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Static analysis helps developers

Page 64: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis recap

Page 65: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis recap• Analyse code without running it

Page 66: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis recap• Analyse code without running it

• Prevent bugs even entering the code base

Page 67: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis recap• Analyse code without running it

• Prevent bugs even entering the code base

• Type hinting and doc blocks comments help static analysis tools

•which in turn help developers

Page 68: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Static analysis recap• Analyse code without running it

• Prevent bugs even entering the code base

• Type hinting and doc blocks comments help static analysis tools

•which in turn help developers

• Use an IDE that offers static analysis

Page 69: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Run time checks

Page 70: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Run time checks

• Tests

Page 71: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Run time checks

• Tests

• Assertions

Page 72: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Tests are assertions

Page 73: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Tests are assertions

If I apply a discount code “SPEAKER”

Page 74: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Tests are assertions

If I apply a discount code “SPEAKER”

My conference ticket is reduced to £90

Page 75: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Before writing code

Benefits of testing

Page 76: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code should do

What the code actually does

Benefits of testing

Page 77: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code should do

What the code actually does

Benefits of testing

Page 78: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code actually does

What the developer thinks the code does

Benefits of testing

What the code should do

Page 79: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code actually does

What the developer thinks the code does

Benefits of testing

What the code should do

Page 80: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Assertions in code

Page 81: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Assertions in code

Statements that the developer believes should always be true

Page 82: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Code that worries me…if ($type == 1) { $message = ‘hello’; } elseif ($type == 2) { $message = ‘goodbye’; }

sendMessage($message);

Page 83: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Code that worries me…if ($type == 1) { $message = ‘hello’; } elseif ($type == 2) { $message = ‘goodbye’; }

sendMessage($message);

Page 84: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Code that worries me…if ($type == 1) { $message = ‘hello’; } elseif ($type == 2) { $message = ‘goodbye’; }

sendMessage($message);

Page 85: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Code that worries me…if ($type == 1) { $message = ‘hello’; } elseif ($type == 2) { $message = ‘goodbye’; }

sendMessage($message);

Page 86: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Now I’m happier…if ($type == 1) { $message = ‘hello’; } elseif ($type == 2) { $message = ‘goodbye’; } else { throw new Exception(“Invalid type”); } sendMessage($message);

Page 87: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Now I’m happier…if ($type == 1) { $message = ‘hello’; } elseif ($type == 2) { $message = ‘goodbye’; } else { throw new Exception(“Invalid type”); } sendMessage($message);

Page 88: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

public function setStatus(string $status){ $this->status = $status; }

@DaveLiddament

Can we improve this code

Page 89: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

const REGISTERED = ‘registered’; const STARTED = ‘started’; const FINISHED = ‘finished’; const QUIT = ‘quit’;

public function setStatus(string $status){ $this->status = $status; }

@DaveLiddament

Improvement 1: Add constants

Page 90: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

const REGISTERED = ‘registered’; const STARTED = ‘started’; const FINISHED = ‘finished’; const QUIT = ‘quit’;

public function setStatus(string $status){ $this->status = $status; }

@DaveLiddament

Improvement 1: Add constants

Page 91: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Improvement 2: Add assertion… constants defined as before …

public function setStatus(string $status){ if (!in_array($status,[self::REGISTERED, self::STARTED, self::FINISHED]) { throw new Exception(“Invalid status”); } $this->status = $status; }

Page 92: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Improvement 2: Add assertion… constants defined as before …

public function setStatus(string $status){ if (!in_array($status,[self::REGISTERED, self::STARTED, self::FINISHED]) { throw new Exception(“Invalid status”); } $this->status = $status; }

Page 93: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Improvement 2: Add assertion… constants defined as before …

public function setStatus(string $status){ if (!in_array($status,[self::REGISTERED, self::STARTED, self::FINISHED]) { throw new Exception(“Invalid status”); } $this->status = $status; }

Page 94: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Improving error messages

Page 95: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Improving error messages

Invalid type

Page 96: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Improving error messages

Invalid type

Invalid type [$type]

Page 97: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Improving error messages

Invalid type

Invalid type [$type]

Invalid type [$type] for user [$userId]

Page 98: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code actually does

What the developer thinks the code does

Wins from asserts

Page 99: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code actually does

What the developer thinks the code does

Wins from asserts

Page 100: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Before writing code

Benefits of assertions

Page 101: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Before writing code

Benefits of assertions and a good test suite

Page 102: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Obvious code

Page 103: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Obvious code• Value Objects

Page 104: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Obvious code• Value Objects

• Rename and Refactor

Page 105: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Value Objects

Page 106: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Can we improve this code?class MarketingCampaign {

… some methods …

public function addAddress(string $address); }

$campaign = new MarketingCampaign(); $campaign->addAddress(“[email protected]”)

Page 107: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Can we improve this code?class MarketingCampaign {

… some methods …

public function addAddress(string $address); }

$campaign = new MarketingCampaign(); $campaign->addAddress(“[email protected]”)

Page 108: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

These are all strings…

[email protected]

[email protected]

fredblogs.comfred.blogs

6 Lower Park Row, Bristol

Page 109: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

These are all strings…

[email protected]

[email protected]

fredblogs.comfred.blogs

6 Lower Park Row, Bristol

Page 110: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

class MarketingCampaign {

.. some methods ..

public function addAddress(string $address); }

$campaign = new MarketingCampaign(); $campaign->addAddress(“6 Lower Park Row, Bristol”)

This is wrong (and our IDE can’t spot mistake)

Page 111: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

class MarketingCampaign {

.. some methods ..

public function addAddress(string $address); }

$campaign = new MarketingCampaign(); $campaign->addAddress(“6 Lower Park Row, Bristol”)

This is wrong (and our IDE can’t spot mistake)

Page 112: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

EmailAddress object instead of primitiveclass EmailAddress {

private $emailAddress; public function __construct(string $emailAddress) { $this->emailAddress = $emailAddress; }

public function getEmailAddress(): string { return $this->emailAddress; } }

Page 113: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

EmailAddress object instead of primitiveclass EmailAddress {

private $emailAddress; public function __construct(string $emailAddress) { $this->emailAddress = $emailAddress; }

public function getEmailAddress(): string { return $this->emailAddress; } }

Page 114: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

EmailAddress object instead of primitiveclass EmailAddress {

private $emailAddress; public function __construct(string $emailAddress) { $this->emailAddress = $emailAddress; }

public function getEmailAddress(): string { return $this->emailAddress; } }

Page 115: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

EmailAddress object instead of primitiveclass EmailAddress {

private $emailAddress; public function __construct(string $emailAddress) { $this->emailAddress = $emailAddress; }

public function getEmailAddress(): string { return $this->emailAddress; } }

Page 116: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Using EmailAddressclass MarketingCampaign {

.. some methods ..

public function addAddress(EmailAddress $address); }

$campaign = new MarketingCampaign(); $emailAddress = new EmailAddress(“[email protected]”) $campaign->addAddress($emailAddress)

Page 117: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Using EmailAddressclass MarketingCampaign {

.. some methods ..

public function addAddress(EmailAddress $address); }

$campaign = new MarketingCampaign(); $emailAddress = new EmailAddress(“[email protected]”) $campaign->addAddress($emailAddress)

Page 118: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Using EmailAddressclass MarketingCampaign {

.. some methods ..

public function addAddress(EmailAddress $address); }

$campaign = new MarketingCampaign(); $emailAddress = new EmailAddress(“[email protected]”) $campaign->addAddress($emailAddress)

Page 119: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Using EmailAddressclass MarketingCampaign {

.. some methods ..

public function addAddress(EmailAddress $address); }

$campaign = new MarketingCampaign(); $emailAddress = new EmailAddress(“[email protected]”) $campaign->addAddress($emailAddress)

Page 120: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

class MarketingCampaign {

.. some methods ..

public function addAddress(EmailAddress $address); }

$campaign = new MarketingCampaign(); $campaign->addAddress(“6 Lower Park Row, Bristol”)

This will fail (and your IDE will warn you)

Page 121: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

class MarketingCampaign {

.. some methods ..

public function addAddress(EmailAddress $address); }

$campaign = new MarketingCampaign(); $campaign->addAddress(“6 Lower Park Row, Bristol”)

This will fail (and your IDE will warn you)

Page 122: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

$emailAddress = new EmailAddress(“6 Lower Park Row”);

But this is wrong

Page 123: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

$emailAddress = new EmailAddress(“6 Lower Park Row”);

But this is wrong

Page 124: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

public function __construct(string $emailAddress) { if ( … check email address is valid… == false) { throw new Exception( “Invalid email address [$emailAddress]”); }

$this->emailAddress = $emailAddress; }

Add validation

Page 125: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

public function __construct(string $emailAddress) { if ( … check email address is valid… == false) { throw new Exception( “Invalid email address [$emailAddress]”); }

$this->emailAddress = $emailAddress; }

Add validation

Page 126: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

We’re guaranteed that EmailAddress represents a correctly formatted email

address.

@DaveLiddament

Big win

Page 128: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Store canonical form

public function __construct(string $emailAddress) {

… validate email address … $this->emailAddress = $this->getCanonical($emailAddress); }

Page 129: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Store canonical form

public function __construct(string $emailAddress) {

… validate email address … $this->emailAddress = $this->getCanonical($emailAddress); }

Page 130: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Postcodes formats

Normal: No spaces: Fixed width:

B1 1AB B11AB B1 1AB

Page 131: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Add domain specific logic

public function getPostcode(): string {…}

public function getNoSpacesPostcode(): string {…}

public function getFixedWidthPostcode(): string {…}

Page 132: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Are these positions equal?

A

B C

Page 133: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Are these positions equal?

A

B C

Page 134: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Add equals methodclass Point { const TOLERANCE = 10;

… Other methods …

public function equals(Point $other): bool { $distance = calculateDistance($this, $other); return $distance < self::TOLERANCE; } }

Page 135: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Add equals methodclass Point { const TOLERANCE = 10;

… Other methods …

public function equals(Point $other): bool { $distance = calculateDistance($this, $other); return $distance < self::TOLERANCE; } }

Page 136: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Be careful comparing objects… if ($point1 == $point2) { … some code … }

if ($point1->equals($point2)) { … some code … }

Page 137: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Be careful comparing objects… if ($point1 == $point2) { … some code … }

if ($point1->equals($point2)) { … some code … }

Page 138: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Be careful comparing objects… if ($point1 == $point2) { … some code … }

if ($point1->equals($point2)) { … some code … }

Page 139: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Before writing code

Benefits of Value Objects

Page 140: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code actually does

What the developer thinks the code does

Wins from Value Objects

Page 141: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code actually does

What the developer thinks the code does

Wins from Value Objects

Page 142: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Rename and Refactor

Page 143: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Kno

wle

ge

Time on project

Page 144: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Renamingclass User { public function getName() {…} }

class Game { public function getName() {…} }

Page 145: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Renamingclass User { public function getName() {…} }

class Game { public function getName() {…} }

Page 146: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Renamingclass User { public function getName() {…} }

class Game { public function getQuest() {…} }

Page 147: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Renamingclass User { public function getName() {…} }

class Game { public function getQuest() {…} }

Page 148: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Renamingfunction getUser();

function getGame();

$user = getUser(); $game = getGame();

echo ‘Hello ‘ . $user->getName(); echo ‘You are playing ‘ . $game->getName();

Page 149: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Renamingfunction getUser();

function getGame();

$user = getUser(); $game = getGame();

echo ‘Hello ‘ . $user->getName(); echo ‘You are playing ‘ . $game->getName();

Page 150: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Renamingfunction getUser(): User;

function getGame(): Game;

$user = getUser(); $game = getGame();

echo ‘Hello ‘ . $user->getName(); echo ‘You are playing ‘ . $game->getQuest();

Page 151: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Renamingfunction getUser(): User;

function getGame(): Game;

$user = getUser(); $game = getGame();

echo ‘Hello ‘ . $user->getName(); echo ‘You are playing ‘ . $game->getQuest();

Page 152: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Renamingfunction getUser(): User;

function getGame(): Game;

$user = getUser(); $game = getGame();

echo ‘Hello ‘ . $user->getName(); echo ‘You are playing ‘ . $game->getQuest();

Page 153: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Win-Win: Rename and refactor

Page 154: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

Months into

operation

Feature is first used

Testing Writing code

Before writing code

Summary

Page 155: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

What the code should do What the

code actually does

What the developer thinks the code does

Summary

Page 156: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Summary

Page 157: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Summary

• Type hint everything you can

Page 158: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Summary

• Type hint everything you can• Use docblock for language gaps

Page 159: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Summary

• Type hint everything you can• Use docblock for language gaps• Write tests

Page 160: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Summary

• Type hint everything you can• Use docblock for language gaps• Write tests• Add assertions

Page 161: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Summary

• Type hint everything you can• Use docblock for language gaps• Write tests• Add assertions• Use Value Objects

Page 162: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Summary

• Type hint everything you can• Use docblock for language gaps• Write tests• Add assertions• Use Value Objects• Rename and refactor

Page 163: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Summary

• Use a modern IDE

Page 164: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Questions

Page 165: Is what you’ve coded what you mean? - Dave Liddament · 2020. 5. 28. · First let’s talk about bugs…. @DaveLiddament. Question 1: Who puts bugs in their code? @DaveLiddament

@DaveLiddament

Advice for improving

https://joind.in/talk/8de76