Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[style] Maximum line length #1785

Merged
merged 8 commits into from
Apr 9, 2019
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 141 additions & 1 deletion docs/src/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,148 @@ function foo(x)
end
```

#### Line length

#### TODO: Line breaks
Line lengths are a contentious issue. Our foremost goal is to maximize code
readability. Very long line lengths can be hard to easily comprehend. However,
arbitrarily enforcing a maximum line length (like 80 characters) inevitably
leads to cases in which slightly longer lines (e.g. 81 characters) might be more
readable.

Therefore, aim to keep line lengths under 80 characters by breaking lines
for maximum readability (examples are given in the [Line breaks](@ref) section),
but don't treat this as a hard rule.

We make exceptions for

- URLs
- path names
- long string constants not containing whitespace
- lines for which breaking over multiple lines worsens readability

#### Line breaks

The "readability" of a line is subjective. In this section we give suggestions
of good and bad style of how to break a line.

When defining functions, align arguments vertically after the opening
parenthesis, or list all arguments on a new (indented) line.

Good:
```julia
# Arguments to the function are aligned vertically.
function my_very_long_function_name(with_lots_of_long_arguments_1,
and_another_long_one)
# First line of the function begins here.
end

# Arguments to the function are listed on a new line and indented.
function my_very_long_function_name(
with_lots_of_long_arguments_1, and_another_long_one)
# First line of the function begins here.
end
```

Bad:
```julia
# When defining functions, if vertical alignment is not used, then the arguments
# should not begin on the first line.
function my_very_long_function_name(with_lots_of_long_arguments_1,
and_another_long_one)
# First line of the function begins here.
end
```

Don't use vertical alignment if all of the arguments are very far to the right.

Bad:
```julia
a_very_long_variable_name = a_long_variable_name_with_arguments(first_argument,
second_argument)
```

Better:
```julia
a_very_long_variable_name = a_long_variable_name_with_arguments(
first_argument, second_argument)
```

Only use vertical alignment for function arguments. Don't use it in any other
situation.

Bad:
```julia
# Vertical alignment used in a non-function context. Also, indent is not a
# multiple of 4 spaces.
comprehension = [(first, second) for (first, second) in list_of_things
if isodd(first)]
```

Better
```julia
comprehension = [
(first, second) for (first, second) in list_of_things if isodd(first)
]
```

Don't use vertical alignment if it would be more readable to place all arguments
on a new indented line.

Bad:
```julia
con_index = MOI.add_constraint(backend(owner_model(variable)),
MOI.SingleVariable(index(variable)), set)
```

Better:
```julia
con_index = MOI.add_constraint(
backend(owner_model(variable)), MOI.SingleVariable(index(variable)), set)
```

Don't break lines at an inner-level of function nesting.

Bad:
```julia
con_index = MOI.add_constraint(
backend(owner_model(variable)), MOI.SingleVariable(
index(variable)), new_set)
```

Better:
```julia
con_index = MOI.add_constraint(
backend(owner_model(variable)),
MOI.SingleVariable(index(variable)), new_set)
```

For readability, don't split a one-line function over multiple lines.

Bad:
```julia
f(x) = 1 + x +
x^2 + x^3
```

Better:
```julia
f(x) = 1 + x + x^2 + x^3 + x^3
```

Drop trailing parentheses onto a new line if it improves readability.

Good:
```julia
con_index = MOI.add_constraint(
backend(owner_model(variable)), MOI.SingleVariable(index(variable)), set)
```

Also good:
```julia
con_index = MOI.add_constraint(
backend(owner_model(variable)), MOI.SingleVariable(index(variable)), set
)
```

### Syntax

Expand Down