# Efficient use of test or assert

**URL:** <https://discourse.julialang.org/t/efficient-use-of-test-or-assert/75895>\
**Category:** New to Julia\
**Tags:** error-message\
**Created:** [February 6, 2022, 9:14am UTC](https://discourse.julialang.org/t/efficient-use-of-test-or-assert/75895 "2022-02-06T09:14:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![benoitseron](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@benoitseron](https://discourse.julialang.org/u/benoitseron)\
**Post date:** [February 6, 2022, 9:14am UTC](https://discourse.julialang.org/t/efficient-use-of-test-or-assert/75895/1 "2022-02-06T09:14:39Z")

</div>

Suppose you want to check a property of some arguments given to a function, for instance, that x is positive.

What is the best practice regarding this?

I’m tempted to use `@test x>0` instead of writing a block such as

```julia
if x > 0
else 
	error("x<=0")
end

```

I’ve also came accross @assert but this seems to be for debugging purposes right?

So what is the best practice, considering that those conditions may be checked many times and thus may impact performance?

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [February 6, 2022, 9:19am UTC](https://discourse.julialang.org/t/efficient-use-of-test-or-assert/75895/2 "2022-02-06T09:19:48Z")

</div>

- `@test` is very slow and for tests.
- `@assert` is for double-checking invariants you are certaint are true. The only way it can fire, is when there are bugs in the program. E.g.

```julia
# good
y = _my_fast_sum_of_squares(xs)
@assert y >= 0

```

```julia
# bad
function public_api(x)
    @assert x >= 0
end

```

- [ArgCheck.jl](https://github.com/jw3126/ArgCheck.jl) is specifically designed for fast argument checks with nice error messages.

---

<div class="post-metadata">

**Author:** ![benoitseron](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@benoitseron](https://discourse.julialang.org/u/benoitseron)\
**Post date:** [February 6, 2022, 9:21am UTC](https://discourse.julialang.org/t/efficient-use-of-test-or-assert/75895/3 "2022-02-06T09:21:03Z")

</div>

Perfect, this is exactly what I needed! Thanks.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 6, 2022, 1:14pm UTC](https://discourse.julialang.org/t/efficient-use-of-test-or-assert/75895/4 "2022-02-06T13:14:59Z")

</div>

For a package-free alternative, `throw(DomainError(...))` also seems to be recommended in [this other thread](https://discourse.julialang.org/t/error-vs-throw-argumenterror/18915).

Example:

```julia
dB(x) = (x > 0) ? 20*log10(x) : throw(DomainError(x, "argument <= 0"))
dB(0.5) # -6.0
dB(-0.5) # ERROR: DomainError with -0.5: argument <= 0

```

---

<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:** [February 6, 2022, 4:36pm UTC](https://discourse.julialang.org/t/efficient-use-of-test-or-assert/75895/5 "2022-02-06T16:36:30Z")

</div>

Sorry, why `@assert` is “bad” to use in that circumstances? Should assert only trigger if the error is inside your code and not because external input? Ultimately, if it triggers for some inputs and not others, the input will be involved as the cause of the error.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [February 7, 2022, 1:57am UTC](https://discourse.julialang.org/t/efficient-use-of-test-or-assert/75895/6 "2022-02-07T01:57:09Z")

</div>

> [@Henrique\_Becker](#):
>
> Sorry, why `@assert` is “bad” to use in that circumstances?

`@assert` is [documented](https://docs.julialang.org/en/v1/base/base/#Base.@assert) that it can be turned off as an optimization, so it shouldn’t be used for validating input or anything like that; in the future it might only be run when running in a debug mode (eg [RFC: implement proper debug mode for packages (with support for re-precompilation) by KristofferC · Pull Request #37874 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/37874)). I have an open PR to add a macro `@check` that is just like `@assert` but would never be disabled: [Add `@check` macro for non-disable-able `@assert` by ericphanson · Pull Request #41342 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/41342)

---

<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:** [February 7, 2022, 1:32pm UTC](https://discourse.julialang.org/t/efficient-use-of-test-or-assert/75895/7 "2022-02-07T13:32:27Z")

</div>

I was not aware of these current and planned characteristics of the `@assert` macro, thank you for the info. Seems like I will need to rewrite some code in the future.
