Improve code readability by refactoring if blocks to dedicated classes

Original – by Freek Van der Herten – 1 minute read

If you code bases contains a long procedure with a lot of if statements to determine which case should be handle, consider extracting each case to its own dedicated class. Here's an example from the Mailcoach code base.

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

Webmentions

Fazlul Kabir liked on 1st June 2021
Ahmed Nagi retweeted on 1st June 2021
Ahmed Nagi liked on 1st June 2021
Peter Brinck 🤘 liked on 1st June 2021
José Cage liked on 1st June 2021
Marko Rsumovic liked on 1st June 2021
Daniele Esposito liked on 1st June 2021
Rich liked on 1st June 2021
Arthur Weill liked on 31st May 2021
Ahmet Mirzabeyoğlu retweeted on 31st May 2021
Spatie retweeted on 31st May 2021
Burhan liked on 31st May 2021
Ahmet Mirzabeyoğlu liked on 31st May 2021
Yannick Yayo liked on 31st May 2021
Scorp974 liked on 31st May 2021
Ali Mahdavi liked on 31st May 2021
Parthasarathi G K liked on 31st May 2021
Vargas Josue retweeted on 31st May 2021
Vargas Josue liked on 31st May 2021
Wyatt liked on 31st May 2021
Nathanael McDaniel 🖥 liked on 31st May 2021
ángel retweeted on 31st May 2021
ángel liked on 31st May 2021
Fabrizio replied on 31st May 2021
We need more sauce like this 🙌
Kamau Wanyee liked on 31st May 2021
Padam Shankhadev liked on 31st May 2021
ダビッド トレス liked on 31st May 2021
Oilmone liked on 31st May 2021
Willan Correia liked on 31st May 2021
K. liked on 31st May 2021
이현석 Hyunseok Lee liked on 31st May 2021
jagcgc373 liked on 31st May 2021
Franck Mercado liked on 31st May 2021
Podcaster Dev replied on 31st May 2021
I have long since held that the most powerful insight into OO programming is that it all basically resolves down to encapsulating if/switch logic ...
Elliot Derhay replied on 31st May 2021
Thanks for the reminder about the Factory pattern. I just remembered I have some code I can use that for. 🙂
/dev/prabakaran retweeted on 31st May 2021
Sébastien A retweeted on 31st May 2021
/dev/prabakaran liked on 31st May 2021
Mike liked on 31st May 2021
Fabrizio liked on 31st May 2021
Matthew Campbell liked on 31st May 2021
Podcaster Dev liked on 31st May 2021
Elliot Derhay liked on 31st May 2021
Mark Topper liked on 31st May 2021
Tim Nelke liked on 31st May 2021