Kotlin Coding Best Practices for Spring Boot Development
Add this skill
npx mdskills install PatrickJS/cursor-kotlin-springboot-best-practicesComprehensive Kotlin/Spring Boot ruleset with clear, actionable guidance and practical examples
val by default, and only use var when mutation is necessary to promote safer, more predictable code.
val maxConnections = 10 // immutable reference
var currentUsers = 0 // mutable, try to avoid if possible
equals() and copy() without writing boilerplate code.// Kotlin – use default parameters
fun createConnection(host: String, secure: Boolean = true) { … }
createConnection("example.com") // uses default secure=true
createConnection(host = "test.com", secure = false) // named arg for clarity
when expressions instead of long if-else chains to write cleaner, more readable conditional logic that clearly handles each case.fun String.capitalizeFirst(): String = replaceFirstChar { it.uppercaseChar() }
println("kotlin".capitalizeFirst()) // prints "Kotlin"
apply, let, also, run, and with to reduce repetition and clearly express object configuration or null-safe operations.?.) and the Elvis operator (?:) to avoid runtime crashes.!!) and instead provide fallback values or explicit null checks to write safer and more predictable code.String or String? to avoid spreading nullability uncertainty in your Kotlin code.filter, map, and forEach instead of manual loops to write concise and expressive data transformation logic.
// Imperative approach
val activeUsers = mutableListOf()
for (user in users) {
if (user.isActive) activeUsers.add(user)
}
// Idiomatic functional approach
val activeUsers = users.filter { it.isActive }
fun toDto(entity: User) = UserDto(name = entity.name, email = entity.email)
$var or ${expression}) instead of concatenation, and use triple-quoted strings for clean multi-line text.val to keep them immutable and to align with Spring and Kotlin idioms.
@Service
class OrderService(
private val orderRepo: OrderRepository,
private val notifier: Notifier
) {
// ...
}
final by default, and let Spring’s 'all-open' plugin handle proxy generation so you don’t need to manually add the open modifier.object declaration for true singletons or stateless utility holders instead of static methods or Java-style singletons.when expressions.
sealed class Result
data class Success(val data: T): Result()
data class Error(val exception: Throwable): Result()
use the use function to safely manage and close resources like streams and file handles, ensuring they are closed even if an exception occurs.
FileInputStream("data.txt").use { stream ->
// read from stream
} // stream is automatically closed here
private or internal where possible, and only expose what’s truly necessary as public.launch or async to write clean, asynchronous backend code without callback hell.lazy, observable, infix, and operator overloading to write concise, expressive, and idiomatic code.val fields and Kotlin’s JPA plugin to satisfy JPA requirements while keeping your models safe and thread-friendly.Install via CLI
npx mdskills install PatrickJS/cursor-kotlin-springboot-best-practicesKotlin Spring Boot Best Practices is a free, open-source AI agent skill. Kotlin Coding Best Practices for Spring Boot Development
Install Kotlin Spring Boot Best Practices with a single command:
npx mdskills install PatrickJS/cursor-kotlin-springboot-best-practicesThis downloads the skill files into your project and your AI agent picks them up automatically.
Kotlin Spring Boot Best Practices works with Cursor. Skills use the open SKILL.md format which is compatible with any AI coding agent that reads markdown instructions.