Search
⌘K
    to navigateEnterto select Escto close

    Prefer no trailing if for multiline code

    We prefer 'if' conditional block over a trailing 'if' for multiline code.

    For code with single line adding if condition at the end is definitely preferred but for multiline code it makes it more readable and the developer does not have to read the whole block of code only to realize that its conditional.

    Example 1

    Condition on multiline Code:

    1#bad
    2def update
    3  return render json: {errors: "You can't to edit this directory"},
    4                status: :forbidden if @directory.master?
    5end
    6
    7#preferred
    8def update
    9  if @directory.master?
    10    return render json: {errors: "You can't to edit this directory"},
    11                  status: :forbidden
    12  end
    13end

    Example 2

    Condition on block of code:

    1#bad
    210.times do
    3  # multi-line body omitted
    4end if some_condition
    5
    6#preferred
    7if some_condition
    8  10.times do
    9    # multi-line body omitted
    10  end
    11end
    Previous
    Next