# How to do one-liners and other short conditionals?

**URL:** <https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100>\
**Category:** General Usage\
**Created:** [September 27, 2017, 1:21am UTC](https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100 "2017-09-27T01:21:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [September 27, 2017, 1:21am UTC](https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100/1 "2017-09-27T01:21:35Z")

</div>

Sometimes I want to write a quick and dirty conditional, i.e.

```julia
munge_data!(x,y) if isempty(x)

```

But instead you have to write:

```julia
if isempty(x) munge_data!(x,y) end

```

// which I don’t think is as readable.

* * *

How do other people approach this?

Would it be nearly impossible to add this to the language?

---

<div class="post-metadata">

**Author:** ![JackDevine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackdevine/32/1048_2.png) [@JackDevine](https://discourse.julialang.org/u/JackDevine)\
**Post date:** [September 27, 2017, 1:36am UTC](https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100/2 "2017-09-27T01:36:01Z")

</div>

You could use short circuiting,

```julia
isempty(x) && munge_data!(x, y)

```

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [September 27, 2017, 3:05am UTC](https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100/3 "2017-09-27T03:05:44Z")

</div>

Yeah, using `&&` for that is a little reminiscent of C but is a pretty common idiom in julia code and very compact. You can also use `||` to get “unless” behavior, i.e.:

```julia
isvalid(arg) || throw(ArgumentError("Bad argument!"))

```

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [September 27, 2017, 3:36am UTC](https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100/4 "2017-09-27T03:36:06Z")

</div>

what about this use case:

```julia
julia> isapprox(0, tau_exponent) && tau_exponent = 1

ERROR: syntax: invalid assignment location "isapprox(0,tau_exponent)&&tau_exponent"

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [September 27, 2017, 3:40am UTC](https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100/5 "2017-09-27T03:40:42Z")

</div>

> [@djsegal](#):
>
> isapprox(0, tau\_exponent) && tau\_exponent = 1

```julia
isapprox(0, tau_exponent) && (tau_exponent = 1)

```

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [September 27, 2017, 5:32am UTC](https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100/6 "2017-09-27T05:32:51Z")

</div>

Thanks all 🙏

One last question:

> Is this done widely enough to be considered idiomatic?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [September 27, 2017, 5:46am UTC](https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100/7 "2017-09-27T05:46:46Z")

</div>

It is used often. Consider it a tool.

---

<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:** [September 27, 2017, 3:58pm UTC](https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100/8 "2017-09-27T15:58:15Z")

</div>

We have an explicit design rule for this in LightGraphs which says that shortcuts are preferred if the function is non-mutating. Therefore,

```julia
foo && return bar

```

is ok, but

```julia
foo && (baz += 1)

```

is not.

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [September 27, 2017, 6:51pm UTC](https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100/9 "2017-09-27T18:51:10Z")

</div>

I agree with this sentiment, but feel like it uncovers the problem.

Shouldn’t there be a quick way to write:

```julia
baz += 1 if foo

```

?

// I think the reason you’re against it is because it’s overly implicit

---

<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:** [September 27, 2017, 6:57pm UTC](https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100/10 "2017-09-27T18:57:09Z")

</div>

Nothing’s stopping you from writing

```julia
foo && (baz += 1)

```

… it’s just that we discourage that practice in the package I help maintain. It’s a style thing, not a language limitation.

---

<div class="post-metadata">

**Author:** ![NickNack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicknack/32/1792_2.png) [@NickNack](https://discourse.julialang.org/u/NickNack)\
**Post date:** [September 27, 2017, 8:28pm UTC](https://discourse.julialang.org/t/how-to-do-one-liners-and-other-short-conditionals/6100/11 "2017-09-27T20:28:47Z")

</div>

Allow me to short-circuit the discussion and point to the relevant issues: [#6823](https://github.com/JuliaLang/julia/issues/6823) and [#16389](https://github.com/JuliaLang/julia/issues/16389). Happy reading!
