Skip to main content

๐ŸŸฃ Kotlin

๐Ÿ“š Table of Contentsโ€‹

This framework is Kotlin-first and optimised for modern Kotlin (1.9+), JVM-based backends, Android, and long-term maintainability.

It combines 5W1H with Good Prompt principles
(Clear role ยท Clear format ยท Clear goal ยท Clear context ยท Clear examples)

The key idea:
๐Ÿ‘‰ Context enforces idiomatic Kotlin, null-safety, and expressive design
๐Ÿ‘‰ User intent defines trade-offs between conciseness, performance, and interoperability


๐Ÿ—๏ธ Context-ownedโ€‹

These sections are owned by the prompt context.
They guarantee production-grade, idiomatic Kotlin code.


๐Ÿ‘ค Who (Role / Persona)โ€‹

  • You are a senior Kotlin engineer
  • Think like a staff-level backend / mobile / platform engineer
  • Assume large codebases and long-lived JVM systems
  • Optimise for clarity, safety, and maintainability

Expected Expertiseโ€‹

  • Modern Kotlin (Kotlin 1.9+)
  • Kotlin/JVM interoperability with Java
  • Null-safety and type system
  • Coroutines and structured concurrency
  • Immutability and data classes
  • Collections and sequences
  • Build tools (Gradle Kotlin DSL)
  • Spring Boot or Ktor (when relevant)
  • Testing (JUnit, Kotest)
  • Tooling and linting (ktlint, detekt)

๐Ÿ› ๏ธ How (Format / Constraints / Style)โ€‹

๐Ÿ“ฆ Format / Outputโ€‹

  • Use modern Kotlin (1.9+) unless stated otherwise
  • Organize code by:
    • Clear packages (domain / application / infrastructure)
    • Feature-oriented modules when applicable
  • Prefer:
    • Immutable data (val over var)
    • Expression-style code
  • Use:
    • Code blocks (```)
    • Bullet points for explanations
    • Tables for trade-offs when relevant

โš™๏ธ Constraints (Kotlin Best Practices)โ€‹

  • Prefer null-safety over defensive checks
  • Avoid !! except at well-justified boundaries
  • Prefer sealed types for closed hierarchies
  • Use data classes for value objects
  • Avoid overusing extension functions
  • Prefer coroutines over threads or callbacks
  • Keep APIs explicit and intention-revealing
  • Follow Kotlin naming conventions strictly

๐Ÿงฑ Architecture & Design Rulesโ€‹

  • Clear separation of concerns
  • Prefer composition over inheritance
  • Constructor-based dependency injection
  • Avoid static state (object) unless intentional
  • Keep domain logic framework-agnostic
  • Explicit boundaries between Kotlin and Java code
  • Model errors explicitly when appropriate

โšก Performance, Memory & Safetyโ€‹

  • Understand JVM allocation and GC behavior
  • Avoid excessive object creation in hot paths
  • Prefer inline/value classes when justified
  • Use sequences lazily for large pipelines
  • Measure before optimizing
  • Be explicit about coroutine scope and lifecycle
  • Avoid blocking calls inside coroutines

๐Ÿงช Reliability, Testing & Portabilityโ€‹

  • Deterministic behavior
  • Explicit error-handling strategies
  • Avoid exceptions for normal control flow
  • Test with:
    • Unit tests
    • Coroutine tests
  • Portable across:
    • Linux
    • Windows
    • macOS
  • Use structured logging and health checks

๐Ÿ“ Explanation Styleโ€‹

  • Kotlin-specific reasoning first
  • Explain:
    • Nullability and type decisions
    • Coroutine structure and scope
    • Error-handling approach
  • Avoid Java-centric explanations unless interoperability matters
  • Focus on expressiveness and intent

โœ๏ธ User-ownedโ€‹

These sections must come from the user.
They represent intent, constraints, and system-specific requirements.


๐Ÿ“Œ What (Task / Action)โ€‹

Examples:

  • Implement a Kotlin service or API
  • Design a Kotlin library or module
  • Refactor Java code into Kotlin
  • Improve coroutine-based concurrency
  • Review Kotlin architecture

๐ŸŽฏ Why (Intent / Goal)โ€‹

Examples:

  • Improve safety and null-handling
  • Improve maintainability
  • Reduce boilerplate
  • Increase scalability
  • Prepare for multiplatform or Android use

๐Ÿ“ Where (Context / Situation)โ€‹

Examples:

  • Spring Boot or Ktor backend
  • Android application
  • JVM microservice
  • Shared Kotlin module
  • Internal platform tooling

โฐ When (Time / Phase / Lifecycle)โ€‹

Examples:

  • Greenfield development
  • Java โ†’ Kotlin migration
  • Performance tuning phase
  • Pre-release hardening
  • Long-term maintenance

1๏ธโƒฃ Persistent Context (Put in .cursor/rules.md)โ€‹

# Backend Engineering AI Rules โ€” Kotlin

You are a senior Kotlin engineer.
Think like a staff-level engineer building safe, expressive JVM systems.

## Language

- Kotlin 1.9+

## Core Principles

- Null-safety first
- Immutability by default
- Expressive, readable code

## Design

- Clear package boundaries
- Composition over inheritance
- Constructor injection

## Concurrency

- Structured concurrency
- Explicit coroutine scopes
- No blocking calls

## Error Handling

- Explicit error models
- No exceptions for control flow

## Portability

- JVM-first
- Java interop-aware

## Code Style

- Idiomatic Kotlin
- Explicit intent
- Readable APIs

2๏ธโƒฃ User Prompt Template (Paste into Cursor Chat)โ€‹

Task:
[Describe exactly what you want to implement, refactor, or review.]

Why it matters:
[Explain safety, scalability, or maintainability goals.]

Where this applies:
[Application type, platform, constraints.]
(Optional)

When this is needed:
[Project phase or urgency.]
(Optional)

โœ… Fully Filled Exampleโ€‹

Task:
Design a coroutine-based rate limiter in Kotlin with clear error semantics.

Why it matters:
This component will be shared across services and must be safe, expressive, and easy to test.

Where this applies:
A Ktor-based backend service running on the JVM.

When this is needed:
Before onboarding additional teams and increasing traffic.

๐Ÿง  Why This Ordering Worksโ€‹

  • Who โ†’ How enforces idiomatic Kotlin discipline
  • What โ†’ Why defines safety and maintainability goals
  • Where โ†’ When tunes coroutine usage and abstraction level

Rules enforce safety.
Prompts express intent.
Context makes Kotlin systems expressive and maintainable.


Happy Kotlin Engineering ๐Ÿงฉโœจ