-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed on just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a convention that mutating functions should return the input, like |
||
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 | ||
``` |
There was a problem hiding this comment.
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
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
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
orreturn x
is just a style choice, but making sure to returnnothing
when you don't mean to return a value is more significant.