2020-10-30 14:08:56 -04:00
---
stage: none
group: unassigned
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-10-30 14:08:56 -04:00
---
2020-05-19 23:08:04 -04:00
# Newlines style guide
2016-08-03 05:26:24 -04:00
This style guide recommends best practices for newlines in Ruby code.
2016-08-26 02:12:30 -04:00
## Rule: separate code with newlines only to group together related logic
2016-08-03 05:26:24 -04:00
```ruby
# bad
def method
issue = Issue.new
issue.save
2018-09-06 12:52:18 -04:00
2019-07-08 23:28:41 -04:00
render json: issue
2016-08-03 05:26:24 -04:00
end
```
```ruby
# good
def method
issue = Issue.new
issue.save
2018-09-06 12:52:18 -04:00
2019-07-08 23:28:41 -04:00
render json: issue
2016-08-03 05:26:24 -04:00
end
```
## Rule: separate code and block with newlines
### Newline before block
```ruby
# bad
def method
issue = Issue.new
if issue.save
render json: issue
end
end
```
```ruby
# good
def method
issue = Issue.new
if issue.save
render json: issue
end
end
```
## Newline after block
```ruby
# bad
def method
if issue.save
issue.send_email
end
render json: issue
end
```
```ruby
# 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
```ruby
# bad
def method
if issue
if issue.valid?
issue.save
end
end
end
```
```ruby
# good
def method
if issue
if issue.valid?
issue.save
end
end
end
```