16
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel Tips #1 もし何かの逆だったら…

Posted at

下記のコードを見てください。

public function show(Product $product)
{
    if(!$product->isVisible()) {
        abort(404);
    }
}

もし(商品が表示状態)の逆だったら、404になれと読みますね。
これは複雑じゃないけど、インデントを外したい場合はヘルパーファンクションの abort_if で更に読みやすくなりますね!

public function show(Product $product)
{
    abort_if(!$product->isVisible(), 404);
}

まだもし(商品が表示状態)の逆だったら、404になれと読みますが、コードの1行で。
「何かの逆」でチェックするのは考えづらいので、 abort_unless でやってみると…

public function show(Product $product)
{
    abort_unless($product->isVisible(), 404);
}

商品が表示状態でなければ、404になれとなりますね。

同じくBlade Templateで:

@if (!$something) 
より 
@unless ($something)

ちょっとだけの差なんですが、こうした方が英語の文章っぽく読みやすくて、理解しやすいと思います。
みんなさんはどうでしょうか?

16
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?