I often find myself wishing the following syntax worked:
if a < 0 then a = 0 (get the condition, and statement that follows, on a single line. No need for
This works fine:
if a < 0; a = 0; end, but feels just a tad cumbersome.
a < 0 ? a = 0 : nothing works as well, but I don’t love the
(a < 0) && (a = 0) also works but the need for the parentheses is disappointing.
What is the most Julianic way to do this simple “if-then” logic?
No need of parentheses in the comparison, only in the assignment:
a < 0 && (a = 0)
Maybe it’s not too bad.
a = max(a,0) and let the compiler do it’s magic? Easy to understand and likely to inline to almost nothing.
Not-entirely-serious macro-based solution:
julia> macro ifthen(condition, body)
@ifthen (macro with 1 method)
julia> a = 1
julia> @ifthen a > 0 a = 0
I understand it isn’t on a single line but isn’t
if a < 0
a = 0
just as easy to type?
If you insist on a single line I vote for the short-circuit without parentheses in the condition as the most idiomatic.
In my experience all other versions of
if were slower then
a = a < 0 ? 0 : a, I guess because it converts to a single operation. But may be my benchmarking was off.
a = max(a,0) is also not so brittle regarding type stability.
You can also write it on a single line if you prefer:
if a < 0 a = 0 end
This may not answer your original question, but many Julia coders try to avoid assignment in one-liners on purpose.
The idea behind this is that mutations are frequently the source of bugs, and as such should stand out.
When feasible, I would go for the functional solution suggested above, otherwise
a = default_value
a = new_value
The particular condition and statement were just hypothetical. Usually my conditions and the statement are more complicated. The essence of the question was the style for a single-line if.
Thanks, I didn’t realize that. That improves the readability a bit for me.
Oh, wow. I didn’t realize the semicolons were unnecessary. How did I not know that?
I wasn’t trying to save typing. I want to save vertical space.
I find it easier to read code when I don’t have to scroll around. When the if-then is fairly simple, it’s not hard to grok it when it’s on one line. So I was looking to squeeze it onto a single line without abusing && (if that is a thing) and using a syntax that was aesthetically sound.
I’m not sure I know what you mean by mutations or why having the assignment on its own line helps. Can you elaborate?
I like this solution. Thanks for taking the time to post.
I haven’t been worried to much about a performant solution yet. Just aiming for correctness and maximum clarity.
I don’t think I can expand much on what I wrote above. You are changing the value of a variable, so it should stand out.
Note that idiomatic Julia code often has shorter functions than other languages used for a similar purpose (eg R, and particularly Matlab). Functions longer than, say, 30 LOC are quite rare, or should be.
As another data point, we have explicit guidance on this for LightGraphs contributions:
- Prefer the short circuiting conditional over
else when convenient, and where state is not explicitly being mutated ( e.g. ,
condition && error("message") is good;
condition && i += 1 is not).
It’s a hard and fast rule for LightGraphs but not universal across the Julia ecosystem as far as I’ve seen.