# Best practice for assertions for debugging purposes

**URL:** <https://discourse.julialang.org/t/best-practice-for-assertions-for-debugging-purposes/35930>\
**Category:** New to Julia\
**Tags:** debugging\
**Created:** [March 13, 2020, 3:12pm UTC](https://discourse.julialang.org/t/best-practice-for-assertions-for-debugging-purposes/35930 "2020-03-13T15:12:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![efokken](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@efokken](https://discourse.julialang.org/u/efokken)\
**Post date:** [March 13, 2020, 3:12pm UTC](https://discourse.julialang.org/t/best-practice-for-assertions-for-debugging-purposes/35930/1 "2020-03-13T15:12:23Z")

</div>

Hi there!  
I’m building a package and have started to use asserts in all my functions that check dimensions of the function arguments.  
As most of these functions are called from my own code only, these asserts should not be there once I’m finished.  
To this end I have enclosed them with  
@static if(debugging\_mode==true)

end

so that the debugging\_mode variable is checked during compilation but not anymore during runtime. Looking into the generated llvm code made me believe so.  
The problem is, that it is difficult to change the variable and force a recompilation. The only way I found reliable was to restart Julia.  
Therefore my question ist: How should I make these assertions (or should i use a different mechanism) so that I can switch them on and off during compile time and how do I then force recompilation of my functions?  
(In case you wonder: I’m using Revised but it doesn’t help here)

---

<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:** [March 13, 2020, 4:00pm UTC](https://discourse.julialang.org/t/best-practice-for-assertions-for-debugging-purposes/35930/2 "2020-03-13T16:00:57Z")

</div>

> **[GitHub - MasonProtter/ToggleableAsserts.jl: Assertions that can be turned on...](https://github.com/MasonProtter/ToggleableAsserts.jl)**
>
> Assertions that can be turned on or off with a switch, with no runtime penalty when they're off. - GitHub - MasonProtter/ToggleableAsserts.jl: Assertions that can be turned on or off with a swi...

Or just copy the trick from there: use a function that just returns true, then redefine it to false to turn off asserts. That will force recompilation of functions that use that function.

---

<div class="post-metadata">

**Author:** ![efokken](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@efokken](https://discourse.julialang.org/u/efokken)\
**Post date:** [March 13, 2020, 9:58pm UTC](https://discourse.julialang.org/t/best-practice-for-assertions-for-debugging-purposes/35930/3 "2020-03-13T21:58:55Z")

</div>

Thanks for the hint!

For future reference: Using a function works only, when NOT declaring the if as @static.  
But diggin into it a little I found that the compiler realizes on its own that the if statement can be resolved at compile time and will remove the code enclosed in an if branch that is not followed.  
The code I used in the end was:

```julia
function mydebugging()
    return true
end

```

and in the functions themselves:

```julia
function multiply(M,xi)
    # assert the input dimensions:
    if(mydebugging())
        @assert(size(M)==(2,2))
        @assert(size(xi)==(2,))
    end
    return M*xi
end

```

The @code\_llvm macro was helpful!

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [March 14, 2020, 4:54am UTC](https://discourse.julialang.org/t/best-practice-for-assertions-for-debugging-purposes/35930/4 "2020-03-14T04:54:36Z")

</div>

Another idea, if your arrays are small and statically sized (as in your example, where they have to be `2` x `2` is to use StaticArrays.jl.  
This will let you use types to constrain their size. Given that the arrays are small, StaticArrays should also improve performance.

---

<div class="post-metadata">

**Author:** ![efokken](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@efokken](https://discourse.julialang.org/u/efokken)\
**Post date:** [March 15, 2020, 12:01pm UTC](https://discourse.julialang.org/t/best-practice-for-assertions-for-debugging-purposes/35930/5 "2020-03-15T12:01:17Z")

</div>

That made quite the speedup, thanks!
