# Philosophy on asserts

**URL:** <https://discourse.julialang.org/t/philosophy-on-asserts/26587>\
**Category:** Internals & Design\
**Created:** [July 20, 2019, 3:47pm UTC](https://discourse.julialang.org/t/philosophy-on-asserts/26587 "2019-07-20T15:47:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)\
**Post date:** [July 20, 2019, 3:47pm UTC](https://discourse.julialang.org/t/philosophy-on-asserts/26587/1 "2019-07-20T15:47:30Z")

</div>

Curious what the view on asserts in released, production code is.

Working on PR for Pkg.TOML. It has lots of places that trigger assert error on nothing values with a get method: `get(x) = (@assert !isnull(x); x)`. In the package, nothing is often acceptable, but requires explicit handling. Note that isnull() returns true or false based when whether the argument is nothing–precedes the isnothing() function in Julia 1.1.

In every instance of the use of this get method, it is wrapped in or preceded by an if statement that properly handles the nothing value, consistent with the rules of TOML, including generating appropriate errors where necessary.

Before all these “nothings” were explicitly handled, the asserts were a good testing strategy–if you have the appropriate test cases, you’ll find out where you failed to handle a nothing. In the production code, should the asserts be left in? As the code is currently written, none of the asserts can be raised. I’m inclined to remove them.

For comparison, the JSON package in the registry contains no @asserts.

---

<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:** [July 20, 2019, 3:52pm UTC](https://discourse.julialang.org/t/philosophy-on-asserts/26587/2 "2019-07-20T15:52:38Z")

</div>

`@assert` is documented that it might be removed at some optimization level in the future (it currently is never removed).

So doing something like

```julia
@assert password_is_correct(pwd)
launch_the_nukes()

```

is not recommended.

---

<div class="post-metadata">

**Author:** ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)\
**Post date:** [July 20, 2019, 5:01pm UTC](https://discourse.julialang.org/t/philosophy-on-asserts/26587/3 "2019-07-20T17:01:18Z")

</div>

Why does your comment seem so topical?  
😊

Given that, I’ve removed the asserts. They were, in any case, no ops.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [July 21, 2019, 4:03am UTC](https://discourse.julialang.org/t/philosophy-on-asserts/26587/4 "2019-07-21T04:03:12Z")

</div>

> [@lewis](#):
>
> Given that, I’ve removed the asserts.

I don’t see how that follows from what Kristoffer said.  
I think the message is that (in the future), Julia will have a runtime option that can disable `@assert` statements, so don’t use for critical control flow.

> [@lewis](#):
>
> They were, in any case, no ops.

Are they?  
Aren’t they runtime checks “asserting” x is not null?

---

<div class="post-metadata">

**Author:** ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)\
**Post date:** [July 21, 2019, 5:24am UTC](https://discourse.julialang.org/t/philosophy-on-asserts/26587/5 "2019-07-21T05:24:47Z")

</div>

I noted that each assert was a branch in an if statement that already tested explicitly for nothing values or immediately preceded by a test and return. None of the asserts could ever be raised because of these explicit tests.

In the code I referenced nothing values are often valid, signaling that no more input data was present in a stream. The if statements ensured that proper values were returned (or assigned). This is better than raising an assert and aborting. The asserts  
were a great way to find all those places during testing.

The Julia version seems clearer than one of the Python implementations I looked at. It seems reasonable that the first person who wrote it started with code in another language, did much more than a port to Julia, but like anyone had only so much time.

Neither Kristoffer nor I wrote this code. It serves its purpose and neither of us invested that much time in it. Like lots of code, another pair of eyes finds something to make incrementally better, fix a bug, nudge performance a bit or just do some housekeeping.

The feature to suppress asserts with a runtime switch is great. Asserts that test for required conditions can be left in the source and be easily switched back on for testing.

- Lewis

---

<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:** [July 21, 2019, 5:54am UTC](https://discourse.julialang.org/t/philosophy-on-asserts/26587/6 "2019-07-21T05:54:45Z")

</div>

You may be interested in

> **[GitHub - jw3126/ArgCheck.jl: Package for checking function arguments](https://github.com/jw3126/ArgCheck.jl)**
>
> Package for checking function arguments. Contribute to jw3126/ArgCheck.jl development by creating an account on GitHub.
