Slide 1

Slide 1 text

Less Code, Less Problems Pass 1(of 2) April 2, 2014
 Kansas City PHP User Group Slides: http://johnkary.net/talks John Kary Code Slinger, Build Breaker @johnkary

Slide 2

Slide 2 text

0 Survey
 the land

Slide 3

Slide 3 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function setBucketIds($bucketIds){...}
 
 public function setBallIds($ballIds){...}
 
 public function setMinBallsPerBucket($count){...}
 
 public function setMinBucketsPerBall($count){...}
 
 public function run(){...}
 
 public function getResults(){...}
 
 public function getResultsByBall(){...}
 
 public function getMinBucketsPerBall(){...}
 
 public function getMinBallsPerBucket(){...}
 
 public function getBallPoolSize(){...}
 
 public function getStatusMessage(){...}
 
 public function createBallIdPool(){...}
 
 public function checkBallIdForBucketId($ballId, $bucketId){...}
 } CLASS

Slide 4

Slide 4 text

TEST

Slide 5

Slide 5 text

TEST

Slide 6

Slide 6 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 private $bucketIds;
 private $ballIds;
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->bucketIds = array('a');
 $this->ballIds = array('200');
 
 $results = $this->runDistribution();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 
 // ...
 
 private function runDistribution() {
 $this->dist->setBucketIds($this->bucketIds);
 $this->dist->setBallIds($this->ballIds);
 $this->dist->setMinBallsPerBucket($this->minBallsPerBucket);
 $this->dist->setMinBucketsPerBall($this->minBucketsPerBall);
 
 $this->dist->run();
 
 return $this->dist->getResults();
 }
 } TEST

Slide 7

Slide 7 text

1 Masking our PAIN

Slide 8

Slide 8 text

1 DRY !== Better

Slide 9

Slide 9 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 private $bucketIds;
 private $ballIds;
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->bucketIds = array('a');
 $this->ballIds = array('200');
 
 $results = $this->runDistribution();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 
 // ...
 
 private function runDistribution() {
 $this->dist->setBucketIds($this->bucketIds);
 $this->dist->setBallIds($this->ballIds);
 $this->dist->setMinBallsPerBucket($this->minBallsPerBucket);
 $this->dist->setMinBucketsPerBall($this->minBucketsPerBall);
 
 $this->dist->run();
 
 return $this->dist->getResults();
 }
 } TEST

Slide 10

Slide 10 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 private $bucketIds;
 private $ballIds;
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->bucketIds = array('a');
 $this->ballIds = array('200');
 
 $this->dist->setBucketIds($this->bucketIds);
 $this->dist->setBallIds($this->ballIds);
 $this->dist->setMinBallsPerBucket($this->minBallsPerBucket);
 $this->dist->setMinBucketsPerBall($this->minBucketsPerBall); ! $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 11

Slide 11 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 private $bucketIds;
 private $ballIds;
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->bucketIds = array('a');
 $this->ballIds = array('200');
 
 $this->dist->setBucketIds($this->bucketIds);
 $this->dist->setBallIds($this->ballIds);
 $this->dist->setMinBallsPerBucket($this->minBallsPerBucket);
 $this->dist->setMinBucketsPerBall($this->minBucketsPerBall); ! $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 12

Slide 12 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 private $bucketIds;
 private $ballIds;
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->bucketIds = array('a');
 $this->ballIds = array('200');
 
 $this->dist->setBucketIds($this->bucketIds);
 $this->dist->setBallIds($this->ballIds);
 $this->dist->setMinBallsPerBucket($this->minBallsPerBucket);
 $this->dist->setMinBucketsPerBall($this->minBucketsPerBall); ! $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 13

Slide 13 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 private $bucketIds;
 private $ballIds;
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->bucketIds = array('a');
 $this->ballIds = array('200');
 
 $this->dist->setBucketIds(array('a'));
 $this->dist->setBallIds(array('200'));
 $this->dist->setMinBallsPerBucket($this->minBallsPerBucket);
 $this->dist->setMinBucketsPerBall($this->minBucketsPerBall); ! $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 14

