1---2name: kotlin-development3description: Kotlin development guidelines with best practices for clean code, naming conventions, function design, and data handling4metadata:5 source: https://skillmd.com/skills/bouclem/kotlin-development6---78# Kotlin Development Best Practices910## General Principles1112- Use English for all code and documentation13- Always declare the type of each variable and function14- Avoid the `any` type; create necessary types instead15- No blank lines within function bodies1617## Naming Conventions1819### Case Standards20- **PascalCase**: Classes, interfaces, enums21- **camelCase**: Variables, functions, methods, parameters22- **UPPERCASE**: Environment variables, constants23- **underscores_case**: Files and directories2425### Naming Guidelines26- Start each function with a verb (get, set, create, update, delete, etc.)27- Use complete words over abbreviations28 - Allowed abbreviations: API, URL, i/j for loops, err, ctx, req/res/next29- Avoid magic numbers; define constants instead30- Boolean variables use prefixes: `isLoading`, `hasError`, `canDelete`3132## Function Design3334### Size and Responsibility35- Keep functions under 20 instructions36- Each function should have a single responsibility37- Extract logic into utility functions when complexity increases3839### Naming Functions40- Use verb-based naming that describes the action41- Prefix boolean-returning functions with `is`, `has`, or `can`42- Prefix void functions with action verbs like `execute`, `save`, `send`4344### Reducing Nesting45- Use early returns to handle edge cases first46- Extract nested logic into separate functions47- Employ higher-order functions (map, filter, reduce) to minimize loops48- Prefer arrow functions for simple operations; use named functions otherwise4950### Parameters51- Use default parameters instead of null checks52- When functions have multiple parameters, consolidate them into objects (RO-RO pattern)53- Keep parameter lists short (max 3-4 parameters)5455## Data Handling5657### Data Classes58- Use data classes for data structures59- Encapsulate primitive types in composite types when they represent domain concepts60- Validate data internally within data classes6162### Immutability63- Prefer immutability for data64- Use `val` for variables that don't change65- Use immutable collections when possible66- Create new instances instead of mutating existing ones6768## Classes and Objects6970### Size Guidelines71- Keep classes under 200 instructions72- Limit to fewer than 10 public methods73- Limit to fewer than 10 properties7475### Design Principles76- Follow SOLID principles77- Favor composition over inheritance78- Use interfaces for abstraction79- Keep classes focused on a single responsibility8081## Exception Handling8283### When to Use Exceptions84- Reserve exceptions for truly unexpected errors85- Don't use exceptions for control flow86- Catch exceptions only to:87 - Fix an anticipated issue88 - Add context before re-throwing8990### Best Practices91- Create custom exception types for domain-specific errors92- Include meaningful error messages93- Log exceptions at appropriate levels9495## Testing9697### Unit Tests98- Follow Arrange-Act-Assert convention99- Write unit tests for all public functions100- Use test doubles (mocks, stubs, fakes) for dependencies101- Name tests descriptively to document behavior102103### Acceptance Tests104- Follow Given-When-Then convention105- Test user-facing behavior and scenarios106- Keep tests independent and repeatable107108## Code Organization109110- Group related functionality together111- Keep files focused and cohesive112- Use packages/modules to organize code by feature or layer113- Maintain consistent file structure across the project