diff --git a/doc/src/manual/style-guide.md b/doc/src/manual/style-guide.md index a39697d124456..deaab0edccd50 100644 --- a/doc/src/manual/style-guide.md +++ b/doc/src/manual/style-guide.md @@ -413,3 +413,56 @@ 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. +In cases where the return value of the function is irrelevant, use a `return` statement with no value. +This can help the compiler since it doesn't have to reason about the type of the return value. + +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 +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 +```