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

**URL:** <https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224>\
**Category:** General Usage\
**Created:** [February 5, 2020, 9:32pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224 "2020-02-05T21:32:58Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [February 5, 2020, 9:32pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/1 "2020-02-05T21:32:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lbilli](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@lbilli](https://discourse.julialang.org/u/lbilli)\
**Post date:** [February 5, 2020, 9:55pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/2 "2020-02-05T21:55:03Z")

</div>

No need of parentheses in the comparison, only in the assignment:

```julia
a < 0 && (a = 0)

```

Maybe it’s not too bad.

---

<div class="post-metadata">

**Author:** ![NicholasWMRitchie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicholaswmritchie/32/22449_2.png) [@NicholasWMRitchie](https://discourse.julialang.org/u/NicholasWMRitchie)\
**Post date:** [February 5, 2020, 9:55pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/3 "2020-02-05T21:55:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 5, 2020, 10:19pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/4 "2020-02-05T22:19:28Z")

</div>

Not-entirely-serious macro-based solution:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 5, 2020, 11:00pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/5 "2020-02-05T23:00:28Z")

</div>

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

```julia
if a < 0
   a = 0
end

```

just as easy to type?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 5, 2020, 11:32pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/6 "2020-02-05T23:32:54Z")

</div>

> [@Gus\_Hart](#):
>
> What is the most Julianic way to do this simple “if-then” logic?

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

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [February 6, 2020, 4:06am UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/7 "2020-02-06T04:06:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [February 6, 2020, 7:10am UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/8 "2020-02-06T07:10:26Z")

</div>

`a = max(a,0)` is also not so brittle regarding type stability.

---

<div class="post-metadata">

**Author:** ![kdyrhage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdyrhage/32/2326_2.png) [@kdyrhage](https://discourse.julialang.org/u/kdyrhage)\
**Post date:** [February 6, 2020, 7:45am UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/9 "2020-02-06T07:45:31Z")

</div>

You can also write it on a single line if you prefer:

```julia
if a < 0 a = 0 end

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 6, 2020, 8:24am UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/10 "2020-02-06T08:24:03Z")

</div>

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](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/3), otherwise

```julia
a = default_value
if condition
    a = new_value
end

```

---

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [February 6, 2020, 3:24pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/11 "2020-02-06T15:24:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [February 6, 2020, 3:25pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/12 "2020-02-06T15:25:39Z")

</div>

Thanks, I didn’t realize that. That improves the readability a bit for me.

---

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [February 6, 2020, 3:27pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/13 "2020-02-06T15:27:27Z")

</div>

Oh, wow. I didn’t realize the semicolons were unnecessary. How did I not know that?

---

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [February 6, 2020, 3:30pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/14 "2020-02-06T15:30:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [February 6, 2020, 3:31pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/15 "2020-02-06T15:31:47Z")

</div>

I’m not sure I know what you mean by _mutations_ or why having the assignment on its own line helps. Can you elaborate?

---

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [February 6, 2020, 3:33pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/16 "2020-02-06T15:33:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 6, 2020, 6:42pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/17 "2020-02-06T18:42:29Z")

</div>

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.

> [@Gus\_Hart](#):
>
> I find it easier to read code when I don’t have to scroll around.

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.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [February 6, 2020, 7:17pm UTC](https://discourse.julialang.org/t/style-question-ternary-operator-or-short-circuit-operator-or-if-end/34224/18 "2020-02-06T19:17:54Z")

</div>

As another data point, we have [explicit guidance on this for LightGraphs contributions](https://github.com/JuliaGraphs/LightGraphs.jl/blob/master/CONTRIBUTING.md#development-guidelines):

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