kotlin-rules

Kotlin rules: coding guidelines for Kotlin covering style, design patterns, security, and testing. Trigger conditions include .kt, .kts, build.gradle.kts files or Ktor, Jetpack Compose, coroutines, and kotlinx-related projects.

Last repository push
GitHub Stars
180
Forks
21
Issues / PRs
0

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "kotlin-rules" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-057e4927bc443d6566d824e07e70d812385b870d4ddc902cdcb950ad9be3f419. Verify the upstream source and version against https://github.com/softspark/ai-toolkit before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

github.com

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

Kotlin rules: coding guidelines for Kotlin covering style, design patterns, security, and testing. Trigger conditions include .kt, .kts, build.gradle.kts files or Ktor, Jetpack Compose, coroutines, and kotlinx-related projects.

0 Downloads

Introduction source: third_party · softspark · 0.0.1 Reference

SkillPi interpretation

Tasks
Kotlin coding rules: style, patterns, security, testing. Triggers: .kt, .kts, build.gradle.kts, Ktor, Jetpack Compose, coroutines, kotlinx.
Inputs and outputs
Validate all inputs at API boundaries using Bean Validation or manual checks. Use `require()` for argument validation: `require(age > 0) { "Age must be positive" }`. Use data class `init` blocks for domain validation on construction. Never trust client-provided IDs. Verify resource ownership server-side. Sanitize strings before using in HTML, SQL, or shell commands.
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: kotlin-rules
description: "Kotlin coding rules: style, patterns, security, testing. Triggers: .kt, .kts, build.gradle.kts, Ktor, Jetpack Compose, coroutines, kotlinx."
effort: medium
user-invocable: false
allowed-tools: Read

Kotlin Rules

These rules come from app/rules/kotlin/ in ai-toolkit. They cover the project's standards for coding style, frameworks, patterns, security, and testing in Kotlin. Apply them when writing or reviewing Kotlin code.

Kotlin Coding Style

Naming

  • PascalCase: classes, interfaces, objects, type aliases, enum entries.
  • camelCase: functions, properties, local variables, parameters.
  • UPPER_SNAKE: compile-time constants (const val), top-level val constants.
  • Backing properties: prefix with _ (private val _items, val items: List<T>).
  • Package names: lowercase, no underscores (com.company.project.feature).

Null Safety

  • Use nullable types only when nullability is semantically meaningful.
  • Prefer ?.let { }, ?: (Elvis), and safe calls over !!.
  • Never use !! except in tests or when null is truly impossible.
  • Use requireNotNull() and require() for preconditions at public API boundaries.
  • Use checkNotNull() and check() for state assertions.

Data Classes

  • Use data class for DTOs, value objects, and state containers.
  • Use copy() for immutable updates. Avoid mutable var in data classes.
  • Use sealed class / sealed interface for restricted hierarchies.
  • Use value class (inline class) for type-safe wrappers with zero overhead.
  • Use object for singletons and namespace-like utility groupings.

Functions

  • Use expression body (= expr) for single-expression functions.
  • Use named arguments for functions with >2 parameters of the same type.
  • Use default parameter values instead of overloaded functions.
  • Use extension functions to add behavior without inheritance.
  • Use suspend functions for async operations, not callbacks.

Collections

  • Prefer listOf, mapOf, setOf (immutable) over mutableListOf.
  • Use collection operators: map, filter, groupBy, associate.
  • Use sequence {} for lazy evaluation on large collections.
  • Prefer firstOrNull() over first() for safe access.
  • Use destructuring: val (name, age) = user.

Scope Functions

  • let: null-safe chaining and local scoping.
  • apply: configure object after creation.
  • also: side effects (logging, validation) in chains.
  • run: compute a result using receiver's context.
  • with: multiple operations on an object without chaining.
  • Avoid nesting scope functions more than 1 level deep.

Formatting

  • Use ktlint or detekt for automated formatting and linting.
  • Use trailing commas in multi-line parameter/argument lists.
  • Max line length: 120 characters (Kotlin convention).
  • Use when expression over if-else chains for 3+ branches.

Kotlin Frameworks

Ktor (Server)

  • Use routing DSL: routing { get("/users") { call.respond(users) } }.
  • Use install() for plugins: ContentNegotiation, Authentication, CORS.
  • Use call.receive<T>() for typed request body parsing with kotlinx.serialization.
  • Use StatusPages plugin for centralized error handling.
  • Use Routing with nested route("/api/v1") { } blocks for URL grouping.

Ktor (Client)

  • Use HttpClient with engine configuration (CIO, OkHttp, Apache).
  • Use install(ContentNegotiation) { json() } for JSON serialization.
  • Use client.get<T>() with reified type for typed responses.
  • Use HttpTimeout plugin for connection and request timeouts.
  • Close HttpClient when done or use DI lifecycle management.