Slide 14 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 private $bucketIds;
 private $ballIds;
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->bucketIds = array('a');
 $this->ballIds = array('200');
 
 $this->dist->setBucketIds(array('a'));
 $this->dist->setBallIds(array('200'));
 $this->dist->setMinBallsPerBucket($this->minBallsPerBucket);
 $this->dist->setMinBucketsPerBall($this->minBucketsPerBall); ! $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 15

Slide 15 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 private $bucketIds;
 private $ballIds;
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->dist->setBucketIds(array('a'));
 $this->dist->setBallIds(array('200'));
 $this->dist->setMinBallsPerBucket($this->minBallsPerBucket);
 $this->dist->setMinBucketsPerBall($this->minBucketsPerBall); ! $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 16

Slide 16 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->dist->setBucketIds(array('a'));
 $this->dist->setBallIds(array('200'));
 $this->dist->setMinBallsPerBucket($this->minBallsPerBucket);
 $this->dist->setMinBucketsPerBall($this->minBucketsPerBall); ! $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 17

Slide 17 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->dist->setBucketIds(array('a'));
 $this->dist->setBallIds(array('200'));
 $this->dist->setMinBallsPerBucket($this->minBallsPerBucket);
 $this->dist->setMinBucketsPerBall($this->minBucketsPerBall); ! $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 18

Slide 18 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->dist->setBucketIds(array('a'));
 $this->dist->setBallIds(array('200'));
 $this->dist->setMinBallsPerBucket($this->minBallsPerBucket);
 $this->dist->setMinBucketsPerBall($this->minBucketsPerBall); ! $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 19

Slide 19 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->dist->setBucketIds(array('a'));
 $this->dist->setBallIds(array('200'));
 $this->dist->setMinBallsPerBucket(0);
 $this->dist->setMinBucketsPerBall(0); ! $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 20

Slide 20 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->dist->setBucketIds(array('a'));
 $this->dist->setBallIds(array('200'));
 $this->dist->setMinBallsPerBucket(0);
 $this->dist->setMinBucketsPerBall(0); ! $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 21

Slide 21 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0; ! // ... } CLASS

Slide 22

Slide 22 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->dist->setBucketIds(array('a'));
 $this->dist->setBallIds(array('200'));
 $this->dist->setMinBallsPerBucket(0);
 $this->dist->setMinBucketsPerBall(0); ! $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 23

Slide 23 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->dist->setBucketIds(array('a'));
 $this->dist->setBallIds(array('200'));
 $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 24

Slide 24 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->dist->setBucketIds(array('a'));
 $this->dist->setBallIds(array('200'));
 $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 25

Slide 25 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->dist->setBucketIds(array('a'));
 $this->dist->setBallIds(array('200'));
 $this->dist->run();
 
 $results = $this->dist->getResults();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 26

Slide 26 text

