Skip to content

Indent when as deep as case #2

@unixmonkey

Description

@unixmonkey

https://github.com/getfretless/ruby#indentation

Here's one I slightly disagree with:

I personally would like to see:

kind = case year
  when 1850..1889 then 'Blues'
  when 1890..1909 then 'Ragtime'
  when 1910..1929 then 'New Orleans Jazz'
  when 1930..1939 then 'Swing'
  when 1940..1950 then 'Bebop'
  else 'Jazz'
end

with the when's and else indented 2-spaces over either of these (the AirBnB recommendations):

case
when song.name == 'Misty'
  puts 'Not again!'
when song.duration > 120
  puts 'Too long!'
when Time.now.hour > 21
  puts "It's too late"
else
  song.play
end

kind = case year
       when 1850..1889 then 'Blues'
       when 1890..1909 then 'Ragtime'
       when 1910..1929 then 'New Orleans Jazz'
       when 1930..1939 then 'Swing'
       when 1940..1950 then 'Bebop'
       else 'Jazz'
       end

I think I understand why AirBnB and others don't like this, though.
Case doesn't take a block, and if you indent your whens and else, the else doesn't line up with end.

kind = case year
  when 1850..1889
    'Blues'
  when 1890..1909
    'Ragtime'
  else
    'Jazz'
end

But I would even prefer that.

Or even this:

case something
  when 1850..1889 then 'Blues'
  when 1890..1909 then 'Ragtime'
else
  'Jazz'
end

I think of the indentation as being the "method body", as that's where the meat is, and it is more like an other control structures (like if), so it makes sense to me, and is more aesthetically pleasing to have it indented (IMO).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions