学んだこと
if (! $has_no_ )って否定の否定のコードは、正しく解釈するのが難しくなる
否定の否定文
$has_no_items = ..... // ない場合 if ($has_no_items) { .... } // ある場合 if (!$has_no_items) { .... }
言うまでもなく見づらい
変数名を変える
$has_items = ..... // ない場合 if (!$has_items) { .... } // ある場合 if (!$has_items) { .... }
これで少し見やすくなる
変数に入れ直す
$has_items = ..... $has_no_items = !$has_items; // ない場合 if ($has_no_items) { .... } // ある場合 if ($has_items) { .... }
ただ、こちらは一度処理を先にする。
直感で$has_itemsの変数との関係が見づらくなる可能性もあるので、要検討。
$has_no_itemsの算出方法が変わると、バグの危険もあるので、扱い注意。