2
votes

Coming to Julia from Python I am looking for something that sort of sets the standard for idiomatic Julia. In the Python world there is PEP 8 -- Style Guide for Python Code here. I am looking for the equivalent in the Julia world.

For example, the Python statement on use of return at above link is:

Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. If any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable).

The closest I have found to a PEP 8 in the Julia world is the Style Guide section of the Julio 1.0.0 documentation here. I did not see a discussion regarding the use of return there.

Question: Is there something more that someone can steer me to?

Question: Is it considered idiomatic Julian to always use the return reserved word at the end of a function?

1
I don't think that a unique style guide has emerged yet. Personally I almost always use explicit return statements (I sometimes omit it in very short methods). I find things are too implicit otherwise. My function might actually be subtlely type unstable.carstenbauer
Your best bet thusfar is to read Julia base source code. A little extreme, but they, ironically, show no consistency with return statements: lines 102 & 112 herestillearningsomething
Yup, two identical returns of nothing, 10 lines apart, and one is return nothing and the other is nothing. Thanks for the pointer. I like the comments here suggesting that an explicit return should be used most of the time.Julia Learner
I follow a simple rule: short-form one-line functions: no return. Ordinary long-form functions: always return. I find implicit returns disorienting, and always keep thinking that it must be a statement that has some invisible side-effect, before realizing, 'oh, I guess it's just a return statement'. But it is in very common use, even among core devs, unfortunately.DNF
I concur. Seems like Julia needs something like PEP 8 in Python.Julia Learner

1 Answers

4
votes

Most of the time, making the return explicit is good practice. There are occasions when a function is not intended to return a value (like print); Julia provides a way to make this explicit return nothing. With short functions that are essentially elaborated conditionals if x != 0 ... end, letting each branch of the conditional return its value without explicitly using return is done, but nothing is lost and clarity is often gained by making the return explicit.