Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bringing your application into 2014

Bringing your application into 2014

This should probably be titled "A practical guide to surviving legacy code"

Slides with speaker notes can be found at http://cl.ly/470i2k2O2T1U

Michael Heap

October 05, 2014
Tweet

More Decks by Michael Heap

Other Decks in Technology

Transcript

  1. $dbhost = 'localhost:3036';! $dbuser = 'root';! $dbpass = 'rootpassword';! $conn

    = mysql_connect($dbhost, $dbuser, $dbpass);! if(! $conn )! {! die('Could not connect: ' . mysql_error());! }! $sql = 'SELECT id, name FROM employee';! ! mysql_select_db('test_db');! $retval = mysql_query( $sql, $conn );! if(! $retval )! {! die('Could not get data: ' . mysql_error());! }! while($row = mysql_fetch_array($retval, MYSQL_ASSOC))! {! echo "EMP ID :{$row['id']} <br> ".! "EMP NAME : {$row['name']} <br> ".! "--------------------------------<br>";! } ! echo "Fetched data successfully\n";! mysql_close($conn);!
  2. PSR

  3. <?php! ! $dbhost = 'localhost:3036';! $dbuser = 'root';! $dbpass =

    'rootpassword';! $conn = mysql_connect($dbhost, $dbuser, $dbpass);! if(! $conn )! {! die('Could not connect: ' . mysql_error());! }! $sql = 'SELECT id name FROM employee';! ! mysql_select_db('test_db');! $retval = mysql_query( $sql, $conn );! if(! $retval )! {! die('Could not get data: ' . mysql_error());! }! while($row = mysql_fetch_array($retval, MYSQL_ASSOC))! {! echo "EMP ID :{$row['id']} <br> ".! "EMP NAME : {$row['name']} <br> ".! "--------------------------------<br>";! } ! echo "Fetched data successfully\n";! mysql_close($conn);! ?>!
  4. <?php! ! $dbhost = 'localhost:3036';! $dbuser = 'root';! $dbpass =

    'rootpassword';! $conn = mysql_connect($dbhost, $dbuser, $dbpass);! if(! $conn )! {! die('Could not connect: ' . mysql_error());! }! $sql = 'SELECT id, name FROM employee';! ! mysql_select_db('test_db');! $retval = mysql_query( $sql, $conn );! if(! $retval )! {! die('Could not get data: ' . mysql_error());! }! while($row = mysql_fetch_array($retval, MYSQL_ASSOC))! {! echo "EMP ID :{$row['id']} <br> ".! "EMP NAME : {$row['name']} <br> ".! "--------------------------------<br>";! } ! echo "Fetched data successfully\n";! mysql_close($conn);! ?>!
  5. <?php! ! $dbhost = 'localhost:3036';! $dbuser = 'root';! $dbpass =

    'rootpassword';! $conn = mysql_connect($dbhost, $dbuser, $dbpass);! if(! $conn )! {! die('Could not connect: ' . mysql_error());! }! $sql = 'SELECT id, name FROM employee';! ! mysql_select_db('test_db');! $retval = mysql_query( $sql, $conn );! if(! $retval )! {! die('Could not get data: ' . mysql_error());! }! while($row = mysql_fetch_array($retval, MYSQL_ASSOC))! {! echo "EMP ID :{$row['id']} <br> ".! "EMP NAME : {$row['name']} <br> ".! "--------------------------------<br>";! } ! echo "Fetched data successfully\n";! mysql_close($conn);! ?>!
  6. …! ! mysql_select_db('test_db');! $retval = mysql_query( $sql, $conn );! if(!

    $retval )! {! die('Could not get data: ' . mysql_error());! }! while($row = mysql_fetch_array($retval, MYSQL_ASSOC))! {! echo "EMP ID :{$row['id']} <br> ".! "EMP NAME : {$row['name']} <br> ".! "--------------------------------<br>";! } ! ! …!
  7. …! ! mysql_select_db('test_db');! $retval = mysql_query( $sql, $conn );! if(!

    $retval )! {! die('Could not get data: ' . mysql_error());! }! ! $data = array();! ! while($row = mysql_fetch_array($retval, MYSQL_ASSOC))! {! ! $data[] = $row;! }! ! foreach ($data as $row)! echo "EMP ID :{$row['id']} <br> ".! "EMP NAME : {$row['name']} <br> ".! "--------------------------------<br>";! } ! ! …
  8. …! ! $data = array(! ! array("id" => 1, "name"

    => "Michael ")! );! ! foreach ($data as $row)! echo "EMP ID :{$row['id']} <br> ".! "EMP NAME : {$row['name']} <br> ".! "--------------------------------<br>";! } ! ! …!
  9. <?php! ! $dbhost = 'localhost:3036';! $dbuser = 'root';! $dbpass =

    'rootpassword';! $conn = mysql_connect($dbhost, $dbuser, $dbpass);! if(! $conn )! {! die('Could not connect: ' . mysql_error());! }! $sql = 'SELECT id, name FROM employee';! ! mysql_select_db('test_db');! $retval = mysql_query( $sql, $conn );! if(! $retval )! {! die('Could not get data: ' . mysql_error());! }! ! while($row = mysql_fetch_array($retval, MYSQL_ASSOC))! {! ! $data[] = $row;! }! ! include 'employee_view.php';! ! mysql_close($conn);! ?>!
  10. <?php! ! $data = array(! ! array("id" => 1, "name"

    => "Michael ")! );! ! include 'employee_view.php';! ! ?>!
  11. <?php! ! $dbhost = 'localhost:3036';! $dbuser = 'root';! $dbpass =

    'rootpassword';! $conn = mysql_connect($dbhost, $dbuser, $dbpass);! if(! $conn )! {! die('Could not connect: ' . mysql_error());! }! $sql = 'SELECT id, name FROM employee';! ! mysql_select_db('test_db');! $retval = mysql_query( $sql, $conn );! if(! $retval )! {! die('Could not get data: ' . mysql_error());! }! ! while($row = mysql_fetch_array($retval, MYSQL_ASSOC))! {! ! $data[] = $row;! }! ! include 'employee_view.php';! ! mysql_close($conn);! ?>!
  12. <?php! ! $dbhost = 'localhost:3036';! $dbuser = 'root';! $dbpass =

    'rootpassword';! $conn = mysql_connect($dbhost, $dbuser, $dbpass);! if(! $conn )! {! die('Could not connect: ' . mysql_error());! }! $sql = 'SELECT id, name FROM employee';! ! mysql_select_db('test_db');! $retval = mysql_query( $sql, $conn );! if(! $retval )! {! die('Could not get data: ' . mysql_error());! }! ! while($row = mysql_fetch_array($retval, MYSQL_ASSOC))! {! ! $data[] = $row;! }! ! include 'employee_view.php';! ! mysql_close($conn);! ?>!
  13. <?php! ! include ‘db_setup.php';! ! $sql = 'SELECT id, name

    FROM employee';! ! $retval = mysql_query( $sql, $conn );! if(! $retval )! {! die('Could not get data: ' . mysql_error());! }! ! while($row = mysql_fetch_array($retval, MYSQL_ASSOC))! {! ! $data[] = $row;! }! ! include 'employee_view.php';! ! include ‘db_teardown.php';! ?>!
  14. <?php! ! include ‘db_setup.php';! ! $sql = 'SELECT id, name

    FROM employee_archive’;! ! $retval = mysql_query( $sql, $conn );! if(! $retval )! {! die('Could not get data: ' . mysql_error());! }! ! while($row = mysql_fetch_array($retval, MYSQL_ASSOC))! {! ! $data[] = $row;! }! ! include 'employee_view.php';! ! include ‘db_teardown.php';! ?>!
  15. <?php! ! class Db {! ! public function __construct($user, $password,

    $database) {! ! ! // Connection logic goes here! ! }! ! ! public function query($sql) {! ! ! $retval = mysql_query( $sql, $this->conn );! ! ! if(! $retval )! ! ! {! ! ! die('Could not get data: ' . mysql_error());! ! ! }! ! ! ! while($row = mysql_fetch_array($retval, MYSQL_ASSOC))! ! ! {! ! ! ! $data[] = $row;! ! ! }! ! ! ! return $data;! ! ! }! }!
  16. <?php! ! include 'db_setup.php';! ! $data = $db->query('SELECT id, name

    FROM employee');! include 'employee_view.php';! ! include 'db_teardown.php';! ?>!
  17. <?php! ! class EmployeeRepo {! ! public function getAllEmployees() {!

    ! ! global $db;! ! ! return $db->query('SELECT id, name FROM employee');! ! }! }!
  18. <?php! ! class EmployeeRepo {! ! public function __construct() {!

    ! ! global $db;! ! ! $this->db = $db;! ! }! ! ! public function getAllEmployees() {! ! ! return $this->db->query('SELECT id, name FROM employee');! ! }! }!
  19. <?php! ! include 'db_setup.php';! include 'employee_repo.php';! ! $repo = new

    EmployeeRepo;! $data = $repo->getAllEmployees();! include 'employee_view.php';! ! include 'db_teardown.php';! ?>!
  20. <?php! ! include ‘vendor/autoloader.php’;! include ‘db_setup.php’;! ! $repo = new

    EmployeeRepo;! $data = $repo->getAllEmployees();! include 'employee_view.php';! ?>!
  21. <?php! ! class EmployeeRepo {! ! public function __construct() {!

    ! ! global $db;! ! ! $this->db = $db;! ! }! ! ! public function getAllEmployees() {! ! ! return $this->db->query('SELECT id, name FROM employee');! ! }! }!
  22. <?php! ! class EmployeeRepo {! ! public function __construct($db) {!

    ! ! $this->db = $db;! ! }! ! ! public function getAllEmployees() {! ! ! return $this->db->query(‘SELECT id, name FROM employee');! ! }! }!
  23. <?php! ! include ‘vendor/autoload.php';! include ‘db_setup.php’;! ! $repo = new

    EmployeeRepo($db);! $data = $repo->getAllEmployees();! include 'employee_view.php';! ?>!
  24. <?php! ! include 'vendor/autoload.php';! include 'db_setup.php';! ! $repo = new

    EmployeeRepo($db);! $data = $repo->getAllEmployees();! ! $response = new Response();! ob_start();! include 'employee_view.php';! $reponse->content = ob_get_contents();! ob_end_clean();! ! $response->send();! ?>!
  25. “Don’t Repeat Yourself” was never about code. It’s about knowledge.

    It’s about cohesion. If two pieces of code represent the exact same knowledge, they will always change together. Having to change them both is risky: you might forget one of them. On the other hand, if two identical pieces of code represent different knowledge, they will change independently. De-duplicating them introduces risk, because changing the knowledge for one object, might accidentally change it for the other object. - http://verraes.net/2014/08/dry-is-about-knowledge/