On October 3rd, Mercedes-Benz.io hosted Kotlin Lisboa Meetup #9 at its Lisbon office, focusing on
immutability, static code analysis, and the transition from Java to Kotlin. Meetups like this highlight the
value of knowledge sharing, as our teams and the wider community come together to learn,
exchange ideas, and push the boundaries of software development. We are dedicated to fostering
continuous learning and growth through shared expertise, which strengthens the
innovation-driven culture we're so proud of.
The evening featured insightful presentations, including two by our fellow MB.ioneers.
This is one of them.
In the ever-evolving world of software development, maintaining clean and consistent code is crucial, especially when
working with large teams or complex projects. As the popularity of Kotlin grows in the software industry, so does the
need for robust tools that ensure not only code quality but also architectural consistency. Enter Konsist – a powerful
static code analysis tool designed specifically for Kotlin. Unlike traditional linters, Konsist offers a deeper focus
on maintaining a project’s architecture and enforcing coding conventions at a higher level.
The Challenges of Architectural Consistency
In any software project, the separation of concerns, encapsulation, and maintainability are cornerstones of good
architecture. Without a tool to enforce these principles, developers often find themselves dealing with technical debt,
spaghetti code, or inconsistent design patterns. For Kotlin projects, tools like Detekt and Ktlint help maintain code
quality by flagging common issues, such as unused parameters, outdated documentation, wild card imports, and several
types of code smells. However, these tools don’t address the architectural integrity of the project, where layers of the
system may inadvertently interact or evolve into tightly coupled codebases over time.
For example, it’s common for controller classes to accidentally depend on database models or repositories, having access
to the persistence layer. These are subtle architectural issues that can lead to larger problems down the road, such as
difficulty in scaling the project or implementing new features.
How Konsist Fills the Gap
Konsist brings a fresh approach to code consistency in Kotlin by focusing on enforcing project’s architecture and coding
conventions. With Konsist, developers can write rules that go beyond just checking syntax or styling; the tool can
ensure that certain layers in the application architecture don’t violate dependencies, making sure that domain rules are
followed and architectural patterns such as hexagonal, onion, 3-tier, etc. are respected; also, one can enforce coding
standards like naming conventions, package structure, classes visibility, coroutines usage, etc. We can even go further
and check that all classes in our projects have tests!
The real beauty of Konsist approach is that you can write these rules as tests, like you would write your unit tests. I
will not be biased, but since we are using Kotlin it for sure makes sense to use a testing framework that is Kotlin
native such as Kotest. Either way you can choose one from the pool of available testing frameworks to write your rules.
For example, imagine you want to ensure that your controllers do not leak database entity classes and will only return
DTOs (Data Transfer Objects). Konsist allows you to define this rule and enforce it across your entire project. If
someone violates this rule and if you have your CI/CD checks in place to ensure that tests in your application run with
success, this will be automatically detected during the code review process, preventing code violations from making
their way into production.
By doing so, Konsist transforms static code analysis from simply checking code style to becoming a more robust guardian
of architecture and coding standards, which is especially valuable in larger teams or when working on complex projects
large codebases.
When compared to tools like Detekt, Ktlint, and ArchUnit, Konsist offers an unparalleled focus on architecture and
coding standards targeting Kotlin language features. While Detekt and Ktlint focus primarily on code smells and coding
style issues, Konsist takes a more holistic view of your project, leveraging the power of testing frameworks into the
mix. ArchUnit, while following a similar mindset regarding architectural verifications, it is a tool focused on Java.
Even though ArchUnit works for all JVM based languages, since it operates at the bytecode level, it lacks the
Kotlin-specific insights that Konsist provides, which in the end will require some deep knowledge on the generated
bytecode, forcing us to write custom extensions to ArchUnit, to deal with Kotlin specificities, making it more
error-prone and harder to maintain. Also, since it operates at the bytecode level Kotlin Multiplatform (KMP) support is
out of the equation, since for example when targeting JavaScript there is no bytecode to be analysed!
Konsist ensures that your Kotlin project maintains not just clean code, but clean architecture, which is key for
scalability and maintainability in enterprise-level applications.
Real-World Impact
In real-world applications, Konsist can be invaluable for teams working with complex projects and large codebases.
Enforcing architectural and coding standard checks helps developers to keep a clean codebase, follow SOLID principles,
avoid bugs that arise from tight coupling between application layers, and avoid common pitfalls that can lead to
technical debt. It also helps to ensure that the codebase is scalable and maintainable, which is crucial for long-term
success.
Another key use case for Konsist (alongside with other tools like Detekt and Ktlint) is enforcing coding conventions
across large teams. With several developers working on the same project, it’s easy for coding styles to diverge, since
every developer has its own signature when writing code. This can lead to inconsistencies in the codebase, making it
harder to read, maintain, and debug. By enforcing coding standards, Konsist ensures that all code written in the project
follows the same conventions, making it easier for developers to understand and collaborate.
Konsist in CI/CD Pipelines
One of the cool features of Konsist is its seamless integration with CI/CD pipelines because it is part of your
application testing. This allows teams to enforce architectural rules automatically during the build process, catching
potential violations early before they make their way into production. Konsist can be integrated in any CI tool like
Jenkins, CircleCI, or GitHub Actions, providing continuous feedback to developers and ensuring that code quality remains
high at all stages of development.
Why Knowledge Sharing Matters in the Tech Industry
In the fast-paced world of technology, the importance of knowledge sharing cannot be overstated. In an industry that
constantly evolves, collaboration is key to staying ahead of the curve. When we share knowledge, we foster innovation,
create opportunities for mentorship, and elevate the entire community. Tools like Konsist, which streamline processes
and enforce high standards, are a direct result of such collaboration.
Knowledge sharing also ensures that best practices are passed down, not just within a company but across the industry.
It encourages continuous learning, helping developers refine their skills and adopt new tools that can improve both the
quality and efficiency of their work. By fostering an environment of openness and collaboration, the tech industry
continues to push the boundaries of what's possible.
Conclusion
Konsist is more than just a static code analysis tool – it’s a game-changer for Kotlin developers. By focusing on
architectural consistency and providing an easy way to enforce coding conventions, Konsist ensures that your codebase
remains scalable, maintainable, and clean. As Kotlin continues to grow in popularity, tools like Konsist will become
indispensable for teams looking to push the boundaries of software development while maintaining high-quality standards.
If you haven’t explored Konsist yet, now is the time. Start integrating it into your workflow and see firsthand how it
can transform your codebase and drive your projects toward long-term success.