5 ways to awesome-ize your (php) code

Post on 18-May-2015

2.536 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

5 Ways to Awesome-ize Your (PHP) Code

Jeremy Kendall

Thursday, November 7, 13

Thursday, November 7, 13

I love to code

Thursday, November 7, 13

I love to code

I’m terribly forgetful

Thursday, November 7, 13

I love to code

I’m terribly forgetful

I take pictures

Thursday, November 7, 13

I love to code

I’m terribly forgetful

I take pictures

I work at OpenSky

Thursday, November 7, 13

What Are These 5 Ways of Which You Speak?

Thursday, November 7, 13

Use Version Control

Thursday, November 7, 13

Use Version Control

‣ If you aren’t using version control, use git

Thursday, November 7, 13

Use Version Control

‣ If you aren’t using version control, use git

Install git

Thursday, November 7, 13

Use Version Control

‣ If you aren’t using version control, use git

Install git

cd /path/to/project

Thursday, November 7, 13

Use Version Control

‣ If you aren’t using version control, use git

Install git

cd /path/to/project

git init

Thursday, November 7, 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

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

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”

‣ Interactive tutorial at http://try.github.io

Thursday, November 7, 13

or Else . . .

Thursday, November 7, 13

Error Reporting: Turn it Up!

Thursday, November 7, 13

Error Reporting: Turn it Up!‣ Turn error reporting all the way up in dev

Thursday, November 7, 13

Error Reporting: Turn it Up!‣ Turn error reporting all the way up in dev

‣ Helps find existing problems

Thursday, November 7, 13

Error Reporting: Turn it Up!‣ Turn error reporting all the way up in dev

‣ Helps find existing problems

‣ Helps head off future pain

Thursday, November 7, 13

Error Reporting: Turn it Up!‣ Turn error reporting all the way up in dev

‣ Helps find existing problems

‣ Helps head off future pain

‣ Don’t say it’s just an E_NOTICE . . .

Thursday, November 7, 13

Error Reporting: Turn it Up!‣ Turn error reporting all the way up in dev

‣ Helps find existing problems

‣ Helps head off future pain

‣ Don’t say it’s just an E_NOTICE . . .

Thursday, November 7, 13

Error Reporting: Dev

display_errors = Ondisplay_startup_errors = Onerror_reporting = -1log_errors = On

Thursday, November 7, 13

Error Reporting: Prod

display_errors = Offdisplay_startup_errors = Offerror_reporting = E_ALLlog_errors = On

Thursday, November 7, 13

Ditch NIH

Thursday, November 7, 13

Ditch NIH

‣ If you write it all yourself . . .

Thursday, November 7, 13

Ditch NIH

‣ If you write it all yourself . . .

‣ . . . you maintain it all yourself

Thursday, November 7, 13

Ditch NIH

‣ If you write it all yourself . . .

‣ . . . you maintain it all yourself

‣ Eventually you have very little time for either . . .

Thursday, November 7, 13

Real, Actual Work

Thursday, November 7, 13

Real, Actual Work

Thursday, November 7, 13

A Life

Thursday, November 7, 13

Ditch NIH

Thursday, November 7, 13

Ditch NIH

‣ Offload work to the open source community

Thursday, November 7, 13

Ditch NIH

‣ Offload work to the open source community

‣ Install Composer (http://getcomposer.org)

Thursday, November 7, 13

Ditch NIH

‣ Offload work to the open source community

‣ Install Composer (http://getcomposer.org)

‣ Search Packagist (http://packagist.org)

Thursday, November 7, 13

Ditch NIH

‣ Offload work to the open source community

‣ Install Composer (http://getcomposer.org)

‣ Search Packagist (http://packagist.org)

‣ Add dependency to composer.json

Thursday, November 7, 13

Ditch NIH

‣ Offload work to the open source community

‣ Install Composer (http://getcomposer.org)

‣ Search Packagist (http://packagist.org)

‣ Add dependency to composer.json

‣ Run composer install or composer update

Thursday, November 7, 13

Ditch NIH

‣ Offload work to the open source community

‣ 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.

Thursday, November 7, 13

DRY Up Your DB

Thursday, November 7, 13

DRY Up Your DB

‣ If you’re not using PDO, switch now.

Thursday, November 7, 13

DRY Up Your DB

‣ If you’re not using PDO, switch now.

‣ If you’re not using prepared statements, switch now.

Thursday, November 7, 13

DRY Up Your DB

‣ If you’re not using PDO, switch now.

‣ If you’re not using prepared statements, switch now.

‣ Replace connections and queries one at a time

Thursday, November 7, 13

DRY Up Your DB

‣ If you’re not using PDO, switch now.

‣ If you’re not using prepared statements, switch now.

‣ Replace connections and queries one at a time

‣ (or one group at a time)

Thursday, November 7, 13

DRY Up Your DB

‣ If you’re not using PDO, switch now.

‣ 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

Thursday, November 7, 13

DRY Up Your DBclass 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

DRY Up Your DBclass 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

DRY Up Your DBclass 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

DRY Up Your DBclass 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

DRY Up Your DBclass 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

Or Just Pick Something from Packagist

Thursday, November 7, 13

Start Writing Beautiful Code

Thursday, November 7, 13

Start Writing Beautiful Code‣ It’s time for a coding standard!

Thursday, November 7, 13

Start Writing Beautiful Code‣ It’s time for a coding standard!

‣ Makes life so much easier

Thursday, November 7, 13

Start Writing Beautiful Code‣ It’s time for a coding standard!

‣ Makes life so much easier

‣ Pick someone else’s

Thursday, November 7, 13

Start Writing Beautiful Code‣ It’s time for a coding standard!

‣ Makes life so much easier

‣ Pick someone else’s

‣ Use automated tools to enforce

Thursday, November 7, 13

Start Writing Beautiful Code‣ It’s time for a coding standard!

‣ Makes life so much easier

‣ Pick someone else’s

‣ Use automated tools to enforce

‣ php-cs-fixer

Thursday, November 7, 13

Start Writing Beautiful Code‣ It’s time for a coding standard!

‣ Makes life so much easier

‣ Pick someone else’s

‣ Use automated tools to enforce

‣ php-cs-fixer

‣ PHP_CodeSniffer

Thursday, November 7, 13

Start Writing Beautiful Code‣ It’s time for a coding standard!

‣ Makes life so much easier

‣ Pick someone else’s

‣ Use automated tools to enforce

‣ php-cs-fixer

‣ PHP_CodeSniffer

‣ Refactor bit-by-bit

Thursday, November 7, 13

Bonus!

Thursday, November 7, 13

PHP: The Right Way

Thursday, November 7, 13

PHP: The Right Way

‣ Go to http://www.phptherightway.com/

Thursday, November 7, 13

PHP: The Right Way

‣ Go to http://www.phptherightway.com/

‣ Start reading

Thursday, November 7, 13

PHP: The Right Way

‣ Go to http://www.phptherightway.com/

‣ Start reading

‣ Don’t stop reading

Thursday, November 7, 13

PHP: The Right Way

‣ Go to http://www.phptherightway.com/

‣ Start reading

‣ Don’t stop reading

‣ Do it “The Right Way” for 6 months

Thursday, November 7, 13

PHP: The Right Way

‣ Go to http://www.phptherightway.com/

‣ Start reading

‣ Don’t stop reading

‣ Do it “The Right Way” for 6 months

‣ Then pick and choose

Thursday, November 7, 13

Thanks!

jeremy@jeremykendall.net

http://about.me/jeremykendall

@jeremykendall

http://365.jeremykendall.net

Thursday, November 7, 13

top related