use KCPHPUG\BucketBallDistribution;
 
 class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /** @var BucketBallDistribution */
 private $dist;
 
 protected function setUp() {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeOneBucketToOneApp() {
 $this->dist->setBucketIds(array('a'));
 $this->dist->setBallIds(array('200'));
 
 $results = $this->dist->run();
 $expected = array(
 'a' => array('200'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

2Single Responsibility Principle

Slide 29

Slide 29 text

2 A class should have
 one
 and only one
 reason to change

Slide 30

Slide 30 text

2(I added the next part)

Slide 31

Slide 31 text

2 and when it does change
 you should try to
 preserve
 its public API

Slide 32

Slide 32 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function setBucketIds($bucketIds){...}
 
 public function setBallIds($ballIds){...}
 
 public function setMinBallsPerBucket($count){...}
 
 public function setMinBucketsPerBall($count){...}
 
 public function run(){...}
 
 public function getResults(){...}
 
 public function getResultsByBall(){...}
 
 public function getMinBucketsPerBall(){...}
 
 public function getMinBallsPerBucket(){...}
 
 public function getBallPoolSize(){...}
 
 public function getStatusMessage(){...}
 
 public function createBallIdPool(){...}
 
 public function checkBallIdForBucketId($ballId, $bucketId){...}
 } CLASS

Slide 33

Slide 33 text

Smell! ! Too many public methods make it unclear how a class is supposed to be used

Slide 34

Slide 34 text

Public Methods
 communicate how a class can WILL BE used

Slide 35

Slide 35 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function setBucketIds($bucketIds){...}
 
 public function setBallIds($ballIds){...}
 
 public function setMinBallsPerBucket($count){...}
 
 public function setMinBucketsPerBall($count){...}
 
 public function run(){...}
 
 public function getResults(){...}
 
 public function getResultsByBall(){...}
 
 public function getMinBucketsPerBall(){...}
 
 public function getMinBallsPerBucket(){...}
 
 public function getBallPoolSize(){...}
 
 public function getStatusMessage(){...}
 
 public function createBallIdPool(){...}
 
 public function checkBallIdForBucketId($ballId, $bucketId){...}
 } CLASS

Slide 36

Slide 36 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Inform runtime about various state
 public function createBallIdPool()
 public function getBallPoolSize()
 public function checkBallIdForBucketId($ballId, $bucketId)
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 37

Slide 37 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Inform runtime about various state
 public function createBallIdPool()
 public function getBallPoolSize()
 public function checkBallIdForBucketId($ballId, $bucketId)
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 38

Slide 38 text

Find methods only used internally
 and hide them

Slide 39

Slide 39 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Inform runtime about various state
 public function createBallIdPool()
 public function getBallPoolSize()
 public function checkBallIdForBucketId($ballId, $bucketId)
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 40

Slide 40 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Inform runtime about various state
 public function createBallIdPool()
 public function getBallPoolSize()
 public function checkBallIdForBucketId($ballId, $bucketId)
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 41

Slide 41 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Inform runtime about various state
 private function createBallIdPool()
 private function getBallPoolSize()
 private function checkBallIdForBucketId($ballId, $bucketId)
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 42

Slide 42 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 43

Slide 43 text

Smell! ! When an object's public methods must be invoked
 in a specific order to properly use it

Slide 44

Slide 44 text

Object Instantiation Rules

Slide 45

Slide 45 text

An object should be ! ready to use and impossible to misuse! ! after it has been instantiated Object Instantiation Rules

Slide 46

Slide 46 text

An object should be ! ready to use and impossible to misuse! ! after it has been instantiated Object Instantiation Rules Enforce required values in the constructor

Slide 47

Slide 47 text

class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function setUp()
 {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeManyBucketToManyAppsLimit3()
 {
 $this->dist->setBucketIds(array('a','b','c','d'));
 $this->dist->setBallIds(array('201','202','203','204','205','206','207','208' $this->dist->setMinBallsPerBucket(1);
 $this->dist->setMinBucketsPerBall(3);
 
 $results = $this->dist->run();
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 48

Slide 48 text

class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function setUp()
 {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeManyBucketToManyAppsLimit3()
 {
 $this->dist->setBucketIds(array('a','b','c','d'));
 $this->dist->setBallIds(array('201','202','203','204','205','206','207','208' $this->dist->setMinBallsPerBucket(1);
 $this->dist->setMinBucketsPerBall(3);
 
 $results = $this->dist->run();
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 49

Slide 49 text

class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function setUp()
 {
 $this->dist = new BucketBallDistribution();
 }
 
 public function testDistributeManyBucketToManyAppsLimit3()
 {
 $this->dist->setBucketIds(array('a','b','c','d'));
 $this->dist->setBallIds(array('201','202','203','204','205','206','207','208' $this->dist->setMinBallsPerBucket(1);
 $this->dist->setMinBucketsPerBall(3);
 
 $results = $this->dist->run();
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 }
 } TEST

Slide 50

Slide 50 text

TEST class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function build($minBucketsPerBall = 0, $minBallsPerBucket = 0)
 {
 $this->dist = new BucketBallDistribution($minBucketsPerBall, $minBallsPerBuck }
 
 public function testDistributeManyBucketToManyAppsLimit3()
 {
 $this->dist->setBucketIds(array('a','b','c','d'));
 $this->dist->setBallIds(array(‘201','202','203','204','205','206','207','208' $this->dist->setMinBallsPerBucket(1);
 $this->dist->setMinBucketsPerBall(3);
 
 $results = $this->dist->run();
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 }
 }

Slide 51

Slide 51 text

TEST class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function build($minBucketsPerBall = 0, $minBallsPerBucket = 0)
 {
 $this->dist = new BucketBallDistribution($minBucketsPerBall, $minBallsPerBuck }
 
 public function testDistributeManyBucketToManyAppsLimit3()
 {
 $this->build(3, 1);
 
 $this->dist->setBucketIds(array('a','b','c','d'));
 $this->dist->setBallIds(array(‘201','202','203','204','205','206','207','208' $this->dist->setMinBallsPerBucket(1);
 $this->dist->setMinBucketsPerBall(3);
 
 $results = $this->dist->run();
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 }


Slide 52

Slide 52 text

TEST class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function build($minBucketsPerBall = 0, $minBallsPerBucket = 0)
 {
 $this->dist = new BucketBallDistribution($minBucketsPerBall, $minBallsPerBuck }
 
 public function testDistributeManyBucketToManyAppsLimit3()
 {
 $this->build(3, 1);
 
 $this->dist->setBucketIds(array('a','b','c','d'));
 $this->dist->setBallIds(array(‘201','202','203','204','205','206','207','208' $this->dist->setMinBallsPerBucket(1);
 $this->dist->setMinBucketsPerBall(3);
 
 $results = $this->dist->run();
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 }


Slide 53

Slide 53 text

TEST class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function build($minBucketsPerBall = 0, $minBallsPerBucket = 0)
 {
 $this->dist = new BucketBallDistribution($minBucketsPerBall, $minBallsPerBuck }
 
 public function testDistributeManyBucketToManyAppsLimit3()
 {
 $this->build(3, 1);
 
 $this->dist->setBucketIds(array('a','b','c','d'));
 $this->dist->setBallIds(array(‘201','202','203','204','205','206','207','208' $this->dist->setMinBallsPerBucket(1);
 $this->dist->setMinBucketsPerBall(3);
 
 $results = $this->dist->run();
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 }


Slide 54

Slide 54 text

TEST class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function build($minBucketsPerBall = 0, $minBallsPerBucket = 0)
 {
 $this->dist = new BucketBallDistribution($minBucketsPerBall, $minBallsPerBuck }
 
 public function testDistributeManyBucketToManyAppsLimit3()
 {
 $this->build(3, 1);
 
 $this->dist->setBucketIds(array('a','b','c','d'));
 $this->dist->setBallIds(array(‘201','202','203','204','205','206','207','208' 
 $results = $this->dist->run();
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 }
 }

Slide 55

Slide 55 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 56

Slide 56 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 57

Slide 57 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0) $this->minBucketsPerBall = $minBucketsPerBall; $this->minBallsPerBucket = $minBallsPerBucket;
 } ! // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 CLASS

Slide 58

Slide 58 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0) $this->minBucketsPerBall = $minBucketsPerBall; $this->minBallsPerBucket = $minBallsPerBucket;
 } ! // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 CLASS

Slide 59

Slide 59 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0) $this->minBucketsPerBall = $minBucketsPerBall; $this->minBallsPerBucket = $minBallsPerBucket;
 } ! // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 CLASS

Slide 60

Slide 60 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0) $this->minBucketsPerBall = $minBucketsPerBall; $this->minBallsPerBucket = $minBallsPerBucket;
 } ! // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 CLASS

Slide 61

Slide 61 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0) $this->minBucketsPerBall = $minBucketsPerBall; $this->minBallsPerBucket = $minBallsPerBucket;
 } ! // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 CLASS

Slide 62

Slide 62 text

An object’s configuration should not change after instantiation API Design Principles

Slide 63

Slide 63 text

An object’s configuration should not change after instantiation If you need a different configuration create another instance API Design Principles

Slide 64

Slide 64 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0) $this->minBucketsPerBall = $minBucketsPerBall; $this->minBallsPerBucket = $minBallsPerBucket;
 } ! // Set Configuration
 public function setMinBucketsPerBall($count)
 public function setMinBallsPerBucket($count)
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 CLASS

Slide 65

Slide 65 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0) $this->minBucketsPerBall = $minBucketsPerBall; $this->minBallsPerBucket = $minBallsPerBucket;
 } ! // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall() ! // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0) $this->minBucketsPerBall = $minBucketsPerBall; $this->minBallsPerBucket = $minBallsPerBucket;
 } ! // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 68

Slide 68 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0) $this->minBucketsPerBall = $minBucketsPerBall; $this->minBallsPerBucket = $minBallsPerBucket;
 } ! // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 69

Slide 69 text

Do not expose getters for values you don’t use yset Do not expose getters for configuration API Design Principles

Slide 70

Slide 70 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0) $this->minBucketsPerBall = $minBucketsPerBall; $this->minBallsPerBucket = $minBallsPerBucket;
 }
 
 // Get Configuration (but based on runtime data?)
 public function getMinBallsPerBucket()
 public function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 71

Slide 71 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0) $this->minBucketsPerBall = $minBucketsPerBall; $this->minBallsPerBucket = $minBallsPerBucket;
 }
 
 // Get Configuration (but based on runtime data?)
 private function getMinBallsPerBucket()
 private function getMinBucketsPerBall()
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 72

Slide 72 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 // Set Configuration
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0) $this->minBucketsPerBall = $minBucketsPerBall; $this->minBallsPerBucket = $minBallsPerBucket;
 }
 
 // Set runtime data
 public function setBucketIds($bucketIds)
 public function setBallIds($ballIds)
 
 // Main runtime algorithm
 public function run()
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage()
 }
 CLASS

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

