|
9 | 9 | - **Naming**: Classes = PascalCase, Functions/Variables = camelCase |
10 | 10 | - **Architecture**: MVVM pattern with UI, State (Model), and Data layers |
11 | 11 | - **State Management**: Kotlin Flows (StateFlow for UI state) |
| 12 | +- **Visibility Modifiers**: |
| 13 | + - Use the most restrictive visibility modifier possible |
| 14 | + - Prefer `internal` over `public` for implementation classes |
| 15 | + - Use `private` for all properties and functions that don't need wider visibility |
| 16 | + - Use `public` only for APIs that need to be accessed outside the module |
12 | 17 | - **DI**: Koin for dependency injection |
| 18 | + - Use constructor injection whenever possible instead of property injection |
| 19 | + - Constructor parameters make dependencies explicit and improve testability |
13 | 20 | - **Imports**: Grouped by purpose, explicit imports preferred over wildcards |
14 | 21 | - **Error Handling**: Repository pattern for data operations, proper error propagation through Flows |
| 22 | +- **Expressive Kotlin Features**: |
| 23 | + - Use `runCatching` instead of try-catch blocks |
| 24 | + - Use `.use` extension for closeables |
| 25 | + - Prefer `kotlin.io` extensions over Java IO streams |
| 26 | + - Use scope functions (`let`, `apply`, `run`, `with`, `also`) appropriately |
| 27 | + - Leverage extension functions for better readability |
| 28 | + - Use property delegates (`by lazy`, `by Delegates`) where appropriate |
| 29 | + - Use Elvis operator (`?:`) for fallback values |
| 30 | + |
| 31 | +## Architecture Guidelines |
| 32 | +- Network models (from APIs) should not be propagated to the presentation layer |
| 33 | +- Always map network models to domain models in the repository layer |
| 34 | +- Service layer handles API communication, Repository layer handles domain translation |
| 35 | +- One-shot data fetching should use suspend functions, not Flows |
| 36 | +- Only use Flows when there's data to continuously observe |
| 37 | +- Classes should receive their dependencies through constructor parameters: |
| 38 | + ```kotlin |
| 39 | + // Preferred: Constructor injection |
| 40 | + class MyRepository( |
| 41 | + private val apiManager: ApiManager, |
| 42 | + private val database: AppDatabase |
| 43 | + ) |
| 44 | + |
| 45 | + // Avoid: Property injection |
| 46 | + class MyRepository : KoinComponent { |
| 47 | + private val apiManager: ApiManager by inject() |
| 48 | + private val database: AppDatabase by inject() |
| 49 | + } |
| 50 | + ``` |
| 51 | +- Limit KoinComponent usage to top-level components (ViewModels, Application class) |
| 52 | +- Apply appropriate visibility modifiers to control access: |
| 53 | + ```kotlin |
| 54 | + // Preferred: Implementation classes are internal |
| 55 | + internal class MyRepositoryImpl( |
| 56 | + private val apiManager: ApiManager |
| 57 | + ) : MyRepository { |
| 58 | + // Private implementation details |
| 59 | + private fun processData(data: Data): ProcessedData { |
| 60 | + // ... |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Public interfaces define the contract |
| 65 | + interface MyRepository { |
| 66 | + suspend fun getData(): Data |
| 67 | + } |
| 68 | + ``` |
15 | 69 |
|
16 | 70 | ## Project Structure |
17 | 71 | - `feature/` - App features organized by domain (tracker, search, settings) |
|
0 commit comments