Treating Warnings As Errors In Kotlin

JAVING
2 min readMar 3, 2021

On my previous article a talked about the Kotlin compiler and I showed it’s basic command line usage. I just want to briefly extend on that article to remind of a feature that Kotlin compiler has that it’s quite useful but often forgotten.

A code base that throws a lot of warnings is a code base that contains risk. It’s just matter of time until one of those warnings will cause an issue. Maybe we are using a deprecated library, or maybe we forgot to use a parameter, etc … As Agile developers we should always strive to make our code maintainable, readable, simple and risk free; hence treating warnings as errors is considered a good Agile practice.

Let’s look at an example. I am going to compile a Kotlin class that has a function with some unused parameters. By default this will compile and generate the .jar file but the console will print some warnings.

some code that has unused parameters
command used to compile
warnings displayed in the console
.jar file is created despite there are warnings

As shown in the sequence of screenshots above the .jar file was created in the place where we executed the compilation command. Ignoring those warnings could potentially cause a bug. In order to make sure we are treating them as errors, we need to use the -Werror option that Kotlin compiler gives us.

Compile command using -Werror option
Warnings are now displayed as errors and the .jar file will not be created

--

--

JAVING

The present continuous form of “to program in Java”.