Rule
0 installs · 0 said it worked
Cursor rules for writing idiomatic Kotlin Spring Boot backend code
A Cursor rules file giving the assistant Kotlin and Spring Boot best practices covering project structure, naming, idiomatic Kotlin usage, and implementation patterns for backend code.
People use this for
- kotlin spring boot cursor rules
- cursor rules for kotlin backend
- idiomatic kotlin spring boot code style
- spring boot project structure conventions
- kotlin naming conventions for spring apps
- Name
cursor-rules-for-writing-idiomatic-kotlin-spring-boot-backend-co- Works with
- Cursor
- License
- CC0(as observed at the source)
- Version
- v1 · updated Nov 29, 2026
Imported on Nov 29, 2026 from github.com . License observed at the source on Nov 29, 2026: CC0.
Are you the author?Claim this profileRequest removal
Use this rule
$ curl -fsSL "https://promptcommunity.dev/api/v1/posts/cursor-rules-for-writing-idiomatic-kotlin-spring-boot-backend-code-gzvr/export/mdc" -o .cursor/rules/cursor-rules-for-writing-idiomatic-kotlin-spring-boot-backend-co.mdcThen open a new session; Cursor picks it up from .cursor/rules/.
Manual install
Save cursor-rules-for-writing-idiomatic-kotlin-spring-boot-backend-co.mdc as .cursor/rules/cursor-rules-for-writing-idiomatic-kotlin-spring-boot-backend-co.mdc for this project or ~/.cursor/rules/cursor-rules-for-writing-idiomatic-kotlin-spring-boot-backend-co.mdc for every project, then start a new session.
Rule
description: "Cursor rules for Kotlin Springboot Best Practices." globs: */ alwaysApply: false
Kotlin Coding Best Practices for Spring Boot Development
Project Structure and Organization
- Group your source code into clearly defined packages like controller, service, repository, and model to separate concerns and improve maintainability.
- Organize your file system so that each directory mirrors the Kotlin package name (e.g. put com.myapp.users under src/main/kotlin/com/myapp/users).
- Name each Kotlin file after the primary class or concept it contains to make the codebase easier to navigate and understand.
- Avoid vague file names like Utils.kt; instead, use concise and meaningful names that reflect the purpose of the file’s contents.
- Place your Spring Boot application entry point in the root package and structure sub-packages by layer or feature to help Spring scan and organize components efficiently.
Coding Style and Conventions
- Use PascalCase for class and object names, camelCase for functions and variables, and UPPER_SNAKE_CASE for constants to follow Kotlin naming conventions and improve readability.
- Declare variables using
valby default, and only usevarwhen mutation is necessary to promote safer, more predictable code.val maxConnections = 10 // immutable reference var currentUsers = 0 // mutable, try to avoid if possible - Limit the scope of variables to where they are actually used—inside functions or smaller blocks—to avoid accidental misuse and make code easier to follow.
- Format your code consistently using 4-space indentation, proper spacing around operators and commas, and short, focused functions to improve clarity and maintainability.
- Write clear and expressive code instead of clever one-liners; break complex logic into intermediate variables or well-named functions to improve readability.
- Name classes, functions, and variables descriptively to convey intent, and avoid vague suffixes like '-Manager' or '-Helper' that don’t add meaning.
- Keep property getters and setters simple and free of heavy logic; if complex behavior is needed, move it into a separate method to keep property access predictable.
Idiomatic Kotlin Usage
- Use data class to define DTOs and entities so you get useful methods like
equals()andcopy()without writing boilerplate code. - Replace overloaded constructors with default and named parameters to simplify function calls and make them more expressive.
// 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 - Use
whenexpressions instead of longif-elsechains to write cleaner, more readable conditional logic that clearly handles each case. - Create extension functions instead of utility classes to add reusable behavior to existing types in a more natural and readable way.
fun String.capitalizeFirst(): String = replaceFirstChar { it.uppercaseChar() } println("kotlin".capitalizeFirst()) // prints "Kotlin" - Use scope functions like
apply,let,also,run, andwithto reduce repetition and clearly express object configuration or null-safe operations. - Declare variables as nullable only when necessary, and handle them using safe-call operators (
?.) and the Elvis operator (?:) to avoid runtime crashes. - Avoid using the not-null assertion (
!!) and instead provide fallback values or explicit null checks to write safer and more predictable code. - Handle platform types from Java APIs immediately by explicitly casting them to
StringorString?to avoid spreading nullability uncertainty in your Kotlin code. - Use Kotlin’s functional collection operations like
filter,map, andforEachinstead of manual loops to write concise and expressive data transformation logic.// Imperative approach val activeUsers = mutableListOf<User>() for (user in users) { if (user.isActive) activeUsers.add(user) } // Idiomatic functional approach val activeUsers = users.filter { it.isActive } - Convert simple functions into single-expression functions when the logic is clear, to eliminate unnecessary syntax and improve code brevity.
fun toDto(entity: User) = UserDto(name = entity.name, email = entity.email) - Build strings using string templates (
$varor${expression}) instead of concatenation, and use triple-quoted strings for clean multi-line text.
Implementation Patterns and Design
- Inject dependencies via constructor parameters using
valto keep them immutable and to align with Spring and Kotlin idioms.@Service class OrderService( private val orderRepo: OrderRepository, private val notifier: Notifier ) { // ... } - Keep classes
finalby default, and let Spring’s 'all-open' plugin handle proxy generation so you don’t need to manually add the open modifier. - Use Kotlin’s
objectdeclaration for true singletons or stateless utility holders instead of static methods or Java-style singletons. - Favor composition by combining small, focused classes or using higher-order functions instead of relying on deep inheritance hierarchies.
- Define sealed classes when a type has a limited, closed set of variants to enforce exhaustive handling and improve type safety in
whenexpressions.sealed class Result<out T> data class Success<T>(val data: T): Result<T>() data class Error(val exception: Throwable): Result<Nothing>() - Use enum class to model fixed sets of constants that may contain logic, avoiding magic strings or raw values in business logic.
- Return nullable types, sealed classes, or result wrappers instead of throwing exceptions for expected scenarios like “not found” or “invalid input”.
- Always
usethe 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 - Minimize visibility of your components by using
privateorinternalwhere possible, and only expose what’s truly necessary as public. - Use Kotlin coroutines with suspend functions and coroutine builders like
launchorasyncto write clean, asynchronous backend code without callback hell. - Leverage Kotlin’s standard library features like
lazy,observable,infix, and operator overloading to write concise, expressive, and idiomatic code. - Use immutable data class entities with
valfields and Kotlin’s JPA plugin to satisfy JPA requirements while keeping your models safe and thread-friendly. - Write unit tests for your business logic using dependency injection and pure functions to make testing straightforward and independent from Spring’s context.
No result recorded yet. Added it? File a Tried-it report so the next person knows how it went in your editor.
Tried it
Reports from people who added this rule to their editor. They roll up into the badges above.
No one has filed a report yet. Try the prompt and be the first.
Related rules
- Explain, test, and harden a Python regular expression
Explains a Python regex token by token, tests it against must-match and must-not-match samples, flags risky patterns like catastrophic backtracking, and returns a safer rewrite with a test table.
Coding · @f
- Cursor rules enforcing clean, readable, maintainable code practices
A Cursor .mdc rule file that guides the assistant toward clean code: named constants, meaningful names, sparing comments, single-purpose functions, DRY, language style standards, extracted conditionals, continuous refactoring, and version control.
Coding · @patrickjs
- Python docstring coverage and drift checker for code review
A skill-style system prompt that makes the assistant measure Python docstring coverage via AST analysis, flag stale or missing docstrings, and plan a CI-gated documentation fix.
Coding · @f
- Claude api: reference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming,…
Reference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER — read BEFORE opening the target file; don't skip because it "looks like a one-liner" — whenever: the prompt names Claude/Anthropic in any fo
Coding · @anthropic
- Match new code to a project's existing style and conventions
A Cursor rules prompt that makes the model analyze existing codebase patterns (naming, formatting, architecture, testing) and adapt new code to fit the established style.
Coding · @patrickjs
License: CC0v1 · published Nov 29, 2026
Comments
No comments yet. If you tried this prompt, say how it went.