Spring Boot (Kotlin)

  • Use constructor injection (Kotlin classes are final by default).
  • Apply kotlin-spring plugin for open classes (required for proxying).
  • Use @ConfigurationProperties with data classes for typed config.
  • Use WebFlux with coroutines: coRouter { } and suspend handler functions.
  • Use spring-boot-starter-validation with @Valid on Kotlin data classes.

Exposed (ORM)

  • Use DSL API for type-safe queries: Users.select { Users.name eq "Ada" }.
  • Use DAO API for Active Record-style: User.find { Users.age greaterEq 18 }.
  • Wrap database operations in transaction { } blocks.
  • Use SchemaUtils.create(Users) for schema management in development.

kotlinx.serialization

  • Use @Serializable annotation on data classes for compile-time serialization.
  • Use @SerialName("field_name") for JSON field name mapping.
  • Use Json { ignoreUnknownKeys = true } for lenient deserialization.
  • Use polymorphic serialization with sealed class and @Polymorphic.
  • Prefer kotlinx.serialization over Jackson for pure Kotlin projects.

Koin (DI)

  • Define modules: module { single { UserService(get()) } }.
  • Use by inject<T>() for lazy injection in Android/Ktor.
  • Use factory { } for new instance per injection, single { } for singleton.
  • Use checkModules() in tests to verify DI graph completeness.

Compose (Multiplatform UI)

  • Use @Composable functions for UI components. Keep them stateless.
  • Use remember { } and mutableStateOf() for local state.
  • Hoist state to callers: pass state down, events up.
  • Use LaunchedEffect for side effects tied to composition lifecycle.
  • Use ViewModel with StateFlow for screen-level state management.

Kotlin Patterns

Error Handling

  • Use Result<T> for operations that can fail without exceptions.
  • Use runCatching { } to wrap exception-throwing code into Result.
  • Use sealed class hierarchies for domain errors: sealed class AppError.
  • Prefer fold(), getOrElse(), getOrNull() over getOrThrow().
  • Use require() / check() for preconditions; they throw IllegalArgumentException / IllegalStateException.

Coroutines

  • Use suspend functions for sequential async operations.
  • Use coroutineScope { } for structured concurrency with parallel work.
  • Use async { } + await() for concurrent independent operations.
  • Use supervisorScope { } when child failures should not cancel siblings.
  • Use withContext(Dispatchers.IO) for blocking I/O in coroutine context.
  • Use flow { } for cold asynchronous streams. Collect in lifecycle-aware scope.

Flow Patterns

  • Use stateIn() and shareIn() to convert cold flows to hot shared state.
  • Use combine() to merge multiple flows into derived state.
  • Use flatMapLatest for search-as-you-type patterns (cancel previous).
  • Use catch { } operator for upstream error handling in flows.
  • Use flowOn(Dispatchers.IO) to shift upstream execution context.

Sealed Hierarchies

  • Use sealed interface over sealed class when no shared state is needed.
  • Use when expressions exhaustively on sealed types (compiler-enforced).
  • Combine sealed types with data classes for typed state machines.
  • Use sealed hierarchies for API responses: Success<T>, Error, Loading.

Delegation

  • Use by lazy { } for thread-safe lazy initialization.
  • Use by map for delegated properties backed by a Map.
  • Use class delegation (class Foo : Bar by impl) to favor composition.
  • Use observable / vetoable delegates for reactive property changes.

Builder Patterns

  • Use DSL-style builders with @DslMarker annotation to prevent scope leakage.
  • Use trailing lambda syntax for configuration blocks.
  • Use apply { } for inline object configuration without a dedicated builder.
  • Use buildList { }, buildMap { }, buildString { } for collection construction.

Anti-Patterns

  • Overusing !!: masks null-safety guarantees. Use safe calls or require.
  • Nesting scope functions: foo.let { it.also { ... }.run { } } -- flatten logic.
  • Blocking the main thread: use withContext(Dispatchers.IO) for I/O.
  • Using GlobalScope.launch: leaks coroutines. Use structured concurrency.
  • Mutable shared state without synchronization: use Mutex or StateFlow.

Kotlin Security

Input Validation

  • Validate all inputs at API boundaries using Bean Validation or manual checks.
  • Use require() for argument validation: require(age > 0) { "Age must be positive" }.
  • Use data class init blocks for domain validation on construction.
  • Never trust client-provided IDs. Verify resource ownership server-side.
  • Sanitize strings before using in HTML, SQL, or shell commands.

