Oh Dear! monitors your entire website, not just the homepage. You'll get a notification as soon as your website is down, a monthly uptime report, a warning a few days before your SSL certificate expires and much more! Start your free 10 day trial now!

How to refactor complex if statements

Original – by Freek Van der Herten – 1 minute read

Have you every come across confusing if statements with complex conditionals such as this? I bet you did!

// please kill me 🤯 
if (!(($this->shipping_country == "GB" || (strcmp($this-status, "Valid") !== 0)) {

To me, this is completely unreadable.

In the video below I show how I deal with this situation. Spoiler: add some tests around it and break the conditionals apart.

This video is part of the Mailcoach video course. It contains many more videos on how to write clear code. You can use this coupon code to get a nice discount of $10.

YES-I-WANT-TO-WRITE-READABLE-CODE

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

You can comment on this post by replying to this tweet.
Bishir Abubakr Rungumi liked on 21st February 2020
Sam Serrien liked on 20th February 2020
Jens liked on 20th February 2020
Luis Cortés liked on 20th February 2020
Arpad Olasz liked on 20th February 2020
mehedi.jsr liked on 20th February 2020
Adrian Nürnberger 🐙 liked on 20th February 2020
Konafets liked on 20th February 2020
Gaurav Makhecha replied on 20th February 2020
1/ @freekmurze shares a video where he explains how to refactor complex if statements and making your code more readable as well as maintainable 😎 twitter.com/freekmurze/sta…
Thant Thu Hein liked on 20th February 2020
Jan-Joost Heurter liked on 20th February 2020
Mickael Isaert liked on 19th February 2020
Roberto Gallea replied on 19th February 2020
if (!$this->isNotAlmostTrue(!$uncertain)) { ... }
Thijs Van de Velde liked on 19th February 2020
Harish Patel liked on 19th February 2020
Pankaj agrawal liked on 19th February 2020
Ilya G liked on 19th February 2020
Didik Tri Susanto liked on 19th February 2020
Paul Kamau liked on 19th February 2020
«mauro» liked on 19th February 2020
zoltan liked on 19th February 2020
Coderatio liked on 19th February 2020
Salih Borucu liked on 19th February 2020
Owen Andrews liked on 19th February 2020
Francisco Barreto liked on 19th February 2020
Motorhome Fulltimer liked on 19th February 2020
Ahmet Mirzabeyoğlu liked on 19th February 2020
Daniel Pereyra Costas liked on 19th February 2020
Lars Moelleken liked on 19th February 2020
Richard liked on 19th February 2020
Matthew Poulter liked on 19th February 2020
Joiner Leal liked on 19th February 2020
Mehmed H liked on 19th February 2020
Isah Labbo liked on 19th February 2020
Daryl Geyer II liked on 19th February 2020
Levis Orgil liked on 19th February 2020
Neil Carlo Faisan Sucuangco liked on 19th February 2020
Rohit P. Shirke liked on 19th February 2020
ankush malhotra liked on 19th February 2020
Malinda Weerasinghe 🇱🇰 liked on 19th February 2020
Mohammed Alama liked on 19th February 2020
Sujal Patel liked on 19th February 2020
Tom Philpotts liked on 19th February 2020
Brian Hilson liked on 19th February 2020
O'cakes liked on 19th February 2020
Agasi Gilang Persada liked on 19th February 2020
.: PoOYaN :. liked on 19th February 2020
Gene Ronquillo liked on 19th February 2020
Jordan Hall liked on 19th February 2020
Mohamed Sabil liked on 19th February 2020
Stijn Vanouplines liked on 19th February 2020
Paul Edenburg liked on 18th February 2020
Ralph Morris liked on 18th February 2020
DirtyToothBrush liked on 18th February 2020
maheralmatri liked on 18th February 2020
Giobanny Santamaria liked on 18th February 2020
Pere Casas liked on 18th February 2020
Stefan Tanevski liked on 18th February 2020
Nishanth KD liked on 18th February 2020
Jarno Lasseel liked on 18th February 2020
Angelo liked on 18th February 2020
Steve Bauman liked on 18th February 2020
Mark Debeljak liked on 18th February 2020
Scorp974 liked on 18th February 2020
M00dle liked on 18th February 2020
Jake Casto liked on 18th February 2020
Edify Real Life Integrity liked on 18th February 2020
🍵 Patrick Artounian 🎉 liked on 18th February 2020
Ξlliott Lawson liked on 18th February 2020
Rando Hinn liked on 18th February 2020
Niels liked on 18th February 2020
That's insane. 🤯
Miguel Piedrafita 🚀 liked on 18th February 2020
Alejandro Vásquez N. liked on 18th February 2020
Odinn Adalsteinsson liked on 18th February 2020
Tom Witkowski liked on 18th February 2020
Salman Zafar liked on 18th February 2020
Tiago Silva Pereira liked on 18th February 2020
Larapy retweeted on 18th February 2020
Ishan Vyas retweeted on 18th February 2020
ダビッド トレス retweeted on 18th February 2020
Ishan Vyas liked on 18th February 2020
ダビッド トレス liked on 18th February 2020
Pavlo Bezdverniy liked on 18th February 2020
Wouter Standaert liked on 18th February 2020
Peter Sowah liked on 18th February 2020
StoicDojo liked on 18th February 2020
Samson Iyanu liked on 18th February 2020
Azamat X replied on 18th February 2020
This one is much pretty than those I've seen.
Devin Gray retweeted on 18th February 2020
Romain Norberg liked on 18th February 2020
Ben Holmen liked on 18th February 2020
zorigitano liked on 18th February 2020
rgj liked on 18th February 2020
Edwin I Arellano liked on 18th February 2020
Glen liked on 18th February 2020
Scott Zirkel liked on 18th February 2020
Benjamin Crozat liked on 18th February 2020
Kashyap Merai liked on 18th February 2020
Dainius liked on 18th February 2020
Sylvain Fertons replied on 18th February 2020
😍 I generally prefer to use "private" methods, and then switch to "protected" if needed. But 99% of my favorites developers use "protected" most of the time. Am I missing something?
abhishek retweeted on 18th February 2020
JW van Schie retweeted on 18th February 2020
mike ritter retweeted on 18th February 2020
Andre Sayej liked on 18th February 2020
Sylvain Fertons liked on 18th February 2020
Itai MC Nathaniel 🥯 liked on 18th February 2020
Muhammad khalid liked on 18th February 2020
aphrxia liked on 18th February 2020
Njogu Amos liked on 18th February 2020
traviselkins liked on 18th February 2020
ARSLAN BUTT ♛ liked on 18th February 2020
liked on 18th February 2020
mike ritter liked on 18th February 2020
Stephan Deglow liked on 18th February 2020
Cas liked on 18th February 2020
Daniel Werner liked on 18th February 2020
Lennert Siemons liked on 18th February 2020
Musa  liked on 18th February 2020
Chin Leung liked on 18th February 2020
Ruslan liked on 18th February 2020
Owen Voke (pxgamer) liked on 18th February 2020
LaravelLive Delhi liked on 18th February 2020
Hardik Shah liked on 18th February 2020
Pierre-Luc Brunet replied on 18th February 2020
Also, while I have your attention. I’ve seen most of your tweets for the last couple of weeks. I’ve come to the conclusion that you are either a set of identical triplets or a robot. Nobody does that much in 24 hours. Which is it?
Mattias Geniar replied on 18th February 2020
It's good to learn from those videos too, quality material! 👌
Freek Van der Herten replied on 18th February 2020
Yeah, me too!
Freek Van der Herten replied on 18th February 2020
Yeah, indeed 🙂
Freek Van der Herten replied on 18th February 2020
Watch those videos closely, Mattias :-) Nah, you’re already doing lots of good stuff, specially adding tests with each PR 👍
Freek Van der Herten replied on 18th February 2020
I think in the client projects where I work in it doesn’t make a lot of difference. In open source it’s nice for package users to have an escape hatch and not to hammer everything down with private (and final).
Pierre-Luc Brunet replied on 18th February 2020
This if statement make me a little sick.
Frank de Jonge replied on 18th February 2020
total syntax error too! 😱
Mattias Geniar replied on 18th February 2020
I feel caught 😅