# Proposal: \`then\`, \`else\` syntax to replace \`&&\`, \`||\` short-circuiting

**URL:** <https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323>\
**Category:** Internals & Design\
**Created:** [January 6, 2017, 1:33pm UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323 "2017-01-06T13:33:30Z")\
**Posts on this page:** 15\
**Page:** 3

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [January 8, 2017, 12:18am UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/41 "2017-01-08T00:18:11Z")

</div>

I don’t think the syntax `x ? y:z` is a serious problem; it currently parses as a ternary operator with no range, and we could just keep that. For cases like this you already need parentheses, e.g. ` x ? (y:z) : (a:b)`. Furthermore, writing `x ? (y:z)` would be very unusual since it returns a range or `nothing`, which is not very useful.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [January 8, 2017, 8:52am UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/42 "2017-01-08T08:52:31Z")

</div>

My answer is (also) kind of off-topic (for main thread), to this:

> [@pitao](#):
>
> > if you replaced their silly QWERTY with superior DVORAK without their permission. Doing this in a company would get you fired.
> 
> Conversely if the entire company acts as a unit, gets their heads together, says ‘hey, if we collectively take a hit for a couple of months and put ourselves through some degree of frustration, we come out 20% more productive per year if we make the switch

I’m not sure you gain any keyboard speed.\*\*

Taking this not literally about keyboards, making any change, say changing from you chosen language (C++?) to Julia, could have that improvement (maybe not?), or more.

Joel on software famously wrote about rewrite software is a mistake. Does that mean, Julia is only for startups? I think not, you need not rewrite, you can migrate slowly. Even from PHP, with Polyglot.jl… At least for C, C++, Python and more, is Julia the ideal migration language?

\*\* I’m not sure, are programmers limited by keyboard speed and not thought? Maybe I am a little bit limited by my writing, e.g. right here responding, but for code, I spend more time thinking than typing. Still have heard of programmers using DVORAK and liking.

If the company forced DVORAK on all, I guess, many programmers would find themselves a new job… not sure if that applies to Julia.

---

<div class="post-metadata">

**Author:** ![pitao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitao/32/116_2.png) [@pitao](https://discourse.julialang.org/u/pitao)\
**Post date:** [January 8, 2017, 11:46am UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/43 "2017-01-08T11:46:08Z")

</div>

This is completely missing the central point of the analogy which is to illustrate the problem of a greedy strategy working off incremental change getting stuck in local minima of the cost function. That is why we still have QWERTY.

I am sure keyboard speed is of little consequence in a typical high level programming environment, but this conversation was never about keyboards!

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [January 8, 2017, 3:35pm UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/44 "2017-01-08T15:35:15Z")

</div>

> [@pitao](#):
>
> This is completely missing the central point of the analogy which is to illustrate the problem of a greedy strategy working off incremental change […] this conversation was never about keyboards!

Then I didn’t miss it completely. My much longer text, was not on keyboards (or language design); sticking with the language you know (least resistance) fits your analogy. Changing, say to Julia might help people.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [January 11, 2017, 1:29pm UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/45 "2017-01-11T13:29:35Z")

</div>

Just want to say that as a newb, not knowing about the short-circuit evaluation, I’ve been doing `cond ? true : error()` as a one-line check. This is obviously not very elegant, but it gets the job done. I’ll switch over to the short-circuit for now to be consistent with other code, but just want to add extra support behind `cond ? error()` as an option.

Speaking as a beginner, once you grok the ternary operator, I think this syntax would be immediately recognizable. In fact, I think I initially thought it would work because ternary looks like `if-else`, so it makes sense that just `if` would work as well.

---

<div class="post-metadata">

**Author:** ![schnetter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schnetter/32/45_2.png) [@schnetter](https://discourse.julialang.org/u/schnetter)\
**Post date:** [January 12, 2017, 3:17pm UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/46 "2017-01-12T15:17:42Z")

</div>

Regarding readability:

- If we use operators such as `&&` or `?` etc., then it’s a good idea to stick to semantics that exists in other languages, so that code is easier to read.
- If we use keywords for this, then we could also do:

```julia
length(a) == length(b) or else raise "problem!"
length(a) != length(b) and then raise "problem!"

```

That is, instead of using just `then` and `else` as keywords (difficult to parse), or just `and` and `or`, we can use two keywords. We could also (as we do in `elseif`) combine these to the keywords `orelse` and `andthen`.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [January 12, 2017, 4:03pm UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/47 "2017-01-12T16:03:38Z")

</div>

I was thinking yesterday, that since there has been talk of `Nullable{T}` might end up as simply a `Union{T, Void}`, that if we had the `cond ? expr` syntax, that it could actually return a `Nullable{typeof(expr)}` (when the value of `cond ? expr` is actually used, although frequently it would just be something with a side effect such as assignment or control flow). `if cond ; expr ; end` type is inferred to be `Union{typeof(expr), Void}` already.

(This may be a crazy idea, but it seems to be nicely consistent to me)

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [January 12, 2017, 4:39pm UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/48 "2017-01-12T16:39:12Z")

</div>

It might be nice of have `and` and `or` for `&` and `|`, and `andthen` and `orelse` for `&&` and `||`

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [January 14, 2017, 4:31am UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/49 "2017-01-14T04:31:44Z")

</div>

The `if cond then statement` one liner was always my favourite, and I don’t think it was disproved to be workable yet, was it? (Edit: I like it for explicit readability, I honestly don’t care at all if it’s as much typing as `if cond; statement; end` and more than `&&`).

There is some logic in leaving `?` unary and binary open for whatever progress is made on `Nullable` syntax (though `??` could be an option there).

---

<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:** [January 14, 2017, 6:25am UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/50 "2017-01-14T06:25:16Z")

</div>

> [@pitao](#):
>
> Pretty much every language is fine with its own syntax, just as people by and large are fine with the QWERTY keyboard (despite the keys all being in silly places) and will complain if their keyboard is replaced by a (superior) DVORAK layout.

Hasn’t it been pretty much debunked that DVORAK has any actual speed advantages for users after they master either form? (But that’s beside the point)

> [@pitao](#):
>
> Conversely if the entire company acts as a unit, gets their heads together, says ‘hey, if we collectively take a hit for a couple of months and put ourselves through some degree of frustration, we come out 20% more productive per year if we make the switch’ … then progress has been made!

But will this change actually make us 20% more productive? Or will `.`-fusing, array buffers, the type system overhaul, traits, Pkg3, the ability for precompilation to handle conditional dependencies, and dispatch on keyword arguments make us more productive? I am inclined to say that these issues are a better use of development time, so I wouldn’t want to burden any core developer whose working on anything close to this to spend even a week doing some syntax changes.

That’s why people are asking for this kind of stuff to be a PR. Even if it’s is “better” (debatable): do you think it’s work the development time? If it’s worth your time, then do it. Nobody else thinks it’s a good way to spend time.

Honestly, there are good syntax problems to explore: what’s a good syntax for traits? What about for the types for structs? etc. It’s much easier to see how discussions in those areas push the language forward than this.

It’s always about costs vs benefits.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [January 14, 2017, 2:24pm UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/51 "2017-01-14T14:24:15Z")

</div>

> [@ChrisRackauckas](#):
>
> Hasn’t it been pretty much debunked that DVORAK has any actual speed advantages for users after they master either form? (But that’s beside the point)

Not sure if debunked, but there doesn’t seem to be clear evidence either way, and that’s only looking at speed, the effect of using Dvorak (or some other alternative like Colemak: [https://colemak.com/](https://colemak.com/)) on RSI might be much more important, but I haven’t seen (but they might exist) any studies on RSI / carpal tunnel among QWERTY vs. Dvorak users.

> [@ChrisRackauckas](#):
>
> Nobody else thinks it’s a good way to spend time.

I disagree with only this one thing that you said (that _nobody_ thinks it’s a good way to spend time).  
I do agree that changes / additions like this haven’t bubbled to a higher priority among the core devs for them to work on this, and yes, I’d much rather that they work on things like the low-level Buffer type to implement arrays/strings more generically, trait syntax, fixing the problem with being able to use typevars in expressions in a type/immutable definition [i.e. evaluate the expressions only when they are bound to make a concrete type], Pkg3, etc…

If I’ve got a bit of time, I might try my hand at implementing binary `?`, having it return a `Nullable` (when it doesn’t just have some control flow for the expression).  
`result = ch <= '\U10FFFF' ? Char(ch)` would set `result` to a `Nullable{Char}`, which currently I think would have to be written as `result = ch <= '\U10FFFF' ? Nullable(Char(ch)) : Nullable{Char}()`.

---

<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:** [January 14, 2017, 2:45pm UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/52 "2017-01-14T14:45:10Z")

</div>

> [@ScottPJones](#):
>
> I disagree with only this one thing that you said (that nobody thinks it’s a good way to spend time).

Yeah, nobody was a bit strong of a word. A few people, one of which has submitted a PR (@Ismael-VC), do really care. Also it seems the people at Software Carpentry (at least a few) care:

> **[Why I Am Not Excited About Julia](https://software-carpentry.org/blog/2015/06/why-i-am-not-excited-about-julia.html)**
>
> Software Carpentry is a volunteer project dedicated to teaching basic computing skills to researchers.

I would however put it as very low priority, and think it should be clear that it’s not because I don’t like it (I prefer the `and` syntax), but because I don’t see it as a good use of time (I barely prefer the `and` syntax and would never ask anyone else to go through all of that work to make me less happy over a lifetime than a nice breakfast would).

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 14, 2017, 3:06pm UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/53 "2017-01-14T15:06:22Z")

</div>

(staying offtopic here, sorry)

1. what a lightweight blog post at Software Carpentry, but I like the first response
2. I changed to Colemak 2 years ago, I am faster than before, but primarily, I feel much more comfortable typing. This was always the main goal of that alternative layout, never speed.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [January 14, 2017, 3:17pm UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/54 "2017-01-14T15:17:51Z")

</div>

1. I find that a lot of the criticisms of Julia as it was 2 years ago (including my own!) are no longer valid (there really has been a lot of great progress since I first saw the language end of March 2015). v0.6 is IMO going to be a great release.
2. If I hadn’t been touch typing with QWERTY for 43 years, I’d switch to Colemak myself!

---

<div class="post-metadata">

**Author:** ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)\
**Post date:** [January 28, 2017, 1:11am UTC](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323/55 "2017-01-28T01:11:10Z")

</div>

What about trailing ifs (similar to comprehensions):  
a = 1.0  
a = 4.0 if x\<5  
a += 1.0 if a==2.0  
return if a == 0.0

look very clear to me.

an analogous construction would be:  
A[i] = i for i=1:5  
A[i,i] = 1.0 for i=1:10  
A[i,j] = i+j for i=1:3, j=1:3

which also seems to be useful and close to mathematical notation.

[Previous page](https://discourse.julialang.org/t/proposal-then-else-syntax-to-replace-short-circuiting/1323.md?page=2)
