Whitespace

Indentation

  • Use soft-tabs with a
    two-space indent.[link]

  • Indent when as deep as case.
    [link]

    1. case
    2. when song.name == 'Misty'
    3. puts 'Not again!'
    4. when song.duration > 120
    5. puts 'Too long!'
    6. when Time.now.hour > 21
    7. puts "It's too late"
    8. else
    9. song.play
    10. end
    11. kind = case year
    12. when 1850..1889 then 'Blues'
    13. when 1890..1909 then 'Ragtime'
    14. when 1910..1929 then 'New Orleans Jazz'
    15. when 1930..1939 then 'Swing'
    16. when 1940..1950 then 'Bebop'
    17. else 'Jazz'
    18. end
  • Align function parameters either all on
    the same line or one per line.[link]

    1. # bad
    2. def self.create_translation(phrase_id, phrase_key, target_locale,
    3. value, user_id, do_xss_check, allow_verification)
    4. ...
    5. end
    6. # good
    7. def self.create_translation(phrase_id,
    8. phrase_key,
    9. target_locale,
    10. value,
    11. user_id,
    12. do_xss_check,
    13. allow_verification)
    14. ...
    15. end
    16. # good
    17. def self.create_translation(
    18. phrase_id,
    19. phrase_key,
    20. target_locale,
    21. value,
    22. user_id,
    23. do_xss_check,
    24. allow_verification
    25. )
    26. ...
    27. end
  • Indent succeeding lines in multi-line
    boolean expressions.[link]

    1. # bad
    2. def is_eligible?(user)
    3. Trebuchet.current.launch?(ProgramEligibilityHelper::PROGRAM_TREBUCHET_FLAG) &&
    4. is_in_program?(user) &&
    5. program_not_expired
    6. end
    7. # good
    8. def is_eligible?(user)
    9. Trebuchet.current.launch?(ProgramEligibilityHelper::PROGRAM_TREBUCHET_FLAG) &&
    10. is_in_program?(user) &&
    11. program_not_expired
    12. end

Inline

  • Never leave trailing whitespace.
    [link]

  • When making inline comments, include a
    space between the end of the code and the start of your comment.
    [link]

    1. # bad
    2. result = func(a, b)# we might want to change b to c
    3. # good
    4. result = func(a, b) # we might want to change b to c
  • Use spaces around operators; after commas,
    colons, and semicolons; and around { and before }.
    [link]

    1. sum = 1 + 2
    2. a, b = 1, 2
    3. 1 > 2 ? true : false; puts 'Hi'
    4. [1, 2, 3].each { |e| puts e }
  • Never include a space before a comma.
    [link]

    1. result = func(a, b)
  • Do not include space inside block
    parameter pipes. Include one space between parameters in a block.
    Include one space outside block parameter pipes.
    [link]

    1. # bad
    2. {}.each { | x, y |puts x }
    3. # good
    4. {}.each { |x, y| puts x }
  • Do not leave space between ! and its
    argument.[link]

    1. !something
  • No spaces after (, [ or before ], ).
    [link]

    1. some(arg).other
    2. [1, 2, 3].length
  • Omit whitespace when doing
    string interpolation.[link]

    1. # bad
    2. var = "This #{ foobar } is interpolated."
    3. # good
    4. var = "This #{foobar} is interpolated."
  • Don’t use extra whitespace in range
    literals.[link]

    1. # bad
    2. (0 ... coll).each do |item|
    3. # good
    4. (0...coll).each do |item|

Newlines

  • Add a new line after if conditions spanning
    multiple lines to help differentiate between the conditions and the body.
    [link]

    1. if @reservation_alteration.checkin == @reservation.start_date &&
    2. @reservation_alteration.checkout == (@reservation.start_date + @reservation.nights)
    3. redirect_to_alteration @reservation_alteration
    4. end
  • Add a new line after conditionals,
    blocks, case statements, etc.[link]

    1. if robot.is_awesome?
    2. send_robot_present
    3. end
    4. robot.add_trait(:human_like_intelligence)
  • Don’t include newlines between areas
    of different indentation (such as around class or module bodies).
    [link]

    1. # bad
    2. class Foo
    3. def bar
    4. # body omitted
    5. end
    6. end
    7. # good
    8. class Foo
    9. def bar
    10. # body omitted
    11. end
    12. end
  • Include one, but no more than one, new
    line between methods.[link]

    1. def a
    2. end
    3. def b
    4. end
  • Use a single empty line to break between
    statements to break up methods into logical paragraphs internally.
    [link]

    1. def transformorize_car
    2. car = manufacture(options)
    3. t = transformer(robot, disguise)
    4. car.after_market_mod!
    5. t.transform(car)
    6. car.assign_cool_name!
    7. fleet.add(car)
    8. car
    9. end
  • End each file with a newline. Don’t include
    multiple newlines at the end of a file.
    [link]