Slide 1

Slide 1 text

Practical Refactoring Thursday, August 22, 13

Slide 2

Slide 2 text

Refactoring Thursday, August 22, 13

Slide 3

Slide 3 text

Refactoring • What is it? Thursday, August 22, 13

Slide 4

Slide 4 text

Refactoring • What is it? • Why would we do it? Thursday, August 22, 13

Slide 5

Slide 5 text

Refactoring • What is it? • Why would we do it? • When do we do it? Thursday, August 22, 13

Slide 6

Slide 6 text

Refactoring • What is it? • Why would we do it? • When do we do it? • How do we do it? Thursday, August 22, 13

Slide 7

Slide 7 text

What? Thursday, August 22, 13

Slide 8

Slide 8 text

Thursday, August 22, 13

Slide 9

Slide 9 text

Code refactoring is the process of changing a computer program's internal structure without modifying its external behavior or existing functionality. Thursday, August 22, 13

Slide 10

Slide 10 text

Why? Thursday, August 22, 13

Slide 11

Slide 11 text

Code is never perfect Thursday, August 22, 13

Slide 12

Slide 12 text

Thursday, August 22, 13

Slide 13

Slide 13 text

Thursday, August 22, 13

Slide 14

Slide 14 text

Code quality Thursday, August 22, 13

Slide 15

Slide 15 text

Code quality • Lessen complexity Thursday, August 22, 13

Slide 16

Slide 16 text

Code quality • Lessen complexity • Increase readability Thursday, August 22, 13

Slide 17

Slide 17 text

Code quality • Lessen complexity • Increase readability • Increase maintainability Thursday, August 22, 13

Slide 18

Slide 18 text

Code quality • Lessen complexity • Increase readability • Increase maintainability • Upgrade performance Thursday, August 22, 13

Slide 19

Slide 19 text

Code quality • Lessen complexity • Increase readability • Increase maintainability • Upgrade performance • Increase stability Thursday, August 22, 13

Slide 20

Slide 20 text

Functionality Thursday, August 22, 13

Slide 21

Slide 21 text

Functionality • New functionality Thursday, August 22, 13

Slide 22

Slide 22 text

Functionality • New functionality • Updated functionality Thursday, August 22, 13

Slide 23

Slide 23 text

Thursday, August 22, 13

Slide 24

Slide 24 text

DON’T... Thursday, August 22, 13

Slide 25

Slide 25 text

DON’T... • Just beautify your code Thursday, August 22, 13

Slide 26

Slide 26 text

DON’T... • Just beautify your code • Refactor because of refactoring Thursday, August 22, 13

Slide 27

Slide 27 text

Selling refactoring to your manager/customer Thursday, August 22, 13

Slide 28

Slide 28 text

What’s in it for them? Thursday, August 22, 13

Slide 29

Slide 29 text

What’s in it for them? • Less bugs Thursday, August 22, 13

Slide 30

Slide 30 text

What’s in it for them? • Less bugs • Faster development Thursday, August 22, 13

Slide 31

Slide 31 text

What’s in it for them? • Less bugs • Faster development • It is part of the process Thursday, August 22, 13

Slide 32

Slide 32 text

Example Thursday, August 22, 13

Slide 33

Slide 33 text

Example if ($order) { if ( ! $order->retryPossible()) { throw new BadRequestHttpException('Order already exists for ShoppingCart'); } } Thursday, August 22, 13

Slide 34

Slide 34 text

Example if ($order) { if ( ! $order->retryPossible()) { throw new BadRequestHttpException('Order already exists for ShoppingCart'); } } public function isOpen() { return $this->getOrder() === null; } Thursday, August 22, 13

Slide 35

Slide 35 text

Example Thursday, August 22, 13

Slide 36

Slide 36 text

Example public function isOpen() { return ( $this->getOrder() === null || $this->getOrder()->retryPossible() === true ); } Thursday, August 22, 13

Slide 37

Slide 37 text

