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

suggest using explicit return in style guide #29727

Closed
wants to merge 3 commits into from
Closed
Changes from 2 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
51 changes: 51 additions & 0 deletions doc/src/manual/style-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,54 @@ julia> h(1)

Thus, use `Int` literals when possible, with `Rational{Int}` for literal non-integer numbers,
in order to make it easier to use your code.

## In long-form function definitions, prefer explicit `return` over implicit return values

Explicit `return` on an expression makes it clear that the expression is intended to be returned and
is not only used for its side effect. It also makes it possible to locally see that a certain sub-expression
in a larger expression is returned while for an implicit return value one needs to analyze the whole outer expression.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also mention that it can help the program to run faster if it was returning an unintentional value (when it only needs to return nothing)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very good point but I think it is quite separate --- whether to end a function with x or return x is just a style choice, but making sure to return nothing when you don't mean to return a value is more significant.

For example, prefer

```jl
function f(x)
if x > 2
return "a"
else
return "b"
end
end

function g(x)
y = compute(x)
return compute(y)
end

function h!(x)
fill!(x, compute(x))
modify!(x)
return nothing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just return or nothing? explicitly stating the default is a bit excessive

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on just return.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a convention that mutating functions should return the input, like push!?

end
```

over

```jl
function f(x)
if x > 2
"a"
else
"b"
end
end

function g(x)
y = compute(x)
compute(y)
end

function h!(x)
fill!(x, compute(x))
modify!(x)
end
```