Slide 1

Slide 1 text

Practical Refactoring

Slide 2

Slide 2 text

Refactoring • What is it? • Why would we do it? • When do we do it? • How do we do it?

Slide 3

Slide 3 text

What?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Why?

Slide 7

Slide 7 text

Code is never perfect

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Code quality • Lessen complexity • Increase readability • Increase maintainability • Upgrade performance • Increase stability

Slide 11

Slide 11 text

Functionality • New functionality • Updated functionality

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

DON’T... • Just beautify your code • Refactor because of refactoring

Slide 14

Slide 14 text

Selling refactoring to your manager/customer

Slide 15

Slide 15 text

What’s in it for them? • Less bugs • Faster development • It is part of the process

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

A good read http://robots.thoughtbot.com/post/2685998010/ongoing-refactoring-and-client-work

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

When?

Slide 21

Slide 21 text

Continuously

Slide 22

Slide 22 text

Really

Slide 23

Slide 23 text

Continuously • NOT: Always • NOT: Immediately

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

How?

Slide 26

Slide 26 text

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

Slide 27

Slide 27 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']) . '

'; }

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 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']) . '

'; }

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 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); } }

Slide 32

Slide 32 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']) . '

'; }

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 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

Slide 37

Slide 37 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)); } }

Slide 38

Slide 38 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); }

Slide 39

Slide 39 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)); }

Slide 40

Slide 40 text

When you change your test...

Slide 41

Slide 41 text

You change the behaviour of your code

Slide 42

Slide 42 text

Tests • Untested code? • Write tests before refactoring if possible • Write tests during refactoring • Tested code? • Expand those tests

Slide 43

Slide 43 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

Slide 44

Slide 44 text

What about my legacy?

Slide 45

Slide 45 text

So, refactoring • What is it? • Why would we do it? • When do we do it? • How do we do it?

Slide 46

Slide 46 text

Thank you! • https://joind.in/11795 • http://php.ingewikkeld.net