1---2name: kotlin-development3description: Kotlin development guidelines with best practices for clean code, naming conventions, function design, and data handling4metadata:5 maintainer: Mindrally6 source: https://skillmd.com/skills/mindrally/kotlin-development7 upstream: https://github.com/Mindrally/skills8---910# Kotlin Development Best Practices1112## General Principles1314- Use English for all code and documentation15- Always declare the type of each variable and function16- Avoid the `any` type; create necessary types instead17- No blank lines within function bodies1819## Naming Conventions2021### Case Standards22- **PascalCase**: Classes, interfaces, enums23- **camelCase**: Variables, functions, methods, parameters24- **UPPERCASE**: Environment variables, constants25- **underscores_case**: Files and directories2627### Naming Guidelines28- Start each function with a verb (get, set, create, update, delete, etc.)29- Use complete words over abbreviations30 - Allowed abbreviations: API, URL, i/j for loops, err, ctx, req/res/next31- Avoid magic numbers; define constants instead32- Boolean variables use prefixes: `isLoading`, `hasError`, `canDelete`3334## Function Design3536### Size and Responsibility37- Keep functions under 20 instructions38- Each function should have a single responsibility39- Extract logic into utility functions when complexity increases4041### Naming Functions42- Use verb-based naming that describes the action43- Prefix boolean-returning functions with `is`, `has`, or `can`44- Prefix void functions with action verbs like `execute`, `save`, `send`4546### Reducing Nesting47- Use early returns to handle edge cases first48- Extract nested logic into separate functions49- Employ higher-order functions (map, filter, reduce) to minimize loops50- Prefer arrow functions for simple operations; use named functions otherwise5152### Parameters53- Use default parameters instead of null checks54- When functions have multiple parameters, consolidate them into objects (RO-RO pattern)55- Keep parameter lists short (max 3-4 parameters)5657## Data Handling5859### Data Classes60- Use data classes for data structures61- Encapsulate primitive types in composite types when they represent domain concepts62- Validate data internally within data classes6364### Immutability65- Prefer immutability for data66- Use `val` for variables that don't change67- Use immutable collections when possible68- Create new instances instead of mutating existing ones6970## Classes and Objects7172### Size Guidelines73- Keep classes under 200 instructions74- Limit to fewer than 10 public methods75- Limit to fewer than 10 properties7677### Design Principles78- Follow SOLID principles79- Favor composition over inheritance80- Use interfaces for abstraction81- Keep classes focused on a single responsibility8283## Exception Handling8485### When to Use Exceptions86- Reserve exceptions for truly unexpected errors87- Don't use exceptions for control flow88- Catch exceptions only to:89 - Fix an anticipated issue90 - Add context before re-throwing9192### Best Practices93- Create custom exception types for domain-specific errors94- Include meaningful error messages95- Log exceptions at appropriate levels9697## Testing9899### Unit Tests100- Follow Arrange-Act-Assert convention101- Write unit tests for all public functions102- Use test doubles (mocks, stubs, fakes) for dependencies103- Name tests descriptively to document behavior104105### Acceptance Tests106- Follow Given-When-Then convention107- Test user-facing behavior and scenarios108- Keep tests independent and repeatable109110## Code Organization111112- Group related functionality together113- Keep files focused and cohesive114- Use packages/modules to organize code by feature or layer115- Maintain consistent file structure across the project