Crystal: Do not indent "if" block when assigning it to a variable

Created on 4 Sep 2017  路  3Comments  路  Source: crystal-lang/crystal

# Current formatting
results = if conn.is_a?(DB::Database)
            conn.query(query_string, params)
          else
            conn.connection.query(query_string, params)
          end

# Better IMO
results = if conn.is_a?(DB::Database)
  conn.query(query_string, params)
else
  conn.connection.query(query_string, params)
end

Most helpful comment

Just for reference, the Ruby style guide recommends the former.

Another alternative suggested there is to move the conditional to a new line:

results =
  if conn.is_a?(DB::Database)
    conn.query(query_string, params)
  else
    conn.connection.query(query_string, params)
  end

All 3 comments

Just for reference, the Ruby style guide recommends the former.

Another alternative suggested there is to move the conditional to a new line:

results =
  if conn.is_a?(DB::Database)
    conn.query(query_string, params)
  else
    conn.connection.query(query_string, params)
  end

The cleanest way to do it is

if conn.is_a?(DB::Database)
  results = conn.query(query_string, params)
else
  results = conn.connection.query(query_string, params)
end

which is perfectly valid in crystal.

I don't think we'll be changing this behaviour in the formatter though. If you want it flat use the assignments in the if blocks.

Alright; community opinion matters! :ok:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jhass picture jhass  路  3Comments

Papierkorb picture Papierkorb  路  3Comments

RX14 picture RX14  路  3Comments

lbguilherme picture lbguilherme  路  3Comments

asterite picture asterite  路  3Comments