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

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...

The full tutorial [11 mins, 2147 words] is only for Premium Members

Login Or Become a Premium Member for $129/year or $29/month
What else you will get:
  • 68 courses (1188 lessons, total 43 h 18 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses