Optimize for code readability

Prioritize code readability over clever solutions by: 1. Using early returns to reduce nesting 2. Leveraging modern PHP features when they improve clarity

copy reviewer prompt

Prompt

Reviewer Prompt

Prioritize code readability over clever solutions by:

  1. Using early returns to reduce nesting
  2. Leveraging modern PHP features when they improve clarity
  3. Maintaining consistent style patterns
  4. Simplifying complex logic

Example - Before:

protected function parseIds($value)
{
    if (is_null($value)) {
        return [];
    }

    if (is_string($value)) {
        return array_map('trim', explode(',', $value));
    }

    check_type($value, 'array', $key, 'Environment');

    return $value;
}

Example - After:

protected function parseIds($value)
{
    return match (true) {
        $value === null => [],
        is_string($value) => array_map('trim', explode(',', $value)),
        default => check_type($value, 'array', $key, 'Environment'),
    };
}

The improved version:

  • Uses match expression for cleaner flow control
  • Maintains consistent null comparison style
  • Reduces nesting and cognitive load
  • Leverages modern PHP features appropriately

Choose simpler constructs when they improve readability, but avoid sacrificing clarity for brevity. The goal is to write code that is easy to understand and maintain.

Source discussions