GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [kotlin] New Rule: LooseCoupling · Issue #7092 · pmd/pmd

[kotlin] New Rule: LooseCoupling #7092

New issue
Copy link
New issue
Copy link
Closed
#7094
Closed
[kotlin] New Rule: LooseCoupling#7092
#7094
Copy link
Labels
a:new-ruleProposal to add a new built-in rule
Milestone
7.29.0

Description

@stokpop
stokpop
opened on Sep 21, 2026
Issue body actions

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:

  • 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)
Reacted by Jeroen Borgers

Activity

  1. stokpop
    added 2 commits that reference this issue on Sep 22, 2026

    [kotlin] Add LooseCoupling rule

    This issue will close once commit 1ef9baf is merged into the 'main' branch.
    1ef9baf

    [kotlin] Add LooseCoupling rule

    This issue will close once commit dec2496 is merged into the 'main' branch.
    dec2496
  2. adangel
    added
    a:new-ruleProposal to add a new built-in rule
    on Oct 23, 2026
  3. adangel
    added this to the 7.29.0 milestone on Oct 23, 2026
  4. adangel
    closed this as completedin #7094on Oct 23, 2026
  5. adangel
    added a commit that references this issue on Oct 23, 2026

    [kotlin] Fix pmd#7092: Add LooseCoupling rule (pmd#7094)

    This issue will close once commit 8d4094b is merged into the 'main' branch.
    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-ruleProposal to add a new built-in rule

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.29.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.