https://abs.twimg.com/emoji/v2/... draggable="false" alt="✨" title="Funken" aria-label="Emoji: Funken"> In this thread I& #39;ll list tactics you can use to write cleaner code in Laravel.

As you use them repeatedly, you& #39;ll develop a sense for what& #39;s good code and what& #39;s bad code.

I& #39;ll also sprinkle some general Laravel code advice in between these tactics.

THREAD
https://abs.twimg.com/emoji/v2/... draggable="false" alt="💡" title="Elektrische Glühbirne" aria-label="Emoji: Elektrische Glühbirne"> It& #39;s about the *micro*.

Using some "macro" philosophy for structuring your code, like hexagonal architecture or DDD won& #39;t save you.

A clean codebase is the result of constant good decisions at the micro level.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="💡" title="Elektrische Glühbirne" aria-label="Emoji: Elektrische Glühbirne"> Dedicate a weekend towards learning proper OOP.

Know the difference between static/instance methods & variables and private/protected/public visibility. Also learn how Laravel uses magic methods.

You don& #39;t need this as a beginner, but as your code grows, it& #39;s crucial.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="👉" title="Rückhand Zeigefinger nach rechts" aria-label="Emoji: Rückhand Zeigefinger nach rechts"> Don& #39;t just write procedural code in classes.

This ties the previous tweet with the other tips here. OOP exists to make your code more readable, use it. Don& #39;t just write 400 line long procedural code in controller actions.

Here& #39;s code from my first Laravel project https://abs.twimg.com/emoji/v2/... draggable="false" alt="😬" title="Grimasse schneidendes Gesicht" aria-label="Emoji: Grimasse schneidendes Gesicht">
https://abs.twimg.com/emoji/v2/... draggable= Don& #39;t just write procedural code in classes.This ties the previous tweet with the other tips here. OOP exists to make your code more readable, use it. Don& #39;t just write 400 line long procedural code in controller actions.Here& #39;s code from my first Laravel project https://abs.twimg.com/emoji/v2/... draggable="false" alt="😬" title="Grimasse schneidendes Gesicht" aria-label="Emoji: Grimasse schneidendes Gesicht">" title="https://abs.twimg.com/emoji/v2/... draggable="false" alt="👉" title="Rückhand Zeigefinger nach rechts" aria-label="Emoji: Rückhand Zeigefinger nach rechts"> Don& #39;t just write procedural code in classes.This ties the previous tweet with the other tips here. OOP exists to make your code more readable, use it. Don& #39;t just write 400 line long procedural code in controller actions.Here& #39;s code from my first Laravel project https://abs.twimg.com/emoji/v2/... draggable="false" alt="😬" title="Grimasse schneidendes Gesicht" aria-label="Emoji: Grimasse schneidendes Gesicht">" class="img-responsive" style="max-width:100%;"/>
https://abs.twimg.com/emoji/v2/... draggable="false" alt="💡" title="Elektrische Glühbirne" aria-label="Emoji: Elektrische Glühbirne"> Read up on things like SRP & follow them to *reasonable* extent

Avoid having classes that deal with many unrelated things

But, for the love of god, don& #39;t create a class for every single thing. You& #39;re trying to write clean code. You& #39;re not trying to please the separation gods
https://abs.twimg.com/emoji/v2/... draggable="false" alt="💡" title="Elektrische Glühbirne" aria-label="Emoji: Elektrische Glühbirne"> When you see a function with a huge amount of parameters, it can mean:

1. The function has too many responsibilities. Separate.
2. The responsibilities are fine, but you should refactor the long signature.

Below are two tactics for the fixing second case.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="👉" title="Rückhand Zeigefinger nach rechts" aria-label="Emoji: Rückhand Zeigefinger nach rechts"> Try to only use CRUD actions

If you can, only use the 7 CRUD actions in your controllers. Often even fewer.

Don& #39;t create controllers with 20 methods.

More shorter controllers is better. https://www.youtube.com/watch?v=MF0jFKvS4SI">https://www.youtube.com/watch...
https://abs.twimg.com/emoji/v2/... draggable="false" alt="👉" title="Rückhand Zeigefinger nach rechts" aria-label="Emoji: Rückhand Zeigefinger nach rechts"> Create single-use Blade includes.

Similar to single-use traits.

This tactic is great when you have a very long template and you want to make it more manageable.

There& #39;s nothing wrong with @including headers and footers in layouts, or things like complex forms in page views.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="💡" title="Elektrische Glühbirne" aria-label="Emoji: Elektrische Glühbirne"> Decide if you like spaces around operators.

Above you can see that I use space between ! and the value I& #39;m negating. I like this, because it makes it clear that the value is being negated. I do the same around dots.

Decide if you like it. It can (imo) clean up your code.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="👉" title="Rückhand Zeigefinger nach rechts" aria-label="Emoji: Rückhand Zeigefinger nach rechts"> Have a single source of truth for validation rules.

If you validate some resource& #39;s attributes on multiple places, you definitely want to centralize these validation rules, so that you don& #39;t change them in one place but forget about the other places. https://twitter.com/LiamHammett/status/1260252814158282752">https://twitter.com/LiamHamme...
https://abs.twimg.com/emoji/v2/... draggable="false" alt="👉" title="Rückhand Zeigefinger nach rechts" aria-label="Emoji: Rückhand Zeigefinger nach rechts"> Comments usually indicate poor code design.

Before writing a comment, ask yourself if you could rename some things or create variables to improve readability. If that& #39;s not possible, write the comment in a way that both your colleagues and you will understand in 6 months.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="💡" title="Elektrische Glühbirne" aria-label="Emoji: Elektrische Glühbirne"> Use only what helps you and ignore everything else.

*Your goal to write more readable code.*

Your goal is NOT to do what someone said on the internet

These tips are just tactics that tend to help with clean code. Keep your end goal in mind and ask yourself "is this better?"
https://twitter.com/samuelstancl/status/1272912997757919233">https://twitter.com/samuelsta...
You can follow @samuelstancl.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: