• Like
5 Ways to Awesome-ize Your (PHP) Code
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

5 Ways to Awesome-ize Your (PHP) Code

  • 2,425 views
Published

5 principles you can apply *right now* to make your code better. The principles apply to most languages, the implementation is PHP specific.

5 principles you can apply *right now* to make your code better. The principles apply to most languages, the implementation is PHP specific.

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,425
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
8
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. 5 Ways to Awesome-ize Your (PHP) Code Jeremy Kendall Thursday, November 7, 13
  • 2. Thursday, November 7, 13
  • 3. I love to code Thursday, November 7, 13
  • 4. I love to code I’m terribly forgetful Thursday, November 7, 13
  • 5. I love to code I’m terribly forgetful I take pictures Thursday, November 7, 13
  • 6. I love to code I’m terribly forgetful I take pictures I work at OpenSky Thursday, November 7, 13
  • 7. What Are These 5 Ways of Which You Speak? Thursday, November 7, 13
  • 8. Use Version Control Thursday, November 7, 13
  • 9. Use Version Control ‣ Thursday, November 7, 13 If you aren’t using version control, use git
  • 10. Use Version Control ‣ If you aren’t using version control, use git Install git Thursday, November 7, 13
  • 11. Use Version Control ‣ If you aren’t using version control, use git Install git cd /path/to/project Thursday, November 7, 13
  • 12. Use Version Control ‣ If you aren’t using version control, use git Install git cd /path/to/project git init Thursday, November 7, 13
  • 13. Use Version Control ‣ If you aren’t using version control, use git Install git cd /path/to/project git init git add . Thursday, November 7, 13
  • 14. Use Version Control ‣ If you aren’t using version control, use git Install git cd /path/to/project git init git add . git commit -m “Just saved the company” Thursday, November 7, 13
  • 15. Use Version Control ‣ If you aren’t using version control, use git Install git cd /path/to/project git init git add . git commit -m “Just saved the company” ‣ Thursday, November 7, 13 Interactive tutorial at http://try.github.io
  • 16. or Else . . . Thursday, November 7, 13
  • 17. Error Reporting: Turn it Up! Thursday, November 7, 13
  • 18. Error Reporting: Turn it Up! ‣ Thursday, November 7, 13 Turn error reporting all the way up in dev
  • 19. Error Reporting: Turn it Up! ‣ Helps find existing problems ‣ Turn error reporting all the way up in dev Thursday, November 7, 13
  • 20. Error Reporting: Turn it Up! ‣ Helps find existing problems ‣ Helps head off future pain ‣ Turn error reporting all the way up in dev Thursday, November 7, 13
  • 21. Error Reporting: Turn it Up! ‣ Helps find existing problems ‣ Helps head off future pain ‣ Don’t say it’s just an E_NOTICE . . . ‣ Turn error reporting all the way up in dev Thursday, November 7, 13
  • 22. Error Reporting: Turn it Up! ‣ Helps find existing problems ‣ Helps head off future pain ‣ Don’t say it’s just an E_NOTICE . . . ‣ Turn error reporting all the way up in dev Thursday, November 7, 13
  • 23. Error Reporting: Dev display_errors = On display_startup_errors = On error_reporting = -1 log_errors = On Thursday, November 7, 13
  • 24. Error Reporting: Prod display_errors = Off display_startup_errors = Off error_reporting = E_ALL log_errors = On Thursday, November 7, 13
  • 25. Ditch NIH Thursday, November 7, 13
  • 26. Ditch NIH ‣ Thursday, November 7, 13 If you write it all yourself . . .
  • 27. Ditch NIH ‣ . . . you maintain it all yourself ‣ If you write it all yourself . . . Thursday, November 7, 13
  • 28. Ditch NIH ‣ . . . you maintain it all yourself ‣ Eventually you have very little time for either . . . ‣ If you write it all yourself . . . Thursday, November 7, 13
  • 29. Real, Actual Work Thursday, November 7, 13
  • 30. Real, Actual Work Thursday, November 7, 13
  • 31. A Life Thursday, November 7, 13
  • 32. Ditch NIH Thursday, November 7, 13
  • 33. Ditch NIH ‣ Thursday, November 7, 13 Offload work to the open source community
  • 34. Ditch NIH ‣ Install Composer (http://getcomposer.org) ‣ Offload work to the open source community Thursday, November 7, 13
  • 35. Ditch NIH ‣ Install Composer (http://getcomposer.org) ‣ Search Packagist (http://packagist.org) ‣ Offload work to the open source community Thursday, November 7, 13
  • 36. Ditch NIH ‣ Install Composer (http://getcomposer.org) ‣ Search Packagist (http://packagist.org) ‣ Add dependency to composer.json ‣ Offload work to the open source community Thursday, November 7, 13
  • 37. Ditch NIH ‣ Install Composer (http://getcomposer.org) ‣ Search Packagist (http://packagist.org) ‣ Add dependency to composer.json ‣ Run composer install or composer update ‣ Offload work to the open source community Thursday, November 7, 13
  • 38. Ditch NIH ‣ Install Composer (http://getcomposer.org) ‣ Search Packagist (http://packagist.org) ‣ Add dependency to composer.json ‣ Run composer install or composer update ‣ Everyone needs logging. Go install monolog. ‣ Offload work to the open source community Thursday, November 7, 13
  • 39. DRY Up Your DB Thursday, November 7, 13
  • 40. DRY Up Your DB ‣ Thursday, November 7, 13 If you’re not using PDO, switch now.
  • 41. DRY Up Your DB ‣ If you’re not using prepared statements, switch now. ‣ If you’re not using PDO, switch now. Thursday, November 7, 13
  • 42. DRY Up Your DB ‣ If you’re not using prepared statements, switch now. ‣ Replace connections and queries one at a time ‣ If you’re not using PDO, switch now. Thursday, November 7, 13
  • 43. DRY Up Your DB ‣ If you’re not using prepared statements, switch now. ‣ Replace connections and queries one at a time ‣ (or one group at a time) ‣ If you’re not using PDO, switch now. Thursday, November 7, 13
  • 44. DRY Up Your DB ‣ If you’re not using prepared statements, switch now. ‣ Replace connections and queries one at a time ‣ (or one group at a time) ‣ Combine with simple data access objects ‣ If you’re not using PDO, switch now. Thursday, November 7, 13
  • 45. DRY Up Your DB class UserDao { protected $db; public function __construct(PDO $db) { $this->db = $db; } public function find($id) { $sql = 'SELECT * FROM users WHERE id = :id'; $stmt = $this->db->prepare($sql); $stmt->bindValue(':id', $id); $stmt->execute(); return $stmt->fetch(); } } Thursday, November 7, 13
  • 46. DRY Up Your DB class UserDao { protected $db; public function __construct(PDO $db) { $this->db = $db; } public function find($id) { $sql = 'SELECT * FROM users WHERE id = :id'; $stmt = $this->db->prepare($sql); $stmt->bindValue(':id', $id); $stmt->execute(); return $stmt->fetch(); } } Thursday, November 7, 13
  • 47. DRY Up Your DB class UserDao { protected $db; public function __construct(PDO $db) { $this->db = $db; } public function find($id) { $sql = 'SELECT * FROM users WHERE id = :id'; $stmt = $this->db->prepare($sql); $stmt->bindValue(':id', $id); $stmt->execute(); return $stmt->fetch(); } } Thursday, November 7, 13
  • 48. DRY Up Your DB class UserDao { protected $db; public function __construct(PDO $db) { $this->db = $db; } public function find($id) { $sql = 'SELECT * FROM users WHERE id = :id'; $stmt = $this->db->prepare($sql); $stmt->bindValue(':id', $id); $stmt->execute(); return $stmt->fetch(); } } Thursday, November 7, 13
  • 49. DRY Up Your DB class UserDao { protected $db; public function __construct(PDO $db) { $this->db = $db; } public function find($id) { $sql = 'SELECT * FROM users WHERE id = :id'; $stmt = $this->db->prepare($sql); $stmt->bindValue(':id', $id); $stmt->execute(); return $stmt->fetch(); } } Thursday, November 7, 13
  • 50. Or Just Pick Something from Packagist Thursday, November 7, 13
  • 51. Start Writing Beautiful Code Thursday, November 7, 13
  • 52. Start Writing Beautiful Code ‣ Thursday, November 7, 13 It’s time for a coding standard!
  • 53. Start Writing Beautiful Code ‣ Makes life so much easier ‣ It’s time for a coding standard! Thursday, November 7, 13
  • 54. Start Writing Beautiful Code ‣ Makes life so much easier ‣ Pick someone else’s ‣ It’s time for a coding standard! Thursday, November 7, 13
  • 55. Start Writing Beautiful Code ‣ Makes life so much easier ‣ Pick someone else’s ‣ Use automated tools to enforce ‣ It’s time for a coding standard! Thursday, November 7, 13
  • 56. Start Writing Beautiful Code ‣ Makes life so much easier ‣ Pick someone else’s ‣ Use automated tools to enforce ‣ php-cs-fixer ‣ It’s time for a coding standard! Thursday, November 7, 13
  • 57. Start Writing Beautiful Code ‣ Makes life so much easier ‣ Pick someone else’s ‣ Use automated tools to enforce ‣ php-cs-fixer ‣ PHP_CodeSniffer ‣ It’s time for a coding standard! Thursday, November 7, 13
  • 58. Start Writing Beautiful Code ‣ Makes life so much easier ‣ Pick someone else’s ‣ Use automated tools to enforce ‣ php-cs-fixer ‣ PHP_CodeSniffer ‣ Refactor bit-by-bit ‣ It’s time for a coding standard! Thursday, November 7, 13
  • 59. Bonus! Thursday, November 7, 13
  • 60. PHP: The Right Way Thursday, November 7, 13
  • 61. PHP: The Right Way ‣ Thursday, November 7, 13 Go to http://www.phptherightway.com/
  • 62. PHP: The Right Way ‣ Start reading ‣ Go to http://www.phptherightway.com/ Thursday, November 7, 13
  • 63. PHP: The Right Way ‣ Start reading ‣ Don’t stop reading ‣ Go to http://www.phptherightway.com/ Thursday, November 7, 13
  • 64. PHP: The Right Way ‣ Start reading ‣ Don’t stop reading ‣ Do it “The Right Way” for 6 months ‣ Go to http://www.phptherightway.com/ Thursday, November 7, 13
  • 65. PHP: The Right Way ‣ Start reading ‣ Don’t stop reading ‣ Do it “The Right Way” for 6 months ‣ Then pick and choose ‣ Go to http://www.phptherightway.com/ Thursday, November 7, 13
  • 66. Thanks! jeremy@jeremykendall.net http://about.me/jeremykendall @jeremykendall http://365.jeremykendall.net Thursday, November 7, 13