# When is it okay to use \`@assert\`?

**URL:** <https://discourse.julialang.org/t/when-is-it-okay-to-use-assert/114399>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 17, 2024, 2:08pm UTC](https://discourse.julialang.org/t/when-is-it-okay-to-use-assert/114399 "2024-05-17T14:08:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [May 17, 2024, 2:08pm UTC](https://discourse.julialang.org/t/when-is-it-okay-to-use-assert/114399/1 "2024-05-17T14:08:40Z")

</div>

I see `@assert` used frequently in the wild to check function inputs, and it looks a lot nicer than putting a bunch of `if`/`throw` statements at the top of every function.

However, ModernJuliaWorkflows says:

> To test the arguments provided to the functions within your code (for instance their sign or value), avoid `@assert` (which can be deactivated) and use [ArgCheck.jl](https://github.com/jw3126/ArgCheck.jl) instead.

and `@assert`’s docstring says:

> Warning  
> │ An assert might be disabled at various optimization levels. Assert  
> │ should therefore only be used as a debugging tool and not used for  
> │ authentication verification (e.g., verifying passwords), nor should  
> │ side effects needed for the function to work correctly be used  
> │ inside of asserts.

So,

1. Why do I still see `@assert` in so many popular packages despite the warnings?
2. Can’t we just update `@assert` to natively do whatever makes `ArgCheck` better?

It seems silly to tell people to go find external package tools because the built-in tools are not reliable enough. (I wonder similarly about `@time` and `@btime`.)

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [May 17, 2024, 2:23pm UTC](https://discourse.julialang.org/t/when-is-it-okay-to-use-assert/114399/2 "2024-05-17T14:23:46Z")

</div>

> [@Nathan\_Boyer](#):
>
> Why do I still see `@assert` in so many popular packages despite the warnings?

because

> An assert might be disabled at various optimization levels

never actually happens, despite the fact that the docs warn it might

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [May 17, 2024, 5:28pm UTC](https://discourse.julialang.org/t/when-is-it-okay-to-use-assert/114399/3 "2024-05-17T17:28:49Z")

</div>

`||`/`&&` are also really useful for these short error checks and not quite as large/tedious as the equivalent `if` block. They’re still longer than a naked `@assert` (with the auto-generated error message) but about as long as one with a custom message.

Like

```julia
x > 0 || throw(DomainError(x,"x must be positive"))

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [May 17, 2024, 5:31pm UTC](https://discourse.julialang.org/t/when-is-it-okay-to-use-assert/114399/4 "2024-05-17T17:31:23Z")

</div>

`@assert` removal might happen at some point.

> <https://github.com/JuliaLang/julia/pull/53404>
>
> This PR adds a new flag to control when a "julia specific" debug mode is active.… When the debug mode is active the following happens:
> \- The \`@assert\` macro is active
> \- The \`isdebug\` function returns \`true\`.
> 
> Currently, the \`--debug\` has three options:
> \- \`"yes"\`: debug mode is active in scripts and packages
> \- \`"script"\`: debug mode is active in scripts (default)
> \- \`"no"\`: debug is not active anywhere
> 
> The debug mode is a "compile time option" meaning that it will recompile packages if it changes.
> 
> There are still many open questions regarding this:
> 
> \- Should this instead use \`-g\` and coexist with the current behavior of \`-g\` or should it just steal it?
> \- Are the defaults here reasonable?
> \- When running \`Pkg.test\` should assertions be enabled everywhere or only in the tested package etc?
> \- Will it be expected that people run their tests under both debug and normal mode?
> \- Should \`--debug\` has any influence on \`--check-bounds\`?
> \- Probably many more
> 
> Subsumes https://github.com/JuliaLang/julia/pull/37874
> 
> Ref https://github.com/JuliaLang/julia/pull/41342, https://github.com/JuliaLang/julia/pull/53223

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [May 17, 2024, 7:35pm UTC](https://discourse.julialang.org/t/when-is-it-okay-to-use-assert/114399/5 "2024-05-17T19:35:32Z")

</div>

I personally do not like seeing Boolean logic for control flow like that. It seems like a computer science hack that is not very readable for mortals. [Related Github Issue](https://github.com/JuliaLang/julia/issues/16389)

> [@jar1](#):
>
> `@assert` removal might happen at some point.

I can’t really follow the discussion, but hopefully we get a suitable alternative before `@assert`s are just turned off!

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [May 17, 2024, 8:04pm UTC](https://discourse.julialang.org/t/when-is-it-okay-to-use-assert/114399/6 "2024-05-17T20:04:25Z")

</div>

`p || error("p")` or `ArgCheck.@check` are ways to write enforced checks.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [May 17, 2024, 8:32pm UTC](https://discourse.julialang.org/t/when-is-it-okay-to-use-assert/114399/7 "2024-05-17T20:32:41Z")

</div>

Since `@assert` may be removed in future, you should avoid using `@assert` with side effects as an essential part the program flow. For example, code like this would be problematic:

```julia
v = [1, 2]
@assert pop!(v) == 2 # may stop working in future
println(last(v))

```

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [May 17, 2024, 8:48pm UTC](https://discourse.julialang.org/t/when-is-it-okay-to-use-assert/114399/8 "2024-05-17T20:48:43Z")

</div>

> [@Nathan\_Boyer](#):
>
> hopefully we get a suitable alternative before `@assert`s are just turned off!

I don’t think the plan is to just turn them off, but rather to add flags or command-line options to allow a user to turn them on/off.
