Refactoring PHP: 4 actionable tips
Christop Rumpel shares some good tips on how to refactor code.
Read more [christoph-rumpel.com]
Posts tagged with code quality
Christop Rumpel shares some good tips on how to refactor code.
Read more [christoph-rumpel.com]
Ross Tuck doesn't blog often, but when he does, it's worth your time!
Read more [www.rosstuck.com]
Join 9,500+ smart developers
Every month I share what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.
No spam. Unsubscribe anytime. You can also follow me on X.
Readability of code can be vastly improved by decreasing indentation. In this video we'll reverse conditions and early returns to accomplish this. If you want to see more videos like this one, head over to the video section at Spatie.
– frederickvanbrabant.com - submitted by Frederick Vanbrabant
My buddy Frederick wrote an interesting post on why he always checks out the admin panel of an app instead of the code.
Read more [frederickvanbrabant.com]
Tim MacDonald shares a nice way to go about this.
Read more [laravel-news.com]
A nice example by Evert Pot on how you can make static analysers do their work better.
Read more [evertpot.com]
Kevlin Henney wrote a good post on the importance of naming things well.
Good naming is part of design. It sets expectations and communicates a model, showing how something should be understood and used. If you mean to tell the reader getMillisSince1970, don’t say getTime.
Read more [medium.com]
Extracting doesn’t guarantee simplicity. Sometimes extracting code into another module means I’ve smeared one “conceptual module” in my brain into two physical files. Now I can’t look at the whole thing at once, and their internal wiring is more prominent than it deserves. Oops.
— Dan Abramov (@dan_abramov) September 25, 2019
Read more [twitter.com]
Stefan Zweifel shared his GitHub action that can automatically fix code style issues using prettier and php-cs-fixer
One workflow is really like, is to run prettier and php-cs-fixer to automatically format my code and commit the fixed files back to the repository.
Read more [stefanzweifel.io]
? In many cases avoiding `else` and using early returns will greatly improve readability of your code.
— Freek Van der Herten (@freekmurze) 17 september 2019
I wish somebody would have told me this sooner in my career.
Contrived example in the image. Real world example: https://t.co/oOsSGAJLZm#bestpractices pic.twitter.com/YgqB0kMIpg
Read more [twitter.com]
In this old, but still relevant, blog post Craig Davis explains what guard clauses are and how they can be used to clean up your code.
We’ll first explore several versions of a sample method from a hypothetical billing system. For these purposes, we’ll assume this is code in an existing system and we’ll look at refactoring it to reduce complexity and make it easier for a programmer to understand. The first example will be trivial enough to easily understand, but we’ll build on it in the final examples.
Read more [engineering.helpscout.com]
– dev.to
Here a some great tips on how to write robust software.
NASA's JPL, which is responsible for some of the most awesomest science out there, is quite famous for its Power of 10 rules (see original paper). Indeed, if you are going to send a robot on Mars with a 40 minutes ping and no physical access to it then you pretty damn well should make sure that your code doesn't have bugs.
Read more [dev.to]
In an older but still relevant post, Jeff Atwood shares why you shouldn't use boolean parameters and how this translation to UI.
Read more [blog.codinghorror.com]
Matthew Rocklin reminds us that sometimes you shouldn't extract code.
However, there is also a cost to this behavior. When a new reader encounters this code, they need to jump between many function definitions in many files. This non-linear reading process requires more mental focus than reading linear code.
Read more [matthewrocklin.com]
Refactoring is an excellent book written by Martin Fowler. He recently released a second edition. I'm reading it now and can recommend it to anyone interested in writing better code. Here's an interview with the author on the second edition of the book.
InfoQ interviewed Fowler about the major changes in the 2nd edition of Refactoring, how to recognize code smells and refactor code, how code reviews and refactoring support each other, what tech leads can do to encourage refactoring, the benefits refactoring brings, using tools for refactoring, and mob programming.
Read more [www.infoq.com]
In a new post on his blog Senior CEO Frederick Vanbrabant explains the Integration Operation Segregation Principle, which is programmerspeak for splitting your code into nice little testable bits.
As you can see the Integration Operation Segregation Principle is just a long and complicated term to describe something very simple. This all might seem like a lot of work, but it’s worth it. Your code and especially your tests will thank you later
Read more [frederickvanbrabant.com]
? Reviewing your code for symmetry is a great way to improve readability.
— Jason McCreary (@gonedark) September 17, 2018
To get started, check for paired naming, consistent tone, and relative complexity. From there any remaining asymmetric bits will be easy to spot.
Here's a before and after snippet from the @OhDearApp. ? pic.twitter.com/KAhzYBxzSd
Read more [twitter.com]
Whenever you find yourself using || or && in an if statement, consider extracting the conditions of that if statement to a dedicated method with early returns. It's more readable and it makes debugging much easier.
— Freek Van der Herten (@freekmurze) September 12, 2018
Example: https://t.co/hUcq29uP54
Read more [twitter.com]
Ross Tuck makes the case that not all code is equal.
Most teams follow the Broken Window Theory, fearing even a single tradeoff starts the slide down a slippery slope. This can reduce discussion (read: dissension) in the short term but leads to arbitrary compliance or worse. ... Deciding on a level of quality isn’t like deciding on a coding standard, you can’t have an off-the-shelf-always-okay answer. Quality is the place to have nuanced discussions.
Read more [rosstuck.com]
In a new video Jason McCreary, the creator of the wonderful Laravel Shift, demonstrates a few good tips to clean up code. In the video below Jason uses a code snippet taken from my side project Oh Dear!
If you're interested in more tips from Jason be sure to check out his upcoming BaseCode field guide.
Meanwhile I've cleaned up (and deployed) the code in the actual app. This is what I ended up with:
class Check
{
public function needsToRun(): bool
{
if (!$this->belongsToTeamOnActiveSubscriptionOrOnGenericTrial()) {
return false;
}
if ($this->disabled()) {
return false;
}
if ($this->alreadyRunningOrScheduled()) {
return false;
}
if ($this->didNotRunBefore()) {
return true;
}
if ($this->checkType()->is(CheckType::UPTIME) && $this->latestRun()->failed()) {
return true;
}
if ($this->previousRunCrashed()) {
return true;
}
return $this->latestRun()->endedMoreThanMinutesAgo($this->checkType()->minutesBetweenRuns());
}
protected function checkType(): CheckType
{
return new CheckType($this->type);
}
}
use MyCLabs\Enum\Enum;
class CheckType extends Enum
{
const UPTIME = 'uptime';
const BROKEN_LINKS = 'broken_links';
const MIXED_CONTENT = 'mixed_content';
const CERTIFICATE_HEALTH = 'certificate_health';
const CERTIFICATE_TRANSPARENCY = 'certificate_transparency';
public function minutesBetweenRuns(): int
{
if ($this->getValue() === static::MIXED_CONTENT) {
return 60 * 12;
}
if ($this->getValue() === static::BROKEN_LINKS) {
return 60 * 12;
}
if ($this->getValue() === static::CERTIFICATE_HEALTH) {
return 5;
}
if ($this->getValue() === static::UPTIME) {
return 3;
}
throw new Exception("Minutes between runs not specified for type `{$this->getValue()}`");
}
public function is(string $type): bool
{
return $type === $this->getValue();
}
}