Repository navigation
Description
Proposed Rule Name: LooseCoupling
Proposed Category: Best Practices
Description:
Port of the existing pmd-java LooseCoupling
rule to Kotlin. Flags declared types (fields, properties, parameters, return types, local
variables) that use a concrete collection/map implementation (ArrayList, HashMap,
LinkedHashMap, HashSet, LinkedList, Hashtable, Vector, ...) instead of the corresponding
interface (List, Map, Set, ...). Excessive coupling to implementation types limits the
ability to swap implementations later.
This rule also serves as a real-world demonstration of the new Kotlin XPath type functions and
attributes added in #6893 — specifically @TypeName / pmd-kotlin:typeIs() /
pmd-kotlin:typeIsExactly() for detecting concrete collection types.
Kotlin-specific nuance vs. the Java rule: in Java, List/Map/Set are inherently mutable
interfaces, so there's only one target type to suggest. In Kotlin, List/Map/Set are
read-only views, while MutableList/MutableMap/MutableSet are the mutable counterparts.
A naive port that always suggests the read-only interface would be wrong (and often a compile
error at the call site) wherever the concrete type is actually mutated (put, remove, add,
...) after declaration/return. The rule needs to suggest the narrowest correct interface based
on how the value is used: read-only access → Map/List/Set; mutating access anywhere →
MutableMap/MutableList/MutableSet.
Verified against a real, unrelated open-source Kotlin Android app (525 .kt files): found 5
genuine instances of the pattern, e.g. (anonymized):
Code Sample:
class Example {
// violation - concrete type used for a property
internal val subscriptions: ArrayList<Closeable> = ArrayList()
// violation - concrete type used for a var
private var members: ArrayList<Int> = arrayListOf()
// violation - concrete return type, but only ever read downstream
// (order is preserved by the runtime object regardless of the declared type,
// so loosening to Map is safe here - Kotlin's Map iteration order comes from
// the underlying object, not the declared interface)
private fun sortItems(items: Collection<Item>): LinkedHashMap<String, Item> {
// ...
}
// preferred approach
private val subscriptions: MutableList<Closeable> = mutableListOf()
private fun sortItems(items: Collection<Item>): Map<String, Item> {
// ...
}
}Possible Properties:
allowedTypes(string list, defaultjava.util.Properties) — mirrors the Java rule's property,
allowing user-defined types that should be treated like interfaces to be exempted.
Not in scope / possible follow-up:
- No visibility restriction (matches upstream Java rule, which flags regardless of
public/private/internal). - Choosing between
Map/MutableMap(etc.) suggestions based on actual mutation usage may
require follow-up refinement; first version may start with a simpler heuristic or a single
generic message.
Related:
- Upstream Java rule:
LooseCoupling - Follows the pattern established by [kotlin] New Rule: LocalVariableShadowsParameter #6732 (demonstration rule using new Kotlin AST/XPath features)
- Related to [kotlin] Add XPath functions and type attributes #6893 ([kotlin] Add XPath functions and type attributes)
Activity
[kotlin] Add LooseCoupling rule
[kotlin] Add LooseCoupling rule
[kotlin] Fix pmd#7092: Add LooseCoupling rule (pmd#7094)