# Debug Assert

**URL:** <https://discuss.kotlinlang.org/t/debug-assert/13707>\
**Category:** Support\
**Created:** [August 2, 2019, 4:28am UTC](https://discuss.kotlinlang.org/t/debug-assert/13707 "2019-08-02T04:28:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bessermt](https://avatars.discourse-cdn.com/v4/letter/b/bbe5ce/32.png) [@bessermt](https://discuss.kotlinlang.org/u/bessermt)\
**Post date:** [August 2, 2019, 4:28am UTC](https://discuss.kotlinlang.org/t/debug-assert/13707/1 "2019-08-02T04:28:52Z")

</div>

Does Kotlin have a assert-like method that can only be included in debug builds?

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [August 2, 2019, 7:25am UTC](https://discuss.kotlinlang.org/t/debug-assert/13707/2 "2019-08-02T07:25:09Z")

</div>

There is a [require](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/require.html) method which is almost free in runtime. You can also use Java `assert` on JVM, but I do not think it is recommended to do so right now.

Do you have a use case to share?

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [August 2, 2019, 8:01am UTC](https://discuss.kotlinlang.org/t/debug-assert/13707/3 "2019-08-02T08:01:58Z")

</div>

Please search for previous topic.

> [@Java like "assert" statement](https://discuss.kotlinlang.org/t/java-like-assert-statement/2277):
>
> Both Java and Kotlin have “assert” statement, they look equals but are greatly different. Java assert is per class enabled and expression evaluation is done only when assertion is enabled. By default, assertions are disabled, so you must not assume that the Boolean expression contained in an assertion will be evaluated. Therefore, your expressions must be free of side effects. [http://www.oracle.com/us/technologies/java/assertions-139853.html](http://www.oracle.com/us/technologies/java/assertions-139853.html) Differently, Kotlin assertion is a simple functi…

---

<div class="post-metadata">

**Author:** ![bessermt](https://avatars.discourse-cdn.com/v4/letter/b/bbe5ce/32.png) [@bessermt](https://discuss.kotlinlang.org/u/bessermt)\
**Post date:** [August 5, 2019, 3:21am UTC](https://discuss.kotlinlang.org/t/debug-assert/13707/4 "2019-08-05T03:21:49Z")

</div>

I have a number of use cases. I’m not sure which one (or ones) would help the most. I can dig one up a bit later, but basically they all come down to the same thing. I want my program to tell me (the programmer) in debug immediately when something happens I didn’t expect (range checks etc.) and in release, I want the code to do some configured behavior (log, email, … ) and possibly do some kind of correction to keep the program running in a stable state or possibly tell the user something is wrong, depending on the importance of the user knowing about the issue. I’m aware I don’t want side effects in the test and almost always I just want the program to stop (see [Fail-fast - Wikipedia](https://en.wikipedia.org/wiki/Fail-fast)) running, but there might be other ways that would work. I’m used to systems where I get a notification of the problem as the programmer and can choose to silence the problem temporarily and record the problem in the environment to review at the end.

Does that help?

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [August 5, 2019, 4:38am UTC](https://discuss.kotlinlang.org/t/debug-assert/13707/5 "2019-08-05T04:38:06Z")

</div>

Well, this one is easy to solve:

```kotlin
val debug = System.getProperty("debug").toBoolean() //or something similar

fun myError(block: () -> String) = if(debug){
  error(block)
} else {
  logger.warning(block)
}

```

Just add similar code to your program and you can use it wherever you need. The real problem arises when people try to remove debug code from release version.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [August 5, 2019, 5:43am UTC](https://discuss.kotlinlang.org/t/debug-assert/13707/6 "2019-08-05T05:43:42Z")

</div>

Hi @darksnake,  
I don’t see a reason to declare `block` as a lambda, in your example `block` is always evaluated.

It can be replaced with `fun myError(message: String)`, what do you think?

---

<div class="post-metadata">

**Author:** ![bessermt](https://avatars.discourse-cdn.com/v4/letter/b/bbe5ce/32.png) [@bessermt](https://discuss.kotlinlang.org/u/bessermt)\
**Post date:** [August 11, 2019, 5:31am UTC](https://discuss.kotlinlang.org/t/debug-assert/13707/7 "2019-08-11T05:31:53Z")

</div>

require looks like it throws an exception, not what I want. The assert looks like it exists regardless of build settings.

myError looks like it would be included in both release and debug.

I think I have my answer though. I can’t select what code to compile while compiling like I’m used to having. I would have to write my own pre-compile processing step and somehow integrate that into my build steps. Please correct me if I’m wrong. Thanks.

---

<div class="post-metadata">

**Author:** ![bessermt](https://avatars.discourse-cdn.com/v4/letter/b/bbe5ce/32.png) [@bessermt](https://discuss.kotlinlang.org/u/bessermt)\
**Post date:** [August 11, 2019, 5:34am UTC](https://discuss.kotlinlang.org/t/debug-assert/13707/8 "2019-08-11T05:34:08Z")

</div>

> [@darksnake](#):
>
> error

I can’t find any docs on the error function. Is that something that might only be included in debug builds?

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [August 11, 2019, 6:01am UTC](https://discuss.kotlinlang.org/t/debug-assert/13707/9 "2019-08-11T06:01:45Z")

</div>

Some references for you:

> **[assert - Kotlin Programming Language](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/assert.html)**

> **[error - Kotlin Programming Language](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/error.html)**

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [August 11, 2019, 1:06pm UTC](https://discuss.kotlinlang.org/t/debug-assert/13707/10 "2019-08-11T13:06:39Z")

</div>

> [@bessermt](#):
>
> I think I have my answer though. I can’t select what code to compile while compiling like I’m used to having. I would have to write my own pre-compile processing step and somehow integrate that into my build steps. Please correct me if I’m wrong. Thanks.

One way to achieve something like this would be

```kotlin
const val DEBUG_ENABLED = true
inline fun DEBUG(block: () -> Unit) {
    @Suppress("ConstantConditionIf")
    if(DEBUG_ENABLED) block()
}
inline fun RELEASE(block: () -> Unit) {
    @Suppress("ConstantConditionIf")
    if(!DEBUG_ENABLED) block() 
}

```

As far as I know the compiler should just optimize away the if condition and the unused debug/release code.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [August 11, 2019, 6:02pm UTC](https://discuss.kotlinlang.org/t/debug-assert/13707/11 "2019-08-11T18:02:12Z")

</div>

> [@bessermt](#):
>
> I can’t select what code to compile while compiling like I’m used to having.

Yes, you can.  
However this slows the compilation without any performance gain on JVM.  
Dead code elimination is already implemented on JVM and popular Android minifiers.

> [@Wasabi375](#):
>
> As far as I know the compiler should just optimize away the if condition and the unused debug/release code.

You are right.
