Unreachable code should be removed, as it serves no purpose. Such code is typically a result of incorrect or unintended control flow in code. It is thus important to correctly structure your code and ensure that all code-paths are reachable and can be executed.
const
KT-P1001In Kotlin, using const
properties is preferred over regular properties for improved performance, efficiency, code clarity, and compile-time safety. const
properties are evaluated at compile-time, leading to faster execution and eliminating the need for runtime initialization. const
properties also enhance code readability and consistency by
When you declare a nested class with the inner
keyword, it becomes an inner class. Inner classes have a reference to the instance of the outer class that created them. As a result, they can access both the member properties and functions of the outer class. Inner classes are tightly bound to the instances of the outer class.
NoSuchElementException
KT-W1033NoSuchElementException
is a runtime exception that should be thrown when the next()
method is called on an iterator and there are no more elements to iterate over. This exception serves as a signal to the caller that there are no more elements in the collection.
Unreachable catch blocks should be removed, because they serve no useful purpose and can lead to confusing and potentially incorrect behavior in exception handling.
forEach
with ranges KT-P1000Using the forEach
method on ranges has a heavy performance cost. Prefer using simple for
loops.
This class/object has a name that does not match agreed-upon naming conventions. Not following naming conventions can lead to confusion among developers and inconsistency in the codebase, which can result in bugs and maintenance issues. It is important to maintain a consistent and easily understandable codebase by following the recommended naming conventions.
?: emptyList()
that can be replaced with orEmpty()
call KT-R1004In Kotlin, when you have a nullable collection (e.g., List?
, Set?
, Map?
) and you want to get a non-null version of it or an empty collection if the original collection is null
, you can use the orEmpty()
function as a more concise and idiomatic alternative to ?: emptyList()
.
In Kotlin, it is considered good practice to make variables non-nullable whenever possible and avoid marking them as nullable if they are expected to always have a value. Doing so improves code readability and clarity. It also serves as implicit documentation
Use the safe cast operator (as?
) to perform a fallible cast instead of using a condition to check the type before casting. This can simplify operations by directly converting the value to the desired type. The safe cast operator also evaluates to null if the cast fails, reducing verbosity, enforcing null safety, and avoiding an extra if
condition. It is considered idiomatic Kotlin to use as?
to cast expressions.
String
KT-P1002When converting primitive types to String
, you should avoid creating an object of the respective primitive type first and then using .toString()
for converting it to a string. Creating a temporary object is an unnecessary overhead and can incur a performance penalty.
is
or as
KT-W1000Avoid using is
to check the type of exceptions, or as
to cast exception types within catch blocks.
Avoid throwing NotImplementedError
and using TODO(...)
in code.
It is important to ensure that ranges specified in for
loops are valid. Ranges that are empty can lead to unexpected behavior.
hasNext()
on an Iterator
should not have any side-effects KT-W1032The hasNext()
method in Kotlin's Iterator
interface is used to check if there are any more elements in the collection that can be iterated over. This method should have no side-effects and should not modify the underlying collection or iterator state.
if
statements that can be collapsed into one KT-W1048Collapsing nested if statements into one improves code readability by making it more concise and easier to understand. It simplifies the logic, reduces cyclomatic complexity, and can lead to better performance. Debugging becomes easier, and code maintainability is enhanced. It is important to strike a balance between readability and line length while
abstract
modifier in interface detected KT-W1054In Kotlin, the abstract
keyword is redundant when declaring interfaces and their members. Interfaces are implicitly abstract in Kotlin, so there is no need to use the abstract
keyword, and the same is true for any members of an interface. Omitting the abstract
keyword does not affect the behavior or functionality of an interface, and allows for more concise code.
serialVersionUID
not found KT-W1060When a class implements Serializable
, it is recommended to also declare a serialVersionUID
field within that class. The serialVersionUID
serves as a version identifier for the serialized class. It helps to ensure compatibility between the serialized objects and their corresponding class definitions, particularly when performing deserialization.
In Kotlin, it is generally preferred to use a constant variable instead of a function returning a single constant for improved code clarity, performance, and maintainability. Constant variables are more readable, avoid unnecessary runtime overhead, and enable centralized value management. Additionally, they allow potential compiler optimizations
Developers often add TODO
or FIXME
comments to code that is not complete or needs review. It is a good idea to fix or review such commented code, and remove the comments before considering the code to be production-ready.