Newlines style guide

原文:https://docs.gitlab.com/ee/development/newlines_styleguide.html

Newlines style guide

该样式指南为 Ruby 代码中的换行符推荐了最佳做法.

Rule: separate code with newlines only to group together related logic

  1. # bad
  2. def method
  3. issue = Issue.new
  4. issue.save
  5. render json: issue
  6. end
  1. # good
  2. def method
  3. issue = Issue.new
  4. issue.save
  5. render json: issue
  6. end

Rule: separate code and block with newlines

Newline before block

  1. # bad
  2. def method
  3. issue = Issue.new
  4. if issue.save
  5. render json: issue
  6. end
  7. end
  1. # good
  2. def method
  3. issue = Issue.new
  4. if issue.save
  5. render json: issue
  6. end
  7. end

Newline after block

  1. # bad
  2. def method
  3. if issue.save
  4. issue.send_email
  5. end
  6. render json: issue
  7. end
  1. # good
  2. def method
  3. if issue.save
  4. issue.send_email
  5. end
  6. render json: issue
  7. end

Exception: no need for newline when code block starts or ends right inside another code block

  1. # bad
  2. def method
  3. if issue
  4. if issue.valid?
  5. issue.save
  6. end
  7. end
  8. end
  1. # good
  2. def method
  3. if issue
  4. if issue.valid?
  5. issue.save
  6. end
  7. end
  8. end