TEST class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function build($minBucketsPerBall = 0, $minBallsPerBucket = 0)
 {
 $this->dist = new BucketBallDistribution($minBucketsPerBall, $minBallsPerBuck }
 
 public function testDistributeManyBucketToManyAppsLimit3()
 {
 $this->build(3, 1);
 
 $this->dist->setBucketIds(array('a','b','c','d'));
 $this->dist->setBallIds(array(‘201','202','203','204','205','206','207','208' 
 $results = $this->dist->run();
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 }
 }

Slide 75

Slide 75 text

TEST class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function build($minBucketsPerBall = 0, $minBallsPerBucket = 0)
 {
 $this->dist = new BucketBallDistribution($minBucketsPerBall, $minBallsPerBuck }
 
 public function testDistributeManyBucketToManyAppsLimit3()
 {
 $this->build(3, 1);
 
 $this->dist->setBucketIds(array('a','b','c','d'));
 $this->dist->setBallIds(array(‘201','202','203','204','205','206','207','208' 
 $results = $this->dist->run();
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 }
 }

Slide 76

Slide 76 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! public function setBucketIds(array $bucketIds) {
 $this->bucketIds = $bucketIds;
 } ! public function setBallIds(array $ballIds) {
 $this->ballIds = $ballIds;
 }
 
 public function run() {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($this->bucketIds, $this->ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 77

Slide 77 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! public function setBucketIds(array $bucketIds) {
 $this->bucketIds = $bucketIds;
 } ! public function setBallIds(array $ballIds) {
 $this->ballIds = $ballIds;
 }
 
 public function run() {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($this->bucketIds, $this->ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 78

Slide 78 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! public function setBucketIds(array $bucketIds) {
 $this->bucketIds = $bucketIds;
 } ! public function setBallIds(array $ballIds) {
 $this->ballIds = $ballIds;
 }
 
 public function run() {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($this->bucketIds, $this->ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 79

Slide 79 text

Class and Methods Principles

Slide 80

Slide 80 text

Class and Methods Principles STATELESS Do not store data during execution

Slide 81

Slide 81 text

Class and Methods Principles STATELESS Do not store data during execution DETERMINISTIC Given the same input, the return value is identical

Slide 82

Slide 82 text

Class and Methods Principles STATELESS Do not store data during execution IDEMPOTENT Given the same input, the state of the system is identical when the method completes DETERMINISTIC Given the same input, the return value is identical

Slide 83

Slide 83 text

TEST class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function build($minBucketsPerBall = 0, $minBallsPerBucket = 0)
 {
 $this->dist = new BucketBallDistribution($minBucketsPerBall, $minBallsPerBuck }
 
 public function testDistributeManyBucketToManyAppsLimit3()
 {
 $this->build(3, 1);
 
 $this->dist->setBucketIds(array('a','b','c','d'));
 $this->dist->setBallIds(array(‘201','202','203','204','205','206','207','208' 
 $results = $this->dist->run();
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 }
 }

Slide 84

Slide 84 text

TEST class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function build($minBucketsPerBall = 0, $minBallsPerBucket = 0)
 {
 $this->dist = new BucketBallDistribution($minBucketsPerBall, $minBallsPerBuck }
 
 public function testDistributeManyBucketToManyAppsLimit3()
 {
 $this->build(3, 1);
 
 $this->dist->setBucketIds(array('a','b','c','d'));
 $this->dist->setBallIds(array(‘201','202','203','204','205','206','207','208' 
 $results = $this->dist->run();
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 }
 }

Slide 85

Slide 85 text

TEST class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function build($minBucketsPerBall = 0, $minBallsPerBucket = 0)
 {
 $this->dist = new BucketBallDistribution($minBucketsPerBall, $minBallsPerBuck }
 
 public function testDistributeManyBucketToManyAppsLimit3() {
 $this->build(3, 1);
 
 $bucketIds = array('a', 'b', 'c', 'd');
 $ballIds = array('201', '202', '203', '204', '205', '206', '207', '208', '209 
 $results = $this->dist->run($bucketIds, $ballIds);
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 } }

Slide 86

Slide 86 text

TEST class BucketBallDistributionTest extends \PHPUnit_Framework_TestCase
 {
 /**
 * @var BucketBallDistribution
 */
 private $dist;
 
 protected function build($minBucketsPerBall = 0, $minBallsPerBucket = 0)
 {
 $this->dist = new BucketBallDistribution($minBucketsPerBall, $minBallsPerBuck }
 
 public function testDistributeManyBucketToManyAppsLimit3() {
 $this->build(3, 1);
 
 $bucketIds = array('a', 'b', 'c', 'd');
 $ballIds = array('201', '202', '203', '204', '205', '206', '207', '208', '209 
 $results = $this->dist->run($bucketIds, $ballIds);
 $expected = array(
 'a' => array('201','205','209','203','207','204','208'),
 'b' => array('202','206','210','204','208','201','205','209'),
 'c' => array('203','207','201','205','209','202','206','210'),
 'd' => array('204','208','202','206','210','203','207'),
 );
 
 $this->assertEquals($expected, $results);
 } }

Slide 87

Slide 87 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! public function setBucketIds(array $bucketIds) {
 $this->bucketIds = $bucketIds;
 } ! public function setBallIds(array $ballIds) {
 $this->ballIds = $ballIds;
 }
 
 public function run() {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($this->bucketIds, $this->ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 88

Slide 88 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! public function setBucketIds(array $bucketIds) {
 $this->bucketIds = $bucketIds;
 } ! public function setBallIds(array $ballIds) {
 $this->ballIds = $ballIds;
 }
 
 public function run() {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($this->bucketIds, $this->ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 89

Slide 89 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! public function setBucketIds(array $bucketIds) {
 $this->bucketIds = $bucketIds;
 } ! public function setBallIds(array $ballIds) {
 $this->ballIds = $ballIds;
 }
 
 public function run(array $bucketIds, array $ballIds) {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($this->bucketIds, $this->ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 90

Slide 90 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! public function setBucketIds(array $bucketIds) {
 $this->bucketIds = $bucketIds;
 } ! public function setBallIds(array $ballIds) {
 $this->ballIds = $ballIds;
 }
 
 public function run(array $bucketIds, array $ballIds) {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($this->bucketIds, $this->ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 91

Slide 91 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! public function setBucketIds(array $bucketIds) {
 $this->bucketIds = $bucketIds;
 } ! public function setBallIds(array $ballIds) {
 $this->ballIds = $ballIds;
 }
 
 public function run(array $bucketIds, array $ballIds) {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($bucketIds, $ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 92

Slide 92 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! public function setBucketIds(array $bucketIds) {
 $this->bucketIds = $bucketIds;
 } ! public function setBallIds(array $ballIds) {
 $this->ballIds = $ballIds;
 }
 
 public function run(array $bucketIds, array $ballIds) {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($bucketIds, $ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 93

Slide 93 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall; private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! ! ! ! ! ! ! 
 
 public function run(array $bucketIds, array $ballIds) {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($bucketIds, $ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 94

Slide 94 text

class BucketBallDistribution
 {
 private $minBallsPerBucket;
 private $minBucketsPerBall; private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! ! ! ! ! ! ! 
 
 public function run(array $bucketIds, array $ballIds) {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($bucketIds, $ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 95

Slide 95 text

class BucketBallDistribution
 {
 private $minBallsPerBucket;
 private $minBucketsPerBall; private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! ! ! ! ! ! ! 
 
 public function run(array $bucketIds, array $ballIds) {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($bucketIds, $ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 96

Slide 96 text

class BucketBallDistribution
 {
 private $minBallsPerBucket;
 private $minBucketsPerBall; private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! ! ! ! ! ! ! 
 
 public function run(array $bucketIds, array $ballIds) {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($bucketIds, $ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 97

Slide 97 text

class BucketBallDistribution
 {
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){} ! ! ! ! ! ! ! 
 
 public function run(array $bucketIds, array $ballIds) {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($bucketIds, $ballIds); while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

class BucketBallDistribution
 {
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){ 
 // Main runtime algorithm
 public function run(array $bucketIds, array $ballIds)
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage(array $bucketIds, array $ballIds)
 }
 CLASS

Slide 100

Slide 100 text

class BucketBallDistribution
 {
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){ 
 // Main runtime algorithm
 public function run(array $bucketIds, array $ballIds)
 
 // Get results after runtime
 public function getResults()
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage(array $bucketIds, array $ballIds)
 }
 CLASS

Slide 101

Slide 101 text

class BucketBallDistribution
 {
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 
 public function getResults() {
 return $this->results;
 } ! public function run() {
 $this->results = array();
 $this->resultsByBall = array();
 
 $ballPool = $this->createBallIdPool($this->bucketIds, $this->ballI while ($ballPool ...) {
 // ...
 $this->results[$bucketId][] = $ballId;
 // ...
 }
 
 return $this->results;
 } } CLASS

Slide 102

Slide 102 text

class BucketBallDistribution
 {
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall = 0, $minBallsPerBucket = 0){ 
 // Main runtime algorithm
 public function run(array $bucketIds, array $ballIds)
 
 // Get results after runtime
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage(array $bucketIds, array $ballIds)
 }
 CLASS

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

ass BucketBallDistribution
 private $bucketIds = array();
 private $ballIds = array();
 private $minBallsPerBucket = 0;
 private $minBucketsPerBall = 0;
 private $ballPool = array();
 private $results = array();
 private $resultsByBall = array();
 public function setBucketIds($bucketIds){...}
 public function setBallIds($ballIds){...}
 public function setMinBallsPerBucket($count){...}
 public function setMinBucketsPerBall($count){...}
 public function run(){...}
 public function getResults(){...}
 public function getResultsByBall(){...}
 public function getMinBucketsPerBall(){...}
 public function getMinBallsPerBucket(){...}
 public function getBallPoolSize(){...}
 public function getStatusMessage(){...}
 public function createBallIdPool(){...}
 public function checkBallIdForBucketId($ballId, $bucketId){...}
 class BucketBallDistribution
 {
 private $minBallsPerBucket;
 private $minBucketsPerBall;
 private $results = array();
 private $resultsByBall = array();
 
 public function __construct($minBucketsPerBall 
 // Main runtime algorithm
 public function run(array $bucketIds, array $ba 
 // Get results after runtime
 public function getResultsByBall()
 
 // Text describing results
 public function getStatusMessage(array $bucketI 


Slide 105

Slide 105 text

Further Learning Refactoring: Improving the Design of Existing Code
 Martin Fowler, 2000 http://refactoring.com Less: The Path to Better Design
 Sandi Metz (GoRuCo 2011) http://vimeo.com/26330100 Therapeutic Refactoring
 Katrina Owen (2012) http://www.youtube.com/watch?v=J4dlF0kcThQ

Slide 106

Slide 106 text

Questions John Kary Code Slinger, Build Breaker @johnkary http://johnkary.net/talks