Skip to content

Commit 6c11c1a

Browse files
authored
feat: add cursor rule (#9)
## 🚀 Summary add cursor rule ## ♻️ Changes https://github.com/nphausg/cursor.rules
1 parent 41ba38d commit 6c11c1a

15 files changed

Lines changed: 184 additions & 12 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
description: Enforce clean architecture principles in Android projects
3+
globs:
4+
- "app/src/main/java/**"
5+
alwaysApply: true
6+
---
7+
- Follow domain/data/presentation separation: domain is business logic, data is data sources, presentation is UI.
8+
- Keep domain layer free of Android framework dependencies.
9+
- Use UseCases (Interactors) for business rules, triggered from ViewModels or Controllers.
10+
- Repositories should expose interfaces, not implementations.
11+
- Avoid directly accessing data sources in presentation layer.
12+
- Prefer dependency inversion: inject interfaces rather than concrete classes.
13+
- Maintain simple, testable domain models.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
description: Dependency injection patterns with Hilt
3+
globs:
4+
- "app/src/main/java/**"
5+
alwaysApply: true
6+
---
7+
- Annotate Application class with @HiltAndroidApp
8+
- Use @HiltViewModel for ViewModel injection
9+
- Provide dependencies in @Module with @InstallIn
10+
- Inject interfaces rather than concrete implementations
11+
- Use qualifiers when multiple implementations exist
12+
- Avoid static singletons outside of Hilt’s graph
13+
- Keep modules small and logically grouped
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
alwaysApply: false
3+
---
4+
For every Kotlin Android code you generate or refactor:
5+
6+
1. **Architecture:**
7+
- Follow **MVVM pattern** strictly.
8+
- Business logic must go into `ViewModel`, not in `View` (Activity/Fragment/Composable).
9+
- Views should only observe `StateFlow`/`LiveData` from ViewModel.
10+
11+
2. **Kotlin Best Practices:**
12+
- Use `StateFlow` instead of `LiveData` unless explicitly required.
13+
- Use Kotlin Coroutines (structured concurrency, `viewModelScope`, `SupervisorJob`).
14+
- Prefer `val` over `var` where possible.
15+
- Use extension functions and sealed classes when appropriate.
16+
- No memory leaks (avoid context leaks in ViewModel).
17+
- Use Dependency Injection (Hilt) if necessary.
18+
19+
3. **Android Performance:**
20+
- Avoid heavy work on the main thread.
21+
- Use `Dispatchers.IO` or background coroutines for I/O, disk, and network tasks.
22+
- Use `lazy` initialization where suitable.
23+
- For RecyclerView/List: use `ListAdapter` + `DiffUtil`.
24+
- Avoid unnecessary recomposition in Jetpack Compose.
25+
- Apply LRU caching for repeated data/images if needed.
26+
27+
4. **Testing:**
28+
- Generated ViewModel and Repository code must be testable (no static singletons).
29+
- Show sample unit test if introducing new logic.
30+
31+
5. **Code Quality:**
32+
- Minimize code duplication.
33+
- Clear function separation (Single Responsibility Principle).
34+
- Provide brief inline comments for complex sections.
35+
36+
6. **Output Requirement:**
37+
- Provide **full code samples** (including imports) unless specified otherwise.
38+
- Brief explanation of key design decisions made.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
description: Android Integration Testing best practices for multi-layer testing
3+
alwaysApply: false
4+
---
5+
- Integration tests should verify interactions between multiple layers (e.g., ViewModel + Repository + Data Source).
6+
- Use **Hilt Test Components** or DI overrides to inject fake or in-memory dependencies.
7+
- Avoid hitting real external services; use mocked network responses or test databases.
8+
- Write integration tests to cover critical user flows end-to-end.
9+
- Keep tests stable and repeatable; clean up any test data after execution.
10+
- Use **JUnit Rules** or **TestContainers** where applicable for environment setup/teardown.
11+
- Separate integration tests from unit tests and run them less frequently (e.g., nightly builds).
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
description: Naming conventions for consistency
3+
globs:
4+
- "**/*.kt"
5+
alwaysApply: true
6+
---
7+
- Classes: PascalCase
8+
- Functions: camelCase
9+
- Constants: UPPER_SNAKE_CASE
10+
- Variables: camelCase
11+
- Test functions: GIVEN_<condition> WHEN <action> THEN <result>
12+
- Avoid abbreviations and cryptic names
13+
- Name should communicate clear intent
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
description: Android UI testing best practices using Espresso and Compose Testing
3+
alwaysApply: false
4+
---
5+
- Use **Espresso** for testing classic Android Views; use **Compose Testing APIs** for Jetpack Compose UI.
6+
- Keep UI tests focused on user interactions and visible UI changes.
7+
- Avoid complex logic in UI tests; delegate business logic testing to unit tests.
8+
- Use **Idling Resources** or `composeTestRule.awaitIdle()` to synchronize asynchronous events.
9+
- Use clear, descriptive test names that reflect user behavior being tested.
10+
- Isolate UI tests from backend dependencies by using mock servers or test doubles.
11+
- Run UI tests on real or emulated devices to catch platform-specific issues.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
description: Unit testing best practices with BDD style
3+
alwaysApply: false
4+
---
5+
- Name test functions in BDD style: given_<condition>_when_<action>_then_<result>
6+
- Use GIVEN, WHEN, THEN comments in the test body for clarity.
7+
- Write one behavioral scenario per test.
8+
- Use mocks (MockK, Mockito) to isolate dependencies.
9+
- Prefer runTest or coroutine test rules for suspend functions.
10+
- Avoid fragile tests with real-time delays or random input.
11+
- Example:
12+
```kotlin
13+
@Test
14+
fun givenValidUser_whenFetchUser_thenReturnsUser() {
15+
// GIVEN
16+
val userId = 123
17+
coEvery { userRepo.getUser(userId) } returns User(userId, "Alice")
18+
19+
// WHEN
20+
val result = runBlocking { sut.fetchUser(userId) }
21+
22+
// THEN
23+
assertEquals("Alice", result.name)
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
description: Best practices for WorkManager usage in background tasks
3+
alwaysApply: false
4+
---
5+
- Use `CoroutineWorker` for suspendable background work.
6+
- Keep work inputs and outputs small and serializable.
7+
- Chain dependent work using `WorkContinuation`.
8+
- Use appropriate `Constraints` for battery and network considerations.
9+
- Monitor and handle work statuses for retries and failures gracefully.
10+
- Avoid long-running tasks; break them into smaller units if needed.
11+
- Clean up or cancel obsolete work to prevent resource leaks.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
description: Clean code principles for better readability
3+
alwaysApply: false
4+
---
5+
- Keep functions small and focused
6+
- One class per file
7+
- Limit function size to 20 lines if possible
8+
- Avoid long parameter lists (max 3–4 parameters)
9+
- Minimize nested control structures
10+
- Remove commented-out code before committing
11+
- Prefer meaningful names over comments
12+
- DRY (Don’t Repeat Yourself): extract duplication
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
description: Performance best practices for Android
3+
alwaysApply: false
4+
---
5+
- Avoid allocating objects in tight loops
6+
- Use lazy initialization where appropriate
7+
- Reuse existing objects instead of recreating
8+
- Prefer immutable data structures for concurrency
9+
- Minimize reflection usage
10+
- Avoid excessive recomposition in Compose by using keys and remember
11+
- Profile and measure before optimizing prematurely

0 commit comments

Comments
 (0)