Detekt: Improving Kotlin Quality
Kotlin has several advantages over Java for writing codes. It is more simple, safe and concise than Java. However, there are not many analysis tools to work with Kotlin. If you are Android developer, you have already noticed about lint warning. In this post, I am going to configure a simple Android project which is written in Kotlin for lint format, custom lint rules with gradle tasks.
Let’s find a sample project written in Kotlin or you can use your own. I prefer “todo-mvp-kotlin” which is in Google Samples repository. Then, clone it and run. If everything works perfect, let’s add and configure detekt for this project.
First of all, add detekt plugin in your root build.gradle file.
plugins {
id "io.gitlab.arturbosch.detekt" version "1.0.0-RC14"
}
Now, run ./gradlew detektGenerateConfig task to generate default detekt config file.
Then we write a task to check detekt rules based on config file. In root gradle file run following task.
detekt {
toolVersion = "1.0.0-RC14"
input = files("$projectDir")
config = files("$project.projectDir/default-detekt-config.yml")
reports {
xml {
enabled = true
destination = file("$project.projectDir/detekt-report.xml")
}
html {
enabled = true
destination = file("$project.projectDir/detekt-report.html")
}
}
}
After running task, Android Studio shows build failed. Because, we have break some detekt rules.
Let’s see report file and fix some rules, the path is given by Android Studio like below.
Complexity Report:
— 5783 lines of code (loc)
— 3108 source lines of code (sloc)
— 2027 logical lines of code (lloc)
— 1719 comment lines of code (cloc)
— 556 McCabe complexity (mcc)
— 30 number of total code smells
— 55 % comment source ratio
— 274 mcc per 1000 lloc
— 14 code smells per 1000 llocProject Statistics:
— number of properties: 251
— number of functions: 411
— number of classes: 59
— number of packages: 12
— number of kt files: 52Successfully generated HTML report at /Users/abdullahbalta/StudioProjects/android-architecture/todoapp/detekt-report.html
Successfully generated Checkstyle XML report at /Users/abdullahbalta/StudioProjects/android-architecture/todoapp/detekt-report.xml
Now, fix naming titled issues, the issue says,
Variable names should follow the naming convention set in the projects configuration.
/Users/abdullahbalta/StudioProjects/android-architecture/todoapp/app/src/mock/java/com/example/android/architecture/blueprints/todoapp/data/FakeTasksRemoteDataSource.kt:27:5
Private variable names should match the pattern: (_)?[a-z][A-Za-z0–9]*/Users/abdullahbalta/StudioProjects/android-architecture/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksActivity.kt:36:5
Private variable names should match the pattern: (_)?[a-z][A-Za-z0–9]*
We change FakeTasksRemoteDataSource.kt:27:5 and TasksActivity.kt:36:5 code like these. Because, this type of naming is constant value naming and tasksServiceData is not a constant variable and we convert second variable to constant.
//FakeTaksRemoteDataSource.kt:27:5private val TASKS_SERVICE_DATA = LinkedHashMap<String, Task>()changed toprivate val tasksServiceData = LinkedHashMap<String, Task>()//TaskActivity.kt:36:5private val CURRENT_FILTERING_KEY = "CURRENT_FILTERING_KEY"changed tocompanion object {
private const val CURRENT_FILTERING_KEY = "CURRENT_FILTERING_KEY"
}
So, after fix these rules we re-run detekt tasks to see the result. Now, naming issues have disappeared.
Now, we can customize these rules in detekt-config.yml file. Let’s configure TooManyFunctions rule. The default value is set to 11, so we change to 30. After customizing, re-run the task and see the results. All TooManyFunctions issues disappear.
These rules provide some organization to your codes. Your team can make some decision to generate custom rules and every developer in team has following the rules, on the other hand project not build.