# Two equivalent codes but one returning error

**URL:** https://discourse.julialang.org/t/two-equivalent-codes-but-one-returning-error/100668
**Category:** New to Julia
**Created:** [June 21, 2023, 5:40pm UTC](https://discourse.julialang.org/t/two-equivalent-codes-but-one-returning-error/100668 "2023-06-21T17:40:08Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)
#### Post date: [June 21, 2023, 5:40pm UTC](https://discourse.julialang.org/t/two-equivalent-codes-but-one-returning-error/100668/1 "2023-06-21T17:40:08Z")

</div>

I was wondering if the two codes are different, because the second one returns an error message of “invalid index : nothing of type Nothing”

```julia
ind_one_y = findfirst(surplus[q,:,x,2,mq] .> 0.0) 

Code 1 :     
if isnothing(ind_one_y) == true 
   thr_one_y = 1e+8
else thr_one_y = grids.y[ind_one_y] - grids.mq[mq]
end 

Code 2 :
thr_one_y = ifelse(isnothing(ind_one_y) == true, 1e+8, grids.y[ind_one_y] - grids.mq[mq])

```

I’m just completely lost.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [June 21, 2023, 5:43pm UTC](https://discourse.julialang.org/t/two-equivalent-codes-but-one-returning-error/100668/2 "2023-06-21T17:43:34Z")

</div>

> [@Zerosum](#):
>
> `isno`

`ifelse` behaves like functions do, all its arguments are evaluated. This includes `grids.y[ind_one_y]`, which then errors.

---

<div class="post-metadata">

### Author: ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)
#### Post date: [June 21, 2023, 5:44pm UTC](https://discourse.julialang.org/t/two-equivalent-codes-but-one-returning-error/100668/3 "2023-06-21T17:44:25Z")

</div>

Thank you very much. I didn’t realize there is such subtle difference.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [June 21, 2023, 5:44pm UTC](https://discourse.julialang.org/t/two-equivalent-codes-but-one-returning-error/100668/4 "2023-06-21T17:44:32Z")

</div>

BTW the `== true` in `boolean == true` is redundant.

---

<div class="post-metadata">

### Author: ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)
#### Post date: [June 21, 2023, 6:01pm UTC](https://discourse.julialang.org/t/two-equivalent-codes-but-one-returning-error/100668/5 "2023-06-21T18:01:05Z")

</div>

Thank you for pointing that out. A silly mistake 🙂

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 21, 2023, 6:25pm UTC](https://discourse.julialang.org/t/two-equivalent-codes-but-one-returning-error/100668/6 "2023-06-21T18:25:38Z")

</div>

Note that Julia has a `?:` ternary operator like C/C++/Java and many other languages, so you can do:

```julia
thr_one_y = isnothing(ind_one_y) ? 1e+8 : grids.y[ind_one_y] - grids.mq[mq]

```

If you want to do it in a single-line.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [June 21, 2023, 6:50pm UTC](https://discourse.julialang.org/t/two-equivalent-codes-but-one-returning-error/100668/7 "2023-06-21T18:50:57Z")

</div>

Like the ternary `?:` operator, the `if-else` form can also be done in one line if each branch is 1 full line:

```julia
# OP's code
if isnothing(ind_one_y) == true thr_one_y = 1e+8 else thr_one_y = grids.y[ind_one_y] - grids.mq[mq] end 

# equivalent to Henrique's edit
thr_one_y = if isnothing(ind_one_y) 1e+8 else grids.y[ind_one_y] - grids.mq[mq] end

```

The `?` is better at delimiting the line imo

---

<div class="post-metadata">

### Author: ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)
#### Post date: [June 21, 2023, 7:40pm UTC](https://discourse.julialang.org/t/two-equivalent-codes-but-one-returning-error/100668/8 "2023-06-21T19:40:44Z")

</div>

Thank you very much for everyone.  
Indeed, I was trying ifelse to make the three-lined code to one.  
Everything looks beautiful!