Null Safety as Security

  • Kotlin's null safety prevents null pointer exceptions. Do not circumvent with !!.
  • Use ?. and ?: chains for safe fallback values at boundaries.
  • Treat Java interop as untrusted: platform types can still be null.
  • Use @Nullable / @NotNull annotations on Java code consumed by Kotlin.

SQL Injection

  • Use Exposed DSL or JPA with parameterized queries. Never concatenate input.
  • Use PreparedStatement if writing raw JDBC.
  • Use CriteriaBuilder or Exposed conditions for dynamic query construction.
  • Audit @Query(nativeQuery = true) for parameter interpolation risks.

Serialization

  • Use kotlinx.serialization with @Serializable for compile-time safety.
  • Use Json { ignoreUnknownKeys = true } but validate after deserialization.
  • Never use Java ObjectInputStream for deserialization (RCE risk).
  • Restrict polymorphic deserialization to known sealed class subtypes.

Authentication

  • Use Spring Security or Ktor Authentication plugin. Do not roll your own.
  • Hash passwords with BCrypt or Argon2. Never store plaintext.
  • Use short-lived JWTs (15 min) with refresh token rotation.
  • Validate JWT signature, issuer, audience, and expiration on every request.

Coroutine Security

  • Use withTimeout() to prevent unbounded coroutine execution (DoS vector).
  • Use Mutex for critical sections. Do not use synchronized in suspend functions.
  • Propagate security context through CoroutineContext elements.
  • Cancel coroutine scopes on authentication failure or session expiry.

Secrets Management

  • Use environment variables or Vault for secrets. Never hardcode.
  • Use @ConfigurationProperties with injected secrets, not string literals.
  • Never log request headers containing Authorization tokens.
  • Use separate configuration profiles for dev/staging/prod secrets.

Dependencies

  • Use Dependabot or Renovate for automated dependency updates.
  • Run OWASP Dependency-Check or Gradle dependencyCheckAnalyze.
  • Audit transitive dependencies with gradle dependencies.
  • Pin dependency versions. Avoid dynamic versions like 1.+.

Logging

  • Use parameterized logging: logger.info("User {} logged in", userId).
  • Never log passwords, tokens, or PII.
  • Sanitize user input before logging to prevent log injection.
  • Use structured logging (JSON) for machine-parseable audit trails.

Kotlin Testing

Framework

  • Use JUnit 5 as the test runner.
  • Use Kotest for Kotlin-idiomatic BDD-style testing (alternative).
  • Use MockK for mocking (Kotlin-native, supports coroutines).
  • Use Testcontainers for integration tests with external services.

File Naming

  • Test files: FooTest.kt in src/test/kotlin/ mirroring source package.
  • Integration tests: FooIT.kt or use @Tag("integration").
  • Use @Nested inner classes to group related test cases.

Structure

  • Use @DisplayName for human-readable test names.
  • Use backtick function names for readable test names: `returns 404 when user not found`.
  • Use @BeforeEach for per-test setup. Avoid shared mutable state.
  • Use @ParameterizedTest with @MethodSource for table-driven tests.

MockK

  • Use mockk<UserRepository>() to create mocks.
  • Use every { mock.find(any()) } returns user for stubbing.
  • Use coEvery { ... } and coVerify { ... } for coroutine mocking.
  • Use spyk() for partial mocks on real objects.
  • Use slot<T>() and captured to inspect arguments.
  • Clear mocks in @AfterEach to prevent state leakage.

Coroutine Testing

  • Use runTest { } from kotlinx-coroutines-test for coroutine tests.
  • Use TestDispatcher to control coroutine execution timing.
  • Use advanceUntilIdle() to complete all pending coroutines.
  • Use turbine library for testing Flow emissions.

Assertions

  • Use AssertJ or Kotest assertions for fluent, readable checks.
  • Use shouldBe, shouldThrow, shouldContain (Kotest matchers).
  • Use assertSoftly { } to collect multiple assertion failures.
  • Use assertThrows<FooException> { ... } for exception testing.

Test Data

  • Use factory functions for test data: fun aUser(name: String = "Ada") = User(...).
  • Use default parameters for minimal test data setup.
  • Use copy() on data classes for variations of base test objects.
  • Use faker library for realistic test data generation.

Spring Integration

  • Use @SpringBootTest with @AutoConfigureMockMvc for API tests.
  • Use @WebMvcTest for controller-only tests (faster).
  • Use @MockkBean instead of @MockBean for MockK integration.
  • Use @Transactional on test classes for automatic rollback.

