Skip to content

/standards-kotlin

Kotlin coding standards for modern applications. Includes naming conventions, coroutines, flows, modern Kotlin 2.3.0 features, and recommended tooling.

From plugin
5711 skills12 commands1 hooks
shell
$ npx -y skills add b33eep/claude-code-setup --skill standards-kotlin --agent claude-code

How it fires

How this skill gets triggered: by you, by Claude, or both.

  • Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.
  • You can call itInvoke it directly when you want it.
  • Slash command/standards-kotlin
How auto-invocation works

Context preview

The summary Claude sees to decide when to auto-load this skill.

Kotlin coding standards for modern applications. Includes naming conventions, coroutines, flows, modern Kotlin 2.3.0 features, and recommended tooling.

SKILL.md

standards-kotlin.SKILL.md
name: standards-kotlin
description: Kotlin coding standards for modern applications. Includes naming conventions, coroutines, flows, modern Kotlin 2.3.0 features, and recommended tooling.
type: context
applies_to: [kotlin, gradle, maven, junit, kotest, kotlinx-coroutines, ktor, spring, mockk, testcontainers]
file_extensions: [".kt", ".kts"]

Kotlin Coding Standards

Core Principles

1. **Explicitness**: Explicit code over implicit magic 2. **Readability**: Readable code over clever tricks 3. **Null Safety**: Embrace Kotlin's null safety system 4. **Immutability**: Prefer `val` over `var`, immutable collections 5. **Expressiveness**: Use Kotlin's expressive features (data classes, sealed classes) 6. **DRY**: Don't Repeat Yourself - but keep it simple

General Rules

  • **Prefer `val` over `var`**: Immutability by default
  • **Use data classes**: For simple data holders
  • **Sealed classes/interfaces**: For type-safe state modeling
  • **Early returns**: Avoid deep nesting
  • **Descriptive names**: Clear, meaningful names
  • **Minimal changes**: Only change relevant code
  • **No over-engineering**: Keep it simple
  • **Minimal comments**: Self-explanatory code. Comments for "why", not "what"

Naming Conventions

| Element | Convention | Example | |---------|------------|---------| | Classes | PascalCase | `UserService`, `OrderRepository` | | Interfaces | PascalCase | `UserRepository`, `PaymentProcessor` | | Functions | camelCase | `getUserById`, `calculateTotal` | | Properties | camelCase | `firstName`, `totalAmount` | | Constants | UPPER_SNAKE_CASE | `MAX_RETRY_COUNT`, `DEFAULT_TIMEOUT` | | Packages | lowercase.dot.separated | `com.example.service`, `com.example.repository` | | Files | PascalCase.kt | `UserService.kt`, `OrderRepository.kt` | | Test Classes | ClassNameTest | `UserServiceTest`, `OrderRepositoryTest` | | Test Functions | backtick names | `` `should return user when id exists` `` |

Project Structure

Gradle Kotlin DSL Project (Recommended)

myproject/
├── build.gradle.kts
├── settings.gradle.kts
├── gradle.properties
├── src/
│   ├── main/
│   │   ├── kotlin/
│   │   │   └── com/example/myapp/
│   │   │       ├── Application.kt          # Main entry point
│   │   │       ├── config/
│   │   │       │   └── AppConfig.kt        # Configuration
│   │   │       ├── domain/
│   │   │       │   └── User.kt             # Domain models
│   │   │       ├── repository/
│   │   │       │   └── UserRepository.kt   # Data access
│   │   │       ├── service/
│   │   │       │   └── UserService.kt      # Business logic
│   │   │       └── api/
│   │   │           └── UserController.kt   # REST endpoints
│   │   └── resources/
│   │       ├── application.conf
│   │       └── logback.xml
│   └── test/
│       ├── kotlin/
│       │   └── com/example/myapp/
│       │       ├── service/
│       │       │   └── UserServiceTest.kt
│       │       └── repository/
│       │           └── UserRepositoryTest.kt
│       └── resources/
│           └── application-test.conf
└── README.md

Maven Project (Alternative)

myproject/
├── pom.xml
├── src/
│   ├── main/
│   │   └── kotlin/...      # Same structure as Gradle
│   └── test/
│       └── kotlin/...      # Same structure as Gradle
└── README.md

Modern Kotlin Features

> **Recommended:** Use Kotlin 2.3.0 (latest LTS) for new projects with K2 compiler enabled by default.

K2 Compiler (Stable since 2.0)

The K2 compiler brings significant performance improvements and faster compilation times.

**Features:**

  • Faster compilation (up to 2x)
  • Better smart casts
  • Improved type inference
  • Unified architecture for all platforms

**Enabled by default in Kotlin 2.3.0** - no configuration needed.

Data Classes

Use data classes for immutable data holders.

// Data class - automatic equals, hashCode, toString, copy, componentN
data class User(
    val id: String,
    val name: String,
    val email: String,
    val age: Int
)

// Usage
val user = User("1", "John Doe", "john@example.com", 30)

// Copy with changes
val updatedUser = user.copy(age = 31)

// Destructuring
val (id, name, email, age) = user
println("User: $name ($email)")

Sealed Classes/Interfaces (Exhaustive When)

Use sealed classes for type-safe state modeling with exhaustive `when` expressions.

// Sealed interface for result types
sealed interface Result<out T> {
    data class Success<T>(val data: T) : Result<T>
    data class Error(val message: String, val cause: Throwable? = null) : Result<Nothing>
    data object Loading : Result<Nothing>
}

// Exhaustive when - compiler ensures all cases are handled
fun <T> handleResult(result: Result<T>) {
    when (result) {
        is Result.Success -> println("Success: ${result.data}")
        is Result.Error -> println("Error: ${result.message}")
        Result.Loading -> println("Loading...")
        // No else needed - compiler knows all cases
    }
}

// Usage
val result: Result<User> = Result.Success(user)
handleResult(result)

Inline Value Classes (Zero-Cost Wrappers)

Use inline value classes for type-safe wrappers without runtime overhead.

// Inline value class - no boxing overhead
@JvmInline
value class UserId(val value: String)

@JvmInline
value class Email(val value: String) {
    init {
        require(value.contains("@")) { "Invalid email" }
    }
}

// Usage - type-safe, no runtime cost
fun getUserById(id: UserId): User = TODO()
fun sendEmail(email: Email): Unit = TODO()

val userId = UserId("123")
val email = Email("user@example.com")

Context Receivers (Experimental, 2.2+)

Context receivers allow implicit parameters for cleaner DSLs.

**Enable with:**

// build.gradle.kts
kotlin {
    compilerOptions {
        freeCompilerArgs.add("-Xcontext-receivers")
    }
}

**Usage:**

interface Logger {
    fun log(message: String)
}

// Function with context receiver
context(Logger)
fun processUser(user: User) {
    log("Pr
Read more
Read it on GitHub ↗

Showing the first part of this file.

Ships withclaude-code-setup

Persistent memory for Claude Code via Markdown files. 📖 Read the Documentation for detailed guides, tutorials, and reference.

Get the whole plugin, auto-invoked
Stats
57
Stars
0
Views
6
Forks
Maintained
Maintenance
Shell
Language
MIT
License
2mo ago
Last commit
6mo ago
Created

Repo: b33eep/claude-code-setup