# Defensive programming & assert

**URL:** <https://discourse.julialang.org/t/defensive-programming-assert/8383>\
**Category:** General Usage\
**Created:** [January 15, 2018, 1:49pm UTC](https://discourse.julialang.org/t/defensive-programming-assert/8383 "2018-01-15T13:49:05Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [January 15, 2018, 9:29pm UTC](https://discourse.julialang.org/t/defensive-programming-assert/8383/11 "2018-01-15T21:29:57Z")

</div>

Here’s a way this can be done for modules without breaking precompilation.

```julia
module DebugAsserts

export debug_asserts, @dassert

function debug_asserts(m::Module, dodebug::Bool)
    m.eval(:(_debug_enabled() = $dodebug))
    nothing
end

# Like @assert, but only active in debug mode
macro dassert(exs...)
    if !isdefined( __module__ , :_debug_enabled)
        eval( __module__ , :(_debug_enabled() = false))
    end
    quote
        if $ __module__._debug_enabled()
            @assert $(map(esc,exs)...)
        end
    end
end

end

```

---

_[View the full topic](https://discourse.julialang.org/t/defensive-programming-assert/8383)._
