# Newlines style guide

> 原文：[https://docs.gitlab.com/ee/development/newlines_styleguide.html](https://docs.gitlab.com/ee/development/newlines_styleguide.html)

*   [Rule: separate code with newlines only to group together related logic](#rule-separate-code-with-newlines-only-to-group-together-related-logic)
*   [Rule: separate code and block with newlines](#rule-separate-code-and-block-with-newlines)
    *   [Newline before block](#newline-before-block)
*   [Newline after block](#newline-after-block)
    *   [Exception: no need for newline when code block starts or ends right inside another code block](#exception-no-need-for-newline-when-code-block-starts-or-ends-right-inside-another-code-block)

# Newlines style guide[](#newlines-style-guide "Permalink")

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

## Rule: separate code with newlines only to group together related logic[](#rule-separate-code-with-newlines-only-to-group-together-related-logic "Permalink")

```
# bad
def method
  issue = Issue.new

  issue.save

  render json: issue
end 
```

```
# good
def method
  issue = Issue.new
  issue.save

  render json: issue
end 
```

## Rule: separate code and block with newlines[](#rule-separate-code-and-block-with-newlines "Permalink")

### Newline before block[](#newline-before-block "Permalink")

```
# bad
def method
  issue = Issue.new
  if issue.save
    render json: issue
  end
end 
```

```
# good
def method
  issue = Issue.new

  if issue.save
    render json: issue
  end
end 
```

## Newline after block[](#newline-after-block "Permalink")

```
# bad
def method
  if issue.save
    issue.send_email
  end
  render json: issue
end 
```

```
# good
def method
  if issue.save
    issue.send_email
  end

  render json: issue
end 
```

### Exception: no need for newline when code block starts or ends right inside another code block[](#exception-no-need-for-newline-when-code-block-starts-or-ends-right-inside-another-code-block "Permalink")

```
# bad
def method

  if issue

    if issue.valid?
      issue.save
    end

  end

end 
```

```
# good
def method
  if issue
    if issue.valid?
      issue.save
    end
  end
end 
```