Thursday, August 22, 13

Slide 38

Slide 38 text

A good read http://robots.thoughtbot.com/post/2685998010/ongoing-refactoring-and-client-work Thursday, August 22, 13

Slide 39

Slide 39 text

Thursday, August 22, 13

Slide 40

Slide 40 text

When? Thursday, August 22, 13

Slide 41

Slide 41 text

Continuously Thursday, August 22, 13

Slide 42

Slide 42 text

Really Thursday, August 22, 13

Slide 43

Slide 43 text

Continuously Thursday, August 22, 13

Slide 44

Slide 44 text

Continuously • NOT: Always Thursday, August 22, 13

Slide 45

Slide 45 text

Continuously • NOT: Always • NOT: Immediately Thursday, August 22, 13

Slide 46

Slide 46 text

Continuously Thursday, August 22, 13

Slide 47

Slide 47 text

Continuously • Your code gets better, one step at a time Thursday, August 22, 13

Slide 48

Slide 48 text

Continuously • Your code gets better, one step at a time • You don’t spend ages on refactoring, it’s part of the project Thursday, August 22, 13

Slide 49

Slide 49 text

How? Thursday, August 22, 13

Slide 50

Slide 50 text

Isolation Thursday, August 22, 13

Slide 51

Slide 51 text

Isolation • Small, independant classes and methods Thursday, August 22, 13

Slide 52

Slide 52 text

Isolation • Small, independant classes and methods • Class or method too big? refactor again! Thursday, August 22, 13

Slide 53

Slide 53 text

mysql_connect('127.0.0.1', 'root'); mysql_select_db('refactoring'); $newsSQL = "SELECT * FROM NEWS ORDER BY published_at DESC"; $result = mysql_query($newsSQL); while($row = mysql_fetch_assoc($result)) { echo $row['published_at'] . ': ' . $row['title'].'
'; echo nl2br($row['message']) . '

'; } Thursday, August 22, 13

Slide 54

Slide 54 text

Thursday, August 22, 13

Slide 55

Slide 55 text

require_once('config.php'); $db = new PDO('mysql:dbname='.$db_name.';host='.$db_server, $db_user, $db_pass); $newsSQL = "SELECT * FROM NEWS ORDER BY published_at DESC"; $stmt = $db->prepare($newsSQL); $stmt->execute(); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo $row['published_at'] . ': ' . $row['title'].'
'; echo nl2br($row['message']) . '

'; } Thursday, August 22, 13

Slide 56

Slide 56 text

$db_server = '127.0.0.1'; $db_user = 'root'; $db_pass = ''; $db_name = 'refactoring'; require_once('config.php'); $db = new PDO('mysql:dbname='.$db_name.';host='.$db_server, $db_user, $db_pass); $newsSQL = "SELECT * FROM NEWS ORDER BY published_at DESC"; $stmt = $db->prepare($newsSQL); $stmt->execute(); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo $row['published_at'] . ': ' . $row['title'].'
'; echo nl2br($row['message']) . '

'; } Thursday, August 22, 13

Slide 57

Slide 57 text

Thursday, August 22, 13

Slide 58

Slide 58 text

class NewsRepository { private $db; public function __construct($db) { $this->db = $db; } public function getAllNews() { $newsSQL = "SELECT * FROM NEWS ORDER BY published_at DESC"; $stmt = $this->db->prepare($newsSQL); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } } Thursday, August 22, 13

Slide 59

Slide 59 text

require_once('config.php'); require_once('NewsRepository.php'); $db = new PDO('mysql:dbname='.$db_name.';host='.$db_server, $db_user, $db_pass); $newsRepo = new NewsRepository($db); $news = $newsRepo->getAllNews(); foreach($news as $row) { echo $row['published_at'] . ': ' . $row['title'].'
'; echo nl2br($row['message']) . '

'; } Thursday, August 22, 13

Slide 60

Slide 60 text

Thursday, August 22, 13

Slide 61

Slide 61 text

Thursday, August 22, 13

Slide 62

Slide 62 text

Thursday, August 22, 13

Slide 63

Slide 63 text

Tests Thursday, August 22, 13

Slide 64

Slide 64 text

Tests • Tests are contracts and verifications Thursday, August 22, 13

Slide 65

Slide 65 text

Tests • Tests are contracts and verifications • Contract: Code should do what is tested Thursday, August 22, 13

Slide 66

Slide 66 text

Tests • Tests are contracts and verifications • Contract: Code should do what is tested • Verification: Does it do what is tested? Thursday, August 22, 13

Slide 67

Slide 67 text

Tests • Tests are contracts and verifications • Contract: Code should do what is tested • Verification: Does it do what is tested? • Don’t change the tests Thursday, August 22, 13

Slide 68

Slide 68 text

Tests • Tests are contracts and verifications • Contract: Code should do what is tested • Verification: Does it do what is tested? • Don’t change the tests • Just expand the test Thursday, August 22, 13

Slide 69

Slide 69 text

MockDb = new MockDb(); } public function testGetAllNews() { $newsRepo = new NewsRepository($this->MockDb); // mock repo returns 2 newsitems $news = $newsRepo->getAllNews(); $this->assertEquals(2, count($news)); } } Thursday, August 22, 13

Slide 70

Slide 70 text

public function getAllNews($limit = null) { $newsSQL = "SELECT * FROM NEWS ORDER BY published_at DESC"; if ( ! is_null($limit)) { $newsSQL .= " LIMIT 0, " . $limit; } $stmt = $this->db->prepare($newsSQL); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } Thursday, August 22, 13

Slide 71

Slide 71 text

public function testGetAllNews() { $newsRepo = new NewsRepository($this->MockDb); // mock repo returns 2 newsitems $news = $newsRepo->getAllNews(); $this->assertEquals(2, count($news)); // new functionality: limit the newsitems $news = $newsRepo->getAllNews(1); $this->assertEquals(1, count($news)); } Thursday, August 22, 13

Slide 72

Slide 72 text

When you change your test... Thursday, August 22, 13

Slide 73

Slide 73 text

You change the behaviour of your code Thursday, August 22, 13

Slide 74

Slide 74 text

Tests Thursday, August 22, 13

Slide 75

Slide 75 text

Tests • Untested code? Thursday, August 22, 13

Slide 76

Slide 76 text

Tests • Untested code? • Write tests before refactoring if possible Thursday, August 22, 13

Slide 77

Slide 77 text

Tests • Untested code? • Write tests before refactoring if possible • Write tests during refactoring Thursday, August 22, 13

Slide 78

Slide 78 text

Tests • Untested code? • Write tests before refactoring if possible • Write tests during refactoring • Tested code? Thursday, August 22, 13

Slide 79

Slide 79 text

Tests • Untested code? • Write tests before refactoring if possible • Write tests during refactoring • Tested code? • Expand those tests Thursday, August 22, 13

Slide 80

Slide 80 text

Your IDE can help • Most IDE’s have refactoring support • Scan for usage of methods and classes • http://qafoo.com/blog/ 041_refactoring_browser.html Thursday, August 22, 13

Slide 81

Slide 81 text

So, refactoring Thursday, August 22, 13

Slide 82

Slide 82 text

So, refactoring • What is it? Thursday, August 22, 13

Slide 83

Slide 83 text

So, refactoring • What is it? • Why would we do it? Thursday, August 22, 13

Slide 84

Slide 84 text

So, refactoring • What is it? • Why would we do it? • When do we do it? Thursday, August 22, 13

Slide 85

Slide 85 text

So, refactoring • What is it? • Why would we do it? • When do we do it? • How do we do it? Thursday, August 22, 13

Slide 86

Slide 86 text

Thank you! • https://joind.in/talk/view/8978 • http://php.ingewikkeld.net/ Thursday, August 22, 13