Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here
Premium Members Only
Join to unlock this tutorial and all of our courses.
Tutorial Premium Tutorial

"Clean Code" in Laravel: 6 Practical Tips with Examples

August 19, 2024
11 min read

Clean code is something all devs aim for, right? But what does it ACTUALLY mean? In this tutorial, I will list 6 practical tips for Laravel to write clean code.

The general goal here is to commit code that is easy for future developers (including ourselves) to understand and maintain.

So, what are the main principles, applied to Laravel projects?


Tip 1. Naming Things: Meaning and Conventions

There's an old saying by Phil Karlton:

"There are only two hard things in Computer Science: cache invalidation and naming things."

So yeah, often, we find it hard to understand someone's code because of poorly worded variables, functions, classes, and methods.

It costs time and effort to understand what the code actually does.

Let's take a look at an example code:

public function create()
{
$aid = request('aid', setting('default.account'));
$std = request('std', Date::now()->firstOfMonth()->toDateString());
$etd = request('etd', Date::now()->endOfMonth()->toDateString());
 
$acc = Account::find($acc_id);
 
$c = $acc->currency;
 
$tl = $this->getTransactions($acc, $std, $etd);
 
$ob = $this->getOpeningBalance($acc, $std);
 
return view('banking.reconciliations.create', compact('acc', 'c', 'ob', 'tl'));
}

What do you think? Can you quickly understand what the variables are? Or think about the View file:

{{ $c->symbol }} {{ $ob }}

What is $c? What is $ob? It's not clear, right?

And now, let's look at appropriately named variables:

public function create()
{
$accountId = request('account_id', setting('default.account'));
$startedAt = request('started_at', Date::now()->firstOfMonth()->toDateString());
$endedAt = request('ended_at', Date::now()->endOfMonth()->toDateString());
 
$account = Account::find($accountId);
 
$currency = $account->currency;
 
$transactions = $this->getTransactions($account, $startedAt, $endedAt);
 
$openingBalance = $this->getOpeningBalance($account, $startedAt);
 
return view('banking.reconciliations.create', compact('account', 'currency', 'openingBalance', 'transactions'));
}

And the View file:

{{ $currency->symbol }} {{ $openingBalance }}

Now, it's so much easier to understand what the code does and the purpose of each variable.

This also applies to functions and classes. Use meaningful names that describe what the function does or what the class is responsible for.

Don't be afraid to use long names, as long as they are descriptive. It's better to have a long descriptive name than a short, cryptic one. Characters are free, but brain capacity is not. For example:

public function getTransactionsForUserWithCurrency(Account $account, string $started_at, string $ended_at)
{
// code here
}

Rather than:

public function transactions($a, $std, $etd)
{
// code here
}

When named clearly, you/anyone can easily understand what the function does without looking at its implementation or documentation.

Of course, you should also follow conventions for naming. They help people understand your code faster and avoid confusion while working in a team. Some of the resources you can use:

There are many different styles per language/framework available for you to choose from. The most important thing is to be consistent with your naming conventions.


Tip 2. Single Responsibility principle: Write Code that Does ONE Thing Only

If a class/method is...

Premium Members Only

This advanced tutorial is available exclusively to Laravel Daily Premium members.

Premium membership includes:

Access to all premium tutorials
Video and Text Courses
Private Discord Channel

Comments & Discussion

LN
Loganathan Natarajan ✓ Link copied!

Thank you for sharing. Very clear explanation.

FB
Florian Bouffard ✓ Link copied!

Thanks for theses tips :)

E
Emmanuel71 ✓ Link copied!

Very useful tips (and reminders :-) )

BM
Briere Mostafa Amine ✓ Link copied!

Nice, Thanks

K
Kelvin ✓ Link copied!

Very practical. Thanks. It would be nice if there is an article or video to show the common mistakes related to the final tip (Use Laravel). As someone who came from Java, I feel like Laravel have many tricks on its sleeve that really makes our job easier.

D

Thanks for sharing

MA
Majd Aleid ✓ Link copied!

love this article

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.