Skip to content

[kotlin] New Rule: LooseCoupling #7092

Closed

Description

@stokpop

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, default java.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:

Activity

  1. added 2 commits that reference this issue on Sep 22, 2026
    1ef9baf
    dec2496
  2. added
    a:new-rule
    on Oct 23, 2026
  3. added this to the 7.29.0 milestone on Oct 23, 2026
  4. added a commit that references this issue on Oct 23, 2026
    8d4094b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:new-rule

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions