What is Konsist?
Konsist is a structural linter (static code analyzer) designed for Kotlin language. It aims to standardize the codebase by ensuring adherence to a common set of coding conventions, thereby improving the readability and maintainability of the code. It's like ArchUnit, but for Kotlin language.
The Konsist API provides developers with the capability to create custom checks through unit tests, customized to align with the project's unique requirements. Additionally, it offers smooth integration with leading testing frameworks, including JUnit4, JUnit5, and Kotest, further streamlining the development process.
Konsist tests can be executed during pull request reviews, act as safeguards to preserve the project's codebase structure and architecture. Whether you're working on Android, Spring, or Kotlin Multiplatform projects, Konsist has got you covered.
Konsist is approaching its 1.0 release, marking a significant milestone in its development journey. See the Project Status.
Konsist offers two types of checks, namely Declaration Checks and ArchitecturalChecks, to thoroughly evaluate the codebase.
Declaration Checks
The first type involves declaration checks, where custom tests are created to identify common issues and violations at the declaration level (classes, functions, properties, etc.). These cover various aspects such as class naming, package structure, visibility modifiers, presence of annotations, etc. Here are a few ideas of things to check:
Every child class extending
ViewModel
must haveViewModel
suffixClasses with the
@Repository
annotation should reside in..repository..
packageEvery class constructor has alphabetically ordered parameters
Every constructor parameter has a name derived from the class name
Field injection and
m
prefix is forbiddenEvery public member in
api
package must be documented with KDocand more...
Here is a sample test that verifies if every use case class resides in domain.usecase
package:
For more Konsist test samples see the Snippetssection.
ArchitecturalChecks
The second type of Konsist checks revolves around architecture boundaries - they are intended to maintain the separation of concerns between layers.
Consider this simple 3 layer of Clean Architecture:
The
domain
layer is independentThe
data
layer depends ondomain
layerThe
presentation
layer depends ondomain
layeretc.
Here is a Konsist test that verifies if Clean Architecture dependency requirements are valid:
These types of checks are useful when the architecture layer is defined by the package, rather than a module where dependencies can be enforced by the build system.
Summary
By utilizing Konsist, teams can be confident that their Kotlin codebase remains standardized and aligned with best practices, making code reviews more efficient and code maintenance smoother.
Last updated