Version history

  1. v6.2.0 GitHub ↗
    Original release notes

    **Full Changelog**: https://github.com/softspark/ai-toolkit/compare/v6.1.0...v6.2.0

  2. v6.1.0 GitHub ↗
    Original release notes

    **Full Changelog**: https://github.com/softspark/ai-toolkit/compare/v6.0.0...v6.1.0

  3. v6.0.0 GitHub ↗
    Original release notes

    ## What's Changed * chore: bump softprops/action-gh-release from 3.0.3 to 3.0.4 by @dependabot[bot] in https://github.com/softspark/ai-toolkit/pull/45 * fix: handle Windows status output and clarify Antigravity workflow support by @softspark in https://github.com/softspark/ai-toolkit/pull/46 **Full Changelog**: https://github.com/softspark/ai-toolkit/compare/v5.5.0...v6.0.0

  4. v5.5.0 GitHub ↗
    Original release notes

    ### Ecosystem - **OpenCode V2 and V1 compatibility:** `scripts/generate_opencode_plugin.py` emits the documented `id/setup/server` contract for V2 and V1 **1.18.29+**. Upgrade older V1 clients before using this plugin, then run `ai-toolkit update` to regenerate managed copies. `doctor` warns about obsolete V1-only exports. Scoped global/project IDs avoid native collisions; advisory work has one owner per location, while safety guards execute in every registered layer. The current V2 package is `@opencode/cli`; `opencode-ai` still publishes V1. - **Claude guard failure policy:** destructive-command, home-path and configuration guards use `onFailure: block` on Claude Code **2.1.295+**. Legacy entries migrate without duplication; hooks owned by other plugins survive. Older clients do not have this infrastructure-failure guarantee. - **Codex, Gemini and Devin:** Codex refresh accepts the two diagnostic model-request events. Gemini advisory hooks return native JSON and save native completion responses; Devin prompt/edit hooks emit event-labelled context. - **All 13 integrations reviewed:** refresh release metadata and the ecosystem snapshot, distinguish stable/preview channels, and pin Auggie's stable v0 references. User model, effort and approval selections remain unchanged. ### Fixed - **One Claude Stop checker:** remove the duplicate advisory handler from the global and app manifests. `quality-gate.sh` keeps PHPStan advisory through `quality-check.sh --php-only`, preserving checker precedence and Makefile settings. PHPStan now runs synchronously on Stop and TaskCompleted (#43). - **OpenCode guard execution:** replace the unsupported Bun stdin path with real subprocess execution in both APIs. Denial, missing guards, crashes and timeouts block tools. Transactional generation refuses user-owned collisions and symlinked destinations; lifecycle cleanup releases advisory ownership. - **Isolated release smoke:** `scripts/release.sh` installs the packed artifact in a disposable, network-disabled container with no mounts and the OS user's default home. Host settings are not used as a smoke sandbox. ### Changed - Hook entries: 29 to 28 across the same 14 Claude events. Skills remain 116 and agents remain 44. Bats coverage grows from 2012 to 2037 tests. - Refresh compatibility documentation and remove stale inventory counts from the backward-compatibility policy. All protected public surfaces remain. - Skill body budget is unchanged: the largest body remains `design-engineering` at 17,197 bytes, less than 2,000 bytes below the 18,000-byte warning threshold, so the release ratchet does not apply. **Included pull request:** [#43: run the project checker once on Claude Code Stop](https://github.com/softspark/ai-toolkit/pull/43), contributed by @jacekprzybyl. **Full changelog:** https://github.com/softspark/ai-toolkit/compare/v5.4.2...v5.5.0

  5. v5.4.2 GitHub ↗
    Original release notes

    ## What's Changed * fix(install): read state.json on native Windows by @jacekprzybyl in https://github.com/softspark/ai-toolkit/pull/39 * test: isolate USERPROFILE together with HOME on Windows by @jacekprzybyl in https://github.com/softspark/ai-toolkit/pull/41 **Full Changelog**: https://github.com/softspark/ai-toolkit/compare/v5.4.1...v5.4.2

Only the five most recent versions are shown. View full version history at the source ↗

Current listing version

0.0.1

Verify the source revision ↗

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value95/100

Highlights

  • 提供全面的Kotlin编码规范,涵盖风格、框架、安全、测试等多个维度
  • 内容详实,对Kotlin开发者具有极高的实用价值和指导意义
Trigger Precision90/100

Highlights

  • 触发词精准完整,覆盖.kt、.kts、build.gradle.kts、Ktor、Jetpack Compose、协程、kotlinx等多种变体
  • 边界清晰,不易误触或漏触
Structure and Standards90/100

Highlights

  • 结构清晰,分章节详细,有明确的章节划分和出口条件
  • frontmatter完整,单文件未超限,易于理解执行流程
Execution Reliability95/100

Highlights

  • 内容来源明确,参数正确,覆盖主要异常情况
  • 无不可逆操作风险,执行可靠性高
Evidence Verifiability (mean of the first four)93/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

These common operations were not identified in the main document. This does not mean no permissions or risks exist.

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.