Agent Skillschrisbanes/skills › kotlin-functions

kotlin-functions

GitHub

指导Kotlin函数归属决策,优先将行为置于最小语义所有者。严格限制对String等类型的扩展,通过成员、顶层或工厂函数确保领域纯净与代码清晰。

skills/kotlin-functions/SKILL.md chrisbanes/skills

Trigger Scenarios

选择Kotlin函数形式 决定String或集合扩展归属 重构函数所有权

Install

npx skills add chrisbanes/skills --skill kotlin-functions -g -y
More Options

Use without installing

npx skills use chrisbanes/skills@kotlin-functions

指定 Agent (Claude Code)

npx skills add chrisbanes/skills --skill kotlin-functions -a claude-code -g -y

安装 repo 全部 skill

npx skills add chrisbanes/skills --all -g -y

预览 repo 内 skill

npx skills add chrisbanes/skills --list

SKILL.md

Frontmatter
{
    "name": "kotlin-functions",
    "description": "Use when choosing Kotlin member, top-level, extension, factory, or service functions for String, primitive, collection, Flow, framework, or third-party receivers."
}

Kotlin function ownership

Core principle

Put a function on the smallest accurate semantic owner. Extension syntax changes call shape, not ownership.

Reject primitive, common, and library-owned extensions by default: they create false ownership, domain pollution, noisy completion/imports, and collisions.

Procedure

Apply in order.

1. Name the semantic owner

Name the operation and the concept that owns it. If ownership is unclear, stop before selecting syntax.

2. Reject a misleading receiver early

For String, primitives, collections, Flow, framework, or third-party receivers, require all:

  • Narrow private/internal cohesive scope.
  • Valid for every receiver value.
  • No policy, state, I/O, or dependency.
  • Materially clearer receiver syntax.
  • No better project-owned owner.

Any failure forbids an extension on that receiver; choose a non-extension form in step 3. private fun <T> MutableList<T>.swap(...) can pass: it is list-native, policy-free, and algorithm-local.

3. Choose the function form

Meaning Prefer
Project-owned intrinsic behavior Member
Cross-type, stateless operation Top-level function
Construction or parsing Target factory or named top-level function
Retained policy, state, I/O, clock, locale, or dependencies Injected service/collaborator
Type-native operation with a clearer receiver and every step-2 gate passed Extension

Use a service/collaborator only when behavior retains policy, state, I/O, clock/locale, or dependencies; otherwise use explicit parameters on a stateless function.

4. Move behavior and callers

Move the implementation, then update calls, imports, and function references. Preserve or deprecate public entry points unless this is an explicit breaking release; add non-public migration support only for concrete consumers.

// Before: String falsely owns UserId construction.
fun String.toUserId(): UserId = UserId(this)

// After: UserId owns construction.
@JvmInline
value class UserId private constructor(val value: String) {
    companion object {
        fun parse(raw: String): UserId = UserId(raw)
    }
}

val id = UserId.parse(raw)

5. Verify and finish

For every form, check visibility, imports, collisions, and compatibility. For extensions, also check nullable receivers, generics, and future-member precedence. Compile and test; on failure, narrow the API or return to step 1.

Rationalizations

“But…” Counter
Fluent syntax Readability does not create ownership.
Kotlin uses extensions Idiom still requires accurate semantics.
It is private/internal Scope helps only when every gate passes.
Utility objects are worse Use a top-level function or target factory.
Default policy is obvious Time zone/locale defaults are policy; keep them explicit.
Already in the PR Existing code does not prove ownership.

Red flags

  • Domain meaning on String, numbers, collections, Flow, or vendor types.
  • Clock, locale, I/O, policy, or dependencies hidden in an extension.

Common mistakes

Mistake Fix
Long.toDisplayDate() A formatter owns time-zone/locale policy.
Extension hides parsing Use Type.parse(raw) or a named parser.
Public library-type extension Reclassify it using steps 1-3.

Related

Version History

  • 2026.7.21 Current 2026-07-24 12:25

Same Skill Collection

skills/compose-animations/SKILL.md
skills/compose-focus-navigation/SKILL.md
skills/compose-modifier-and-layout-style/SKILL.md
skills/compose-recomposition-performance/SKILL.md
skills/compose-side-effects/SKILL.md
skills/compose-slot-api-pattern/SKILL.md
skills/compose-stability-diagnostics/SKILL.md
skills/compose-state-authoring/SKILL.md
skills/compose-state-deferred-reads/SKILL.md
skills/compose-state-hoisting/SKILL.md
skills/compose-state-holder-ui-split/SKILL.md
skills/compose-ui-testing-patterns/SKILL.md
skills/implement-issue/SKILL.md
skills/kotlin-control-flow/SKILL.md
skills/kotlin-coroutines-structured-concurrency/SKILL.md
skills/kotlin-flow-state-event-modeling/SKILL.md
skills/kotlin-multiplatform-expect-actual/SKILL.md
skills/kotlin-types-value-class/SKILL.md
skills/shepherd/SKILL.md
skills/using-chrisbanes-skills/SKILL.md

Metadata

Files
0
Version
2026.7.21
Hash
83a9a523
Indexed
2026-07-24 12:25

Accueil - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-07 07:19
浙ICP备14020137号-1 $Carte des visiteurs$