Style question: ternary operator or short circuit operator or if-end

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 end or ;)
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 nothing.

(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.

1 Like

How about a = max(a,0) and let the compiler do it’s magic? Easy to understand and likely to inline to almost nothing.

8 Likes

Not-entirely-serious macro-based solution:

julia> macro ifthen(condition, body)
         quote
           if $(esc(condition))
              $(esc(body))
           end
         end
       end
@ifthen (macro with 1 method)

julia> a = 1
1

julia> @ifthen a > 0 a = 0
0

julia> a
0
2 Likes

I understand it isn’t on a single line but isn’t

if a < 0
   a = 0
end

just as easy to type?

2 Likes

If you insist on a single line I vote for the short-circuit without parentheses in the condition as the most idiomatic.

1 Like

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.

1 Like

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
if condition
    a = new_value
end
1 Like

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.

1 Like

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.

1 Like

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 if / 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.

5 Likes