DEV Community

Discussion on: Mastering Conditional Logic in Laravel with `when()` and `unless()` Methods: Real-Life Examples

Collapse
 
xwero profile image
david duymelinck • Edited

Seeing the examples the when and unless methods don't blow me out of the water.
I don't understand what is wrong with using if statements? Most of the examples could be ternary statements.

$rules = [
    'email' =>  $request->has('newsletter') ?  'required|email'  : 'nullable',
];

$request->validate($rules);

// instead of
$rules = [
    'email' => 'nullable',
];

if ($request->has('newsletter')) {
    $rules['email'] = 'required|email';
}

$request->validate($rules);
Enter fullscreen mode Exit fullscreen mode

I also spotted an error in one of the examples.

$data = [
    'total_price' => $cart->totalPrice(),
];
// returns error
$data = $data->when($request->has('coupon_code'), // ...
// working
$data = collect($data)->when($request->has('coupon_code'), // ...
Enter fullscreen mode Exit fullscreen mode

I trained myself to write positive if statements, so the unless method is lost on me.
I don't see how the functions make code more readable, maintainable or elegant.
The methods make code even harder to read if you use the third parameter.

return $user->unless($user->isActive(), function () {
    return "Your account is inactive. Please contact support.";
})->otherwise(function () {
    return "Welcome back!";
});
// can be written as
return $user->unless($user->isActive(), 
 fn() => "Your account is inactive. Please contact support.",
fn() => "Welcome back!"
);
// plain php
return $user->isActive() ? "Welcome back!" :  "Your account is inactive. Please contact support.";
Enter fullscreen mode Exit fullscreen mode

Thank you for the post! I will keep writing php.

Collapse
 
asfiaaiman profile image
Asfia Aiman

Hello David,

yes you can use multiple if statements, ternary or switch cases where you want. However, this when() and unless() is another way of writing it, nothing else.

return $user->unless($user->isActive(),
fn() => "Your account is inactive. Please contact support.",
fn() => "Welcome back!"
);

is a cleaner way however, I personally believe that it is not reading friendly, so I do not prefer using it.

Collapse
 
xwero profile image
david duymelinck • Edited

I can understand the readability argument. A lot of people find a fluent API more readable.
My previous comment was more about preferences, and those are a million in a bunch.

So I want to give more rooted reasons why not to use the methods.

The when an unless methods are two function calls that have no other added value than readability. One of the low hanging fruits during code optimalisation is reducing function calls.

You also are writing more code instead of less because of the anonymous functions.

And you need to start with a class that has a Conditionable trait. I see people using this and make the context of the flow confusing because they bind it to an unrelated instance.

Thread Thread
 
asfiaaiman profile image
Asfia Aiman

Yeah thats quite true