agentleFS
javiercamarenatriguerojaviercamarenatriguero/android-skills/kotlin-convention/SKILL.md

kotlin-convention

Skill8 starsChanged 5 months agoApache-2.0View on GitHub

Writes idiomatic, clean Kotlin following Android conventions. Use when user asks for "Kotlin code review", "is this idiomatic Kotlin", "Kotlin style check", "how to write this in Kotlin", or to "follow Kotlin best practices".

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Avoid data, info, manager as suffixes unless unavoidable

    line 30
  2. Distinctive:

    Use for restricted hierarchies — states, results, events:

    line 104

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Kotlin Convention

Overview

This skill defines idiomatic Kotlin conventions for Android projects: naming, null safety, data modeling, functional patterns, and language features to embrace or avoid.


Naming

ElementConventionExample
Class / Interface / ObjectPascalCaseOrderRepository, UserMapper
Function / VariablecamelCasefetchUser(), currentState
Constant (const val, object val)SCREAMING_SNAKE_CASEMAX_RETRY_COUNT
Packagelowercase, dot-separatedcom.example.feature.order
Extension functioncamelCase, descriptiveString.isValidEmail()
Test functionbacktick sentence`SHOULD return error WHEN network fails`

Naming Rules

  • Be explicit over short: userIdentifier not uid
  • Boolean properties: prefix with is, has, can → isLoading, hasError
  • Functions returning Boolean: prefix with is, can, should → isValid(), canSubmit()
  • Avoid data, info, manager as suffixes unless unavoidable

Null Safety

Avoid !!

// ❌ Crashes at runtime if null
val name = user!!.name

// ✅ Safe call + Elvis
val name = user?.name ?: "Unknown"

// ✅ Early return pattern
fun process(user: User?) {
    val u = user ?: return
    doWork(u)
}

Prefer let, run, apply, also, with Appropriately

FunctionReceiverReturnsBest For
letitlambda resultNull checks, transformation
runthislambda resultObject configuration + result
applythisreceiverBuilder-style setup
alsoitreceiverSide effects (logging)
withthislambda resultMultiple calls on same object
// let for null-safe call
user?.let { sendEmail(it.email) }

// apply for object construction
val config = Config().apply {
    host = "api.example.com"
    port = 443
    timeout = 30
}

// also for logging side effect
return fetchUser(id).also { log("Fetched user: $it") }

Data Classes

// ✅ Immutable domain model
data class Order(
    val id: String,
    val items: List<OrderItem>,
    val status: OrderStatus,
    val createdAt: Instant,
)

// ✅ Update with copy
val updated = order.copy(status = OrderStatus.SHIPPED)

// ❌ Avoid mutable data classes for domain models
data class Order(
    var id: String,       // mutable = unpredictable
    var status: String,
)

Sealed Classes / Interfaces

Use for restricted hierarchies — states, results, events:

// Result type
sealed interface Result<out T> {
    data class Success<T>(val value: T) : Result<T>
    data class Error(val exception: Throwable) : Result<Nothing>
    data object Loading : Result<Nothing>
}

// Events
sealed interface UiEvent {
    data object OnScreenOpened : UiEvent
    data class OnItemClicked(val id: String) : UiEvent
}

// Exhaustive when — compiler enforces all branches
when (result) {
    is Result.Success -> show(result.value)
    is Result.Error -> showError(result.exception)
    Result.Loading -> showLoading()
}

Prefer sealed interface over sealed class — more flexible (multiple inheritance).


Functions

Single Expression Functions

// ✅ Concise for simple transformations
fun Double.toPercentage(): String = "${(this * 100).toInt()}%"

// ❌ Redundant block for single expression
fun Double.toPercentage(): String {
    return "${(this * 100).toInt()}%"
}

Extension Functions

// ✅ Domain-specific extensions
fun Instant.isOlderThan(duration: Duration): Boolean = this < Clock.System.now() - duration

fun String.isValidEmail(): Boolean = android.util.Patterns.EMAIL_ADDRESS.matcher(this).matches()

// ❌ Extension on types you don't own for unrelated functionality
fun String.doOrderThing(): Order { ... }  // Not clearly related to String

Default Parameters vs Overloads

// ✅ Default parameters (idiomatic Kotlin)
fun fetchOrders(userId: String, pageSize: Int = 20, page: Int = 0): List<Order>

// ❌ Multiple overloads (Java-style)
fun fetchOrders(userId: String): List<Order>
fun fetchOrders(userId: String, pageSize: Int): List<Order>
fun fetchOrders(userId: String, pageSize: Int, page: Int): List<Order>

Collections

// Prefer immutable by default
val items: List<Item> = listOf()       // immutable reference
val mutable: MutableList<Item> = mutableListOf()  // only when mutation needed

// Functional operators over loops
val activeUsers = users
    .filter { it.isActive }
    .sortedByDescending { it.lastLogin }
    .take(10)

// Sequences for large collections with multiple chained operations
val result = largeList
    .asSequence()
    .filter { it.isValid }
    .map { transform(it) }
    .firstOrNull()

// groupBy
val byCategory = products.groupBy { it.category }

// associateBy for quick lookup map
val userById: Map<String, User> = users.associateBy { it.id }

// partition splits into (matching, notMatching)
val (active, inactive) = users.partition { it.isActive }

Value Classes (Inline)

Prevent primitive type confusion at zero runtime cost:

@JvmInline value class UserId(val value: String)
@JvmInline value class ProductId(val value: String)
@JvmInline value class Price(val cents: Long) {
    val euros: Double get() = cents / 100.0
}

// ✅ Type-safe — won't accidentally mix UserId and ProductId
fun getUser(id: UserId): User
fun getProduct(id: ProductId): Product

Error Handling

// ✅ Use Result<T> for expected/recoverable errors
suspend fun fetchData(): Result<Data> = runCatching { api.getData() }

// ✅ Throw exceptions only for programmer errors (illegal state)
require(count >= 0) { "Count must be non-negative, got $count" }
check(isInitialized) { "Must call init() before use" }
error("Unexpected branch in ${::processState.name}")

// ❌ Don't use exceptions for flow control
try {
    val user = fetchUser(id)
} catch (e: UserNotFoundException) {  // ❌ use Result / nullable return
    // ...
}

Coroutines Style

// ✅ suspend function signature — always document blocking behavior
/** Fetches user from network. IO-bound. */
suspend fun fetchUser(id: String): User

// ✅ Name coroutine builders clearly
viewModelScope.launch(CoroutineName("load-orders")) { ... }

// ❌ runBlocking in production code
fun getUser(): User = runBlocking { fetchUser("1") }  // Blocks thread

When Expressions

// ✅ Exhaustive when as expression (sealed types)
val label = when (status) {
    Status.PENDING -> "Pending"
    Status.ACTIVE -> "Active"
    Status.CLOSED -> "Closed"
}

// ✅ When as statement with complex conditions
when {
    age < 0 -> error("Invalid age")
    age < 18 -> showMinorWarning()
    age >= 65 -> offerSeniorDiscount()
    else -> processStandard()
}

Object / Companion Object

// ✅ Companion object for factory methods and constants
class Config private constructor(val host: String, val port: Int) {
    companion object {
        val DEFAULT = Config("localhost", 8080)
        fun fromEnv() = Config(
            host = System.getenv("HOST") ?: "localhost",
            port = System.getenv("PORT")?.toInt() ?: 8080,
        )
    }
}

// ✅ object for singletons with no state
object DateFormatter {
    private val formatter = DateTimeFormatter.ISO_LOCAL_DATE
    fun format(date: LocalDate): String = formatter.format(date)
}

Anti-Patterns

❌ Avoid✅ Instead
!! operatorSafe call + Elvis / early return
var in data classesval + copy()
Checked exceptionsResult<T> / sealed error types
Java-style overloadsDefault parameter values
Nested if chainswhen expression / guard returns
Any as parameter typeSpecific sealed interface / generics
Magic numbersNamed constants / value classes
Mutable List exposed publiclyList (immutable) in public API
lateinit var for nullable typesNullable with ? or by lazy { } — lateinit is only valid for non-null, and crashes with UninitializedPropertyAccessException if accessed before init

Checklist: Code Review

  • Naming follows conventions (PascalCase, camelCase, SCREAMING_SNAKE)
  • No !! operator (except truly null-impossible cases with comment)
  • data class fields are val
  • Sealed hierarchies use sealed interface
  • No unchecked Exception for flow control
  • Large collection chains use .asSequence()
  • Coroutines use injected dispatchers
  • companion object used for factories/constants
  • Extension functions are placed in appropriate scope

References

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.