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

Clean Code: Refactoring

Jeff Carouth
February 08, 2014

Clean Code: Refactoring

There are very few "write once" applications in reality. As developers we must keep up with constant changes and evolution of problems and our code should reflect this. In this talk we will look at practical examples of code that is not "bad" code but needs a little attention. We will look at techniques for refactoring it to a more readable, understandable, and maintainable state.

Jeff Carouth

February 08, 2014
Tweet

More Decks by Jeff Carouth

Other Decks in Technology

Transcript

  1. Clean code is simple; it is easy to understand how

    it works and what it does. Saturday, February 8, 14
  2. Rule #1 Write dirty code, then clean it. You cannot

    write clean code at first. > Saturday, February 8, 14
  3. Rule #1 Write dirty code, then clean it. You cannot

    write clean code at first. Your first pass at solving a problem will not be your best. > > Saturday, February 8, 14
  4. Rule #1 Write dirty code, then clean it. You cannot

    write clean code at first. Your first pass at solving a problem will not be your best. > > Successive refinement through refactoring is the key to clean code. > Saturday, February 8, 14
  5. Rule #2 Leave the code cleaner than you found it.

    Shamelessly borrowed from the Boy Scouts of America. > Saturday, February 8, 14
  6. Rule #2 Leave the code cleaner than you found it.

    Shamelessly borrowed from the Boy Scouts of America. Incremental changes are the way to go. > > Saturday, February 8, 14
  7. Rule #2 Leave the code cleaner than you found it.

    Shamelessly borrowed from the Boy Scouts of America. Incremental changes are the way to go. > > Even if the only change you make is cosmetic, you are maintaining clean code. > Saturday, February 8, 14
  8. class AccountService { public function find($id) { if (is_string($id) &&

    strlen($id) == 36) { $statement = $this->db->prepare( "SELECT * FROM accounts WHERE id = :account_id" ); $statement->execute(['account_id' => $id]); if ($statement->rowCount() == 1) { $data = $statement->fetch(); $account = new Account(); $account->setId($data['id']); $account->setName($data['name']); return $account; } else { return null; } } else { throw new \InvalidArgumentException('ID is not valid'); } } } Saturday, February 8, 14
  9. class AccountService { public function find($id) { if (is_string($id) &&

    strlen($id) == 36) { $statement = $this->db->prepare( "SELECT * FROM accounts WHERE id = :account_id" ); $statement->execute(['account_id' => $id]); if ($statement->rowCount() == 1) { $data = $statement->fetch(); $account = new Account(); $account->setId($data['id']); $account->setName($data['name']); return $account; } else { return null; } } else { throw new \InvalidArgumentException('ID is not valid'); } } } Saturday, February 8, 14
  10. When you have a significant number of nested conditionals, you

    can refactor them to be more understandable using guard clauses. Refactoring Saturday, February 8, 14
  11. class AccountService { public function find($id) { if (is_string($id) &&

    strlen($id) == 36) { $statement = $this->db->prepare( "SELECT * FROM accounts WHERE id = :account_id" ); $statement->execute(['account_id' => $id]); if ($statement->rowCount() == 1) { $data = $statement->fetch(); $account = new Account(); $account->setId($data['id']); $account->setName($data['name']); return $account; } else { return null; } } else { throw new \InvalidArgumentException('ID is not valid'); } } } Saturday, February 8, 14
  12. class AccountService { public function find($id) { if (!is_string($id) ||

    strlen($id) != 36) { throw new \InvalidArgumentException('Account ID is not valid'); } $statement = $this->db->prepare( "SELECT * FROM accounts WHERE id = :account_id" ); $statement->execute(['account_id' => $id]); if ($statement->rowCount() == 1) { $data = $statement->fetch(); $account = new Account(); $account->setId($data['id']); $account->setName($data['name']); return $account; } else { return null; } } } Saturday, February 8, 14
  13. Complicated or boolean expressions using magic numbers or otherwise non-obvious

    logic should be refactored to a descriptively named helper method. Refactoring Saturday, February 8, 14
  14. class AccountService { public function find($id) { if (!$this->isValidId($id)) {

    throw new \InvalidArgumentException('Account ID is not valid'); } //snip – my favorite slide-only refactoring } private function isValidId($id) { return is_string($id) && strlen($id) == 36; } } Saturday, February 8, 14
  15. public function find($id) { if (!$this->isValidId($id)) { throw new \InvalidArgumentException('Account

    ID is not valid'); } $statement = $this->db->prepare( "SELECT * FROM accounts WHERE id = :account_id" ); $statement->execute(['account_id' => $id]); if ($statement->rowCount() == 1) { $data = $statement->fetch(); $account = new Account(); $account->setId($data['id']); $account->setName($data['name']); return $account; } else { return null; } } Saturday, February 8, 14
  16. Extract what could be common functionality, or even functionality that

    belongs on a different object into methods. Refactoring Saturday, February 8, 14
  17. private function fetchAccountDataById($id) { $statement = $this->db->prepare( "SELECT * FROM

    accounts WHERE id = :account_id" ); $statement->execute(['account_id' => $id]); if ($statement->rowCount() == 1) { $data = $statement->fetch(); } else { $data = array(); } return $data; } Saturday, February 8, 14
  18. class AccountService { public function find($id) { if (!$this->isValidId($id)) {

    throw new \InvalidArgumentException('Account ID is not valid'); } $data = $this->fetchAccountDataById($id); if (empty($data)) { return null; } $account = new Account(); $account->setId($data['id']); $account->setName($data['name']); return $account; } private function fetchAccountDataById($id) { //snip – slides are not very tall } } Saturday, February 8, 14
  19. class AccountService { public function find($id) { if (!$this->isValidId($id)) {

    throw new \InvalidArgumentException('Account ID is not valid'); } $data = $this->fetchAccountDataById($id); if (empty($data)) { return null; } $account = new Account(); $account->setId($data['id']); $account->setName($data['name']); return $account; } } Saturday, February 8, 14
  20. class AccountService { public function find($id) { if (!$this->isValidId($id)) {

    throw new \InvalidArgumentException('Account ID is not valid'); } $data = $this->fetchAccountDataById($id); return $this->buildAccountFromData($data); } private function buildAccountFromData($data) { if (empty($data)) { return null; } $account = new Account(); $account->setId($data['id']); $account->setName($data['name']); return $account; } // snip } Saturday, February 8, 14
  21. Replace instances of returning null in place of an object

    with returning instances of a NullObject where possible and appropriate. Refactoring Saturday, February 8, 14
  22. $accountService = new AccountService(new MyPdoWrapper(...)); $account = $accountService->fetch('...'); if (null

    === $account) { //bail. we don't have a valid account } if (!$account->isActive()) { //error the account is not active } Saturday, February 8, 14
  23. class AccountService { public function find($id) { if (!$this->isValidId($id)) {

    throw new \InvalidArgumentException('Account ID is not valid'); } $data = $this->fetchAccountDataById($id); return $this->buildAccountFromData($data); } private function buildAccountFromData($data) { if (empty($data)) { return new NullAccount(); } $account = new Account(); $account->setId($data['id']); $account->setName($data['name']); return $account; } // snip } Saturday, February 8, 14
  24. Extract methods or logic into classes when the code isn’t

    necessarily relevant to the object you are working in. Refactoring Saturday, February 8, 14
  25. class AccountService { // snip private function fetchAccountDataById($id) { $statement

    = $this->db->prepare( "SELECT * FROM accounts WHERE id = :account_id" ); $statement->execute(['account_id' => $id]); if ($statement->rowCount() == 1) { $data = $statement->fetch(); } else { $data = array(); } return $data; } // snip } Saturday, February 8, 14
  26. class AccountDataProvider { public function __construct(PDO $pdo) { $this->dbh =

    $pdo; } public function fetchById($id) { $statement = $this->db->prepare( "SELECT * FROM accounts WHERE id = :account_id" ); $statement->execute(['account_id' => $id]); if ($statement->rowCount() == 1) { $data = $statement->fetch(); } else { $data = array(); } return $data; } } Saturday, February 8, 14
  27. class AccountService { // snip public function find($id) { if

    (!$this->isValidId($id)) { throw new \InvalidArgumentException('Account ID is not valid'); } $data = $this->provider->fetchById($id); return $this->buildAccountFromData($data); } // snip } Saturday, February 8, 14
  28. class AccountService { public function find($id) { if (isset($id) &&

    strlen($id) == 36) { $statement = $this->db->prepare( "SELECT * FROM accounts WHERE id = :account_id" ); $statement->execute(['account_id' => $id]); if ($statement->rowCount() == 1) { $data = $statement->fetch(); $account = new Account(); $account->setId($data['id']); $account->setName($data['name']); return $account; } else { return null; } } else { throw new \InvalidArgumentException('ID is not valid'); } } } Saturday, February 8, 14
  29. class AccountService { public function find($id) { if (!$this->isValidId($id)) {

    throw new \InvalidArgumentException('Account ID is not valid'); } $data = $this->provider->fetchById($id); return $this->buildAccountFromData($data); } // snip } Saturday, February 8, 14
  30. class Product { public function createUrl($store, $isMobile = false, $includeDomain

    = true) { $url = ""; if (!$store->isDefault()) { $trySSL = false; $forceToCore = false; if ($this->featureToggle) { if ($includeDomain) { $url .= $this->config->secureProtocol.$this->config->storeDomainName; } $url .= "/checkout?product_id=".$this->productId; if ($this->isDated()) { $url .= "&date=".date("Y-m-d", strtotime($this->date)); } } else { if ($includeDomain) { $url .= $store->GetFullUrl($trySSL, $isMobile); } $url .= "/resort_detail.php?ProductId=".$this->productId; if ($this->isDated()) { $url .= "&StartDate=".date("Y-m-d", strtotime($this->date)); } } } else { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } if ($this->featureToggle) { if ($includeDomain) { $url .= $this->config->secureProtocol.$domainName; } $url .= "/checkout?product_id=".$this->productId; if ($this->isDated()) { $url .= "&date=".date("Y-m-d", strtotime($this->date)); } } else { if ($includeDomain) { $url .= "http://".$domainName; } $url .= "/resort_detail.php?ProductId=".$this->productId; if ($this->isDated()) { $url .= "&StartDate=".date("Y-m-d", strtotime($this->date)); } } } } } Saturday, February 8, 14
  31. { $url = ""; if (!$store->isDefault()) { $trySSL = false;

    $forceToCore = false; if ($this->featureToggle) { if ($includeDomain) { $url .= $this->config->secureProtocol.$this->config->sto } $url .= "/checkout?product_id=".$this->productId; if ($this->isDated()) { $url .= "&date=".date("Y-m-d", strtotime($this->date)); } } else { if ($includeDomain) { $url .= $store->GetFullUrl($trySSL, $isMobile); } $url .= "/product_detail.php?ProductId=".$this->productId; if ($this->isDated()) { $url .= "&StartDate=".date("Y-m-d", strtotime($this->dat } } } else { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } if ($this->featureToggle) { Saturday, February 8, 14
  32. $url .= "&StartDate=".date("Y-m-d", strtotime($this->date) } } } else { $domainName

    = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } if ($this->featureToggle) { if ($includeDomain) { $url .= $this->config->secureProtocol.$domainName; } $url .= "/checkout?product_id=".$this->productId; if ($this->isDated()) { $url .= "&date=".date("Y-m-d", strtotime($this->date)); } } else { if ($includeDomain) { $url .= "http://".$domainName; } $url .= "/product_detail.php?ProductId=".$this->productId; if ($this->isDated()) { $url .= "&StartDate=".date("Y-m-d", strtotime($this->date) } } } } } Saturday, February 8, 14
  33. { $url = ""; if (!$store->isDefault()) { $trySSL = false;

    $forceToCore = false; if ($this->featureToggle) { if ($includeDomain) { $url .= $this->config->secureProtocol.$this->config->sto } $url .= "/checkout?product_id=".$this->productId; if ($this->isDated()) { $url .= "&date=".date("Y-m-d", strtotime($this->date)); } } else { if ($includeDomain) { $url .= $store->GetFullUrl($trySSL, $isMobile); } $url .= "/product_detail.php?ProductId=".$this->productId; if ($this->isDated()) { $url .= "&StartDate=".date("Y-m-d", strtotime($this->dat } } } else { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } if ($this->featureToggle) { Saturday, February 8, 14
  34. class Product { public function createUrl($store, $isMobile = false, $includeDomain

    = true { $url = ""; if ($includeDomain) { if ($store->isDefault()) { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } if ($this->featureToggle) { $url .= $this->config->secureProtocol.$domainName; } else { $url .= "http://".$domainName; } } else { if ($this->featureToggle) { $url .= $this->config->secureProtocol.$this->config->store } else { $url .= $store->GetFullUrl($trySSL, $isMobile); } } } if (!$store->isDefault()) { $trySSL = false; $forceToCore = false; Saturday, February 8, 14
  35. class Product { public function createUrl($store, $isMobile = false, $includeDomain

    = true { $url = ""; if ($includeDomain) { if ($store->isDefault()) { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } if ($this->featureToggle) { $url .= $this->config->secureProtocol.$domainName; } else { $url .= "http://".$domainName; } } else { if ($this->featureToggle) { $url .= $this->config->secureProtocol.$this->config->store } else { $url .= $store->GetFullUrl($trySSL, $isMobile); } } } if (!$store->isDefault()) { $trySSL = false; $forceToCore = false; Saturday, February 8, 14
  36. class Product { public function createUrl($store, $isMobile = false, $includeDomain

    = true { $url = ""; $protocol = $this->config->secureProtocol; if ($includeDomain) { if ($store->isDefault()) { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } if ($this->featureToggle) { $url .= $protocol.$domainName; } else { $url .= "http://".$domainName; } } else { if ($this->featureToggle) { $url .= $protocol.$this->config->storeDomainName; } else { $url .= $store->GetFullUrl($trySSL, $isMobile); } } } if (!$store->isDefault()) { Saturday, February 8, 14
  37. class Product { public function createUrl($store, $isMobile = false, $includeDomain

    = true { $url = ""; $protocol = $this->config->secureProtocol; if ($includeDomain) { if ($store->isDefault()) { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } if (!$this->featureToggle) { $protocol = "http://"; } $url .= $protocol.$domainName; } else { if ($this->featureToggle) { $url .= $protocol.$this->config->storeDomainName; } else { $url .= $store->GetFullUrl($trySSL, $isMobile); } } } Saturday, February 8, 14
  38. class Product { public function createUrl($store, $isMobile = false, $includeDomain

    = true { $url = ""; $protocol = $this->config->secureProtocol; if ($includeDomain) { if ($store->isDefault()) { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } if (!$this->featureToggle) { $protocol = "http://"; } $url .= $protocol.$domainName; } else { if ($this->featureToggle) { $url .= $protocol.$this->config->storeDomainName; } else { $url .= $store->GetFullUrl($trySSL, $isMobile); } } } Saturday, February 8, 14
  39. class Product { public function createUrl($store, $isMobile = false, $includeDomain

    = true { $url = ""; $protocol = $this->config->secureProtocol; if ($includeDomain) { $domainName = $this->config->storeDomainName; if ($store->isDefault()) { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } } if ($store->isDefault()) { if (!$this->featureToggle) { $protocol = "http://"; } $url .= $protocol.$domainName; } else { if ($this->featureToggle) { $url .= $protocol.$domainName; } else { $url .= $store->GetFullUrl($trySSL, $isMobile); } } Saturday, February 8, 14
  40. class Product { public function createUrl($store, $isMobile = false, $includeDomain

    = true { $url = ""; $protocol = $this->config->secureProtocol; if ($includeDomain) { $domainName = $this->config->storeDomainName; if ($store->isDefault()) { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } } if ($store->isDefault() && !$this->featureToggle) { $protocol = "http://"; } $url .= $protocol.$domainName; if (!$store->isDefault()) { if ($this->featureToggle) { $url .= $protocol.$domainName; } else { $url .= $store->GetFullUrl($trySSL, $isMobile); } } Saturday, February 8, 14
  41. $url = ""; $protocol = $this->config->secureProtocol; if ($includeDomain) { $domainName

    = $this->config->storeDomainName; if ($store->isDefault()) { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } } if ($store->isDefault() && !$this->featureToggle) { $protocol = "http://"; } $url .= $protocol.$domainName; if (!$store->isDefault()) { if ($this->featureToggle) { $url .= $protocol.$domainName; } else { $url .= $store->GetFullUrl($trySSL, $isMobile); } } } if (!$store->isDefault()) { $trySSL = false; $forceToCore = false; Saturday, February 8, 14
  42. $url = ""; $protocol = $this->config->secureProtocol; if ($includeDomain) { $domainName

    = $this->config->storeDomainName; if ($store->isDefault()) { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } } if ($store->isDefault() && !$this->featureToggle) { $protocol = "http://"; } $url .= $protocol.$domainName; if (!$store->isDefault()) { if (!$this->featureToggle) { $url .= $store->GetFullUrl($trySSL, $isMobile); } } } if (!$store->isDefault()) { $trySSL = false; $forceToCore = false; if ($this->featureToggle) { Saturday, February 8, 14
  43. $url = ""; $protocol = $this->config->secureProtocol; if ($includeDomain) { $domainName

    = $this->config->storeDomainName; if ($store->isDefault()) { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } } if ($store->isDefault() && !$this->featureToggle) { $protocol = "http://"; } $url .= $protocol.$domainName; if (!$store->isDefault() && !$this->featureToggle) { $trySSL = false; $url .= $store->GetFullUrl($trySSL, $isMobile); } } if (!$store->isDefault()) { if ($this->featureToggle) { $url .= "/checkout?product_id=".$this->productId; if ($this->isDated()) { $url .= "&date=".date("Y-m-d", strtotime($this->date)); } Saturday, February 8, 14
  44. } if (!$store->isDefault()) { if ($this->featureToggle) { $url .= "/checkout?product_id=".$this->productId;

    if ($this->isDated()) { $url .= "&date=".date("Y-m-d", strtotime($this->date)); } } else { $url .= "/product_detail.php?ProductId=".$this->productId; if ($this->isDated()) { $url .= "&StartDate=".date("Y-m-d", strtotime($this->date) } } } else { if ($this->featureToggle) { $url .= "/checkout?product_id=".$this->productId; if ($this->isDated()) { $url .= "&date=".date("Y-m-d", strtotime($this->date)); } } else { $url .= "/product_detail.php?ProductId=".$this->productId; if ($this->isDated()) { $url .= "&StartDate=".date("Y-m-d", strtotime($this->date) } } } } } Saturday, February 8, 14
  45. } if ($this->featureToggle) { if (!$store->isDefault()) { $url .= "/checkout?product_id=".$this->productId;

    if ($this->isDated()) { $url .= "&date=".date("Y-m-d", strtotime($this->date)); } } else { $url .= "/checkout?product_id=".$this->productId; if ($this->isDated()) { $url .= "&date=".date("Y-m-d", strtotime($this->date)); } } } else { if (!$store->isDefault()) { $url .= "/resort_detail.php?ProductId=".$this->productId; if ($this->isDated()) { $url .= "&StartDate=".date("Y-m-d", strtotime($this->date) } } else { $url .= "/resort_detail.php?ProductId=".$this->productId; if ($this->isDated()) { $url .= "&StartDate=".date("Y-m-d", strtotime($this->date) } } } } } Saturday, February 8, 14
  46. } if ($this->featureToggle) { $url .= "/checkout?product_id=".$this->productId; } else {

    $url .= "/resort_detail.php?ProductId=".$this->productId; } if ($this->isDated) { $dateParam = $this->featureToggle ? "start_date" : "StartDate"; $url = "&{$dateParam}=" . date("Y-m-d", strtotime($this->date)); } } } Saturday, February 8, 14
  47. class Product { public function createUrl($store, $isMobile = false, $includeDomain

    = true { $url = ""; $protocol = $this->config->secureProtocol; if ($includeDomain) { $domainName = $this->config->storeDomainName; if ($store->isDefault()) { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } } if ($store->isDefault() && !$this->featureToggle) { $protocol = "http://"; } $url .= $protocol.$domainName; if (!$store->isDefault() && !$this->featureToggle) { $trySSL = false; $url .= $store->GetFullUrl($trySSL, $isMobile); } } if ($this->featureToggle) { Saturday, February 8, 14
  48. class Product { public function createUrl($store, $isMobile = false, $includeDomain

    = true { $url = ""; $protocol = $this->config->secureProtocol; if ($includeDomain) { $domainName = $this->getDomainNameForStore($store); // snip } // snip } private function getDomainNameForStore($store) { $domainName = $this->config->storeDomainName; if ($store->isDefault()) { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } } } } Saturday, February 8, 14
  49. class Product { public function createUrl($store, $isMobile = false, $includeDomain

    = true) { $url = ""; if (!$store->isDefault()) { $trySSL = false; $forceToCore = false; if ($this->featureToggle) { if ($includeDomain) { $url .= $this->config->secureProtocol.$this->config->storeDomainName; } $url .= "/checkout?product_id=".$this->productId; if ($this->isDated()) { $url .= "&date=".date("Y-m-d", strtotime($this->date)); } } else { if ($includeDomain) { $url .= $store->GetFullUrl($trySSL, $isMobile); } $url .= "/resort_detail.php?ProductId=".$this->productId; if ($this->isDated()) { $url .= "&StartDate=".date("Y-m-d", strtotime($this->date)); } } } else { $domainName = $this->config->domainName; if (!$domainName) { $domainName = $this->config->desktopDomainName; } if ($this->featureToggle) { if ($includeDomain) { $url .= $this->config->secureProtocol.$domainName; } $url .= "/checkout?product_id=".$this->productId; if ($this->isDated()) { $url .= "&date=".date("Y-m-d", strtotime($this->date)); } } else { if ($includeDomain) { $url .= "http://".$domainName; } $url .= "/resort_detail.php?ProductId=".$this->productId; if ($this->isDated()) { $url .= "&StartDate=".date("Y-m-d", strtotime($this->date)); } } } } } Saturday, February 8, 14
  50. class Product { public function createUrl($store, $isMobile = false, $includeDomain

    = true) { $url = ""; $protocol = $this->config->secureProtocol; if ($includeDomain) { $domainName = $this->getDomainNameForStore($store); if ($store->isDefault() && !$this->featureToggle) { $protocol = "http://"; } $url .= $protocol.$domainName; if (!$store->isDefault() && !$this->featureToggle) { $trySSL = false; $url .= $store->GetFullUrl($trySSL, $isMobile); } } if ($this->featureToggle) { $url .= "/checkout?product_id=".$this->productId; } else { $url .= "/product_detail.php?ProductId=".$this->productId; } if ($this->isDated) { $dateParam = $this->featureToggle ? "start_date" : "StartDate"; $url = "&{$dateParam}=" . date("Y-m-d", strtotime($this->date)); } } private function getDomainNameForStore($store) Saturday, February 8, 14
  51. recap Code should be understandable by humans. Clean code is

    readable, cared for, efficient, extensible, and simple. Saturday, February 8, 14
  52. recap Code should be understandable by humans. Clean code is

    readable, cared for, efficient, extensible, and simple. Write dirty code and successively refine. Saturday, February 8, 14