Best practices for Kotlin Coroutines Usage
.cursorrules in your project root# Kotlin Coroutines Usage Rules ## Core Principles * **Asynchronous programming**: Use coroutines for asynchronous programming to avoid blocking threads and improve responsiveness. * **Non-blocking**: Ensure that coroutines are non-blocking to prevent thread starvation and improve overall system performance. * **Context-aware**: Be aware of the coroutine context and use the appropriate dispatcher to execute coroutines. ## Code Style Guidelines ### Naming * **Coroutine scope**: Use a clear and descriptive name for the coroutine scope, such as `viewModelScope` or `lifecycleScope`. * **Coroutine function**: Prefix coroutine functions with `async` or `launch` to indicate their asynchronous nature. * **Coroutine variable**: Use a clear and descriptive name for coroutine variables, such as `job` or `deferred`. ### Typing * **Use suspend functions**: Use suspend functions instead of blocking functions to ensure non-blocking behavior. * **Specify coroutine context**: Specify the coroutine context using the `Dispatcher` type, such as `Dispatchers.IO` or `Dispatchers.Main`. ## Best Practices ### Coroutine Scope * **Use a single scope**: Use a single coroutine scope per component, such as a ViewModel or Fragment. * **Cancel scope on destroy**: Cancel the coroutine scope when the component is destroyed to prevent memory leaks. ### Coroutine Function * **Use async for computations**: Use `async` for computations that can be executed concurrently. * **Use launch for actions**: Use `launch` for actions that have side effects, such as updating the UI. * **Handle exceptions**: Handle exceptions in coroutine functions using `try-catch` blocks. ### Coroutine Variable * **Use Job to manage coroutines**: Use `Job` to manage coroutines and cancel them when necessary. * **Use Deferred to await results**: Use `Deferred` to await results from coroutine functions. ## Common Pitfalls to Avoid ### Blocking Calls * **Avoid blocking calls**: Avoid making blocking calls, such as `Thread.sleep()` or `wait()`, in coroutine functions. * **Use suspend functions instead**: Use suspend functions instead of blocking functions to ensure non-blocking behavior. ### Unhandled Exceptions * **Handle exceptions**: Handle exceptions in coroutine functions using `try-catch` blocks. * **Use CoroutineExceptionHandler**: Use `CoroutineExceptionHandler` to handle unhandled exceptions in coroutine scopes. ### Memory Leaks * **Cancel scope on destroy**: Cancel the coroutine scope when the component is destroyed to prevent memory leaks. * **Use weak references**: Use weak references to prevent memory leaks when storing coroutine variables.
Comprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.
Cursor
coding
AI coding rules customize how Cursor generates and refactors code for your project. Follow these steps to install Kotlin Coroutines Usage.
.cursor/rules, for Windsurf use .windsurfrulesComprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.