Writing Readable PHP

Writing Readable PHP

  • 22 videos
  • 02 h 37 min
  • Beginner
  • $89.00

Overview

Are you...

  • Staring at your old code as if someone else wrote it?
  • Coding in a team without clean code habits?
  • Struggling with consistent naming?

Stop the madness! Let’s learn how to write readable PHP in this hands-on course.

What will the course get me?

  • A collection of bite size tips that make your code a joy to read for you, your co-workers, and future self. These tips are aimed towards developers who know the basics of PHP and want to improve their craft.
  • Learn how to use static analysis to ensure that your code is understandable and correct.
  • Get access to our learning platform to track progress and discuss good coding habits with other course participants and the authors.
  • View high-quality videos on both clean coding and static analysis.

Code Examples

In general, code is more frequently read than written. That’s why you should optimize for readability, which is easier than you would think. It’s a matter of taking care of the details. Here are some examples from the chapters of the course:

Optimize readablity for the happy path

In a previous part we've mentioned that grouping code in paragraphs is good for readability. There's another advantage: it visualises the steps a function takes to get to a certain results. It makes sense to order those steps in a way that's natural to you, the programmer.

Our favorite technique of ordering a function's steps is by first handling all edge cases, and keep the last step in a function for the most important part: the "happy path". Here's an example where the happy path is handled first. Try to read it: you'll notice that, besides some code overhead, it's also more confusing to understand what this code does from a first read.

CleanShot 2022-12-12-12 02 46

So instead, let's first check and handle all edge cases followed by the happy path as the last step of the function. Conveniently, it doesn't need to be wrapped in a condition anymore if all edge cases have already been handled.

CleanShot 2022-12-12-12 02 52

Conditionally building up queries

In your project, you might need to build up queries conditionally.

CleanShot 2022-12-12-12 01 37

You can clean this up with the when method. The query will only be modified if the first argument is truthy.

CleanShot 2022-12-12-12 01 43

Reviews

No review yet.

Authors


Christoph Rumpel Profile Picture

Christoph Rumpel

Hi! I'm a web developer from Vienna, Austria. I code. I write about code. I teach how I write code. I talk at conferences about how I teach to code. I have been working with Laravel since version 4, and it is part of my daily work since then.

Freek Van der Herten Profile Picture

Freek Van der Herten

I'm a PHP developer and Laravel enthusiast. Most of my time is spent at Spatie of which I'm the co-owner. Whenever my colleagues and I stumble upon a problem that we can solve in a clean way, we extract our solution so other developers can use it. Together with my colleagues I regularly release packages that have been downloaded more than 500 million times.