1fc0d4f4b5
To clarify what's meant by "from a logical perspective" here, I consulted Python's PEP8 style guide, which provides some helpfully precise language: > Extra blank lines may be used (sparingly) to separate groups of > related functions. Blank lines may be omitted between a bunch of > related one-liners (e.g. a set of dummy implementations). https://www.python.org/dev/peps/pep-0008/#blank-lines I adapted this passage to the existing language for the newline rule.
102 lines
1.1 KiB
Markdown
102 lines
1.1 KiB
Markdown
# Newlines styleguide
|
|
|
|
This style guide recommends best practices for newlines in Ruby code.
|
|
|
|
## Rule: separate code with newlines only to group together related logic
|
|
|
|
```ruby
|
|
# bad
|
|
def method
|
|
issue = Issue.new
|
|
|
|
issue.save
|
|
|
|
render json: issue
|
|
end
|
|
```
|
|
|
|
```ruby
|
|
# good
|
|
def method
|
|
issue = Issue.new
|
|
issue.save
|
|
|
|
render json: issue
|
|
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
|
|
```
|