# Nested functions pros and cons

**URL:** <https://discourse.julialang.org/t/nested-functions-pros-and-cons/19417>\
**Category:** General Usage\
**Created:** [January 9, 2019, 10:31am UTC](https://discourse.julialang.org/t/nested-functions-pros-and-cons/19417 "2019-01-09T10:31:21Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 10, 2019, 2:52pm UTC](https://discourse.julialang.org/t/nested-functions-pros-and-cons/19417/21 "2019-01-10T14:52:32Z")

</div>

> [@yakir12](#):
>
> write versions of the functions in the loop that short-circuit on specific values (values that indicate a failure).

That will most likely not SIMD either. `@simd` is not a magic command that makes the code go faster, it does a very specific thing. It enables fast mode math on reduction variables inside a loop so that the compiler is allowed to do floating point accumulations in arbitrary order.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [January 10, 2019, 2:59pm UTC](https://discourse.julialang.org/t/nested-functions-pros-and-cons/19417/22 "2019-01-10T14:59:44Z")

</div>

Thanks for the reality!  
Ok, I think I’ll stick to returning failure status and the actual result (or a dummy one if it failed).

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 10, 2019, 4:07pm UTC](https://discourse.julialang.org/t/nested-functions-pros-and-cons/19417/23 "2019-01-10T16:07:39Z")

</div>

> [@yakir12](#):
>
> Cool, I tried it now, but it seems that it won’t work across functions, right?

You cannot. Julia’s goto is limited to within a single function.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 10, 2019, 4:15pm UTC](https://discourse.julialang.org/t/nested-functions-pros-and-cons/19417/24 "2019-01-10T16:15:21Z")

</div>

> [@bennedich](#):
>
> I don’t have anything against `goto` s per se, but I’ve met a lot of programmers, even experienced ones, who have a very strong aversion to them, and will consider any usage of `goto` a sign of poor quality code and a lazy/inexperienced developer.

I suspect that most of this comes from Dijkstra’s “Go To Considered Harmful” essay, which people read and parrot without understanding the context. At the time this paper was written, structured control flow was a brand new concept and goto was how _all_ control flow was done in practice. Dijkstra was trying to convince people to use structured control flow _at all_ not convince them that they shouldn’t ever use goto.

The occasional, judicious use of goto is just fine, but (as always) consider the alternatives. If I can use a couple of gotos and have much clearer simpler code, that’s way better than writing the same thing with no gotos but a nasty, unintelligible mess of structured control flow—and there are some cases where this is the choice. The whole argument against goto was that unbridled use of it lead to complex unintelligible code, but readability and maintainability is the end goal by whatever means.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [January 11, 2019, 11:08am UTC](https://discourse.julialang.org/t/nested-functions-pros-and-cons/19417/25 "2019-01-11T11:08:40Z")

</div>

Just reporting back: I decided to implement a structure where functions return a result or `nothing` (depending on success/failure). This works really well because I could split functions into two, where the second part would have two methods, one for the real result and one short circuiting in case the type of the argument was `Nothing`. This cascades all the way through and felt pretty natural. I’m very happy with the end result, even though I’m not sure if this boosted the speed or hampered it.  
Thanks for all the help!

[Previous page](https://discourse.julialang.org/t/nested-functions-pros-and-cons/19417.md?page=1)
