Langsung ke konten
Skillpedia
Pemrograman umum48k Β· #5894

kotlin

Fungsinya

Panduan ini mengajarkan cara menulis kode Kotlin yang lebih efisien dan jelas dengan mengikuti praktik terbaik. Panduan ini menunjukkan cara menggunakan fitur bahasa untuk menyederhanakan tugas pemrograman yang umum.

Kapan dipakai

  • βœ“Saat belajar membuat program dengan Kotlin.
  • βœ“Saat meninjau atau memperbaiki kode Kotlin yang sudah ada.
  • βœ“Saat ingin menulis kode yang lebih ringkas dan mudah dibaca.

Link skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/super-code/kotlin/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/super-code/kotlin/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

Contoh permintaan ke AI

β€œBagaimana cara membuat kode Kotlin ini lebih rapi dengan menggunakan scope functions?”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Panduan ini membandingkan contoh kode 'buruk' dengan contoh 'baik' untuk menjelaskan praktik terbaik. Fokusnya adalah menggunakan perangkat bawaan Kotlin untuk menghindari kode yang berulang atau rumit.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

Kotlin + Compose: Idiomatic Efficiency Reference

When to Use

  • Use this skill when the task matches this description: Language-specific super-code guidelines for kotlin.

Table of Contents

  1. Collections & Data Transformation
  2. Null Safety
  3. Functions & Lambdas
  4. Classes & Objects
  5. Coroutines & Flow
  6. Compose UI
  7. Anti-patterns specific to Kotlin

1. Collections & Data Transformation {#collections}

Prefer scope functions and stdlib transforms over imperative loops.

// ❌ Verbose
val result = mutableListOf<String>()
for (item in items) {
    if (item.isActive) {
        result.add(item.name.uppercase())
    }
}

// βœ… Idiomatic
val result = items.filter { it.isActive }.map { it.name.uppercase() }
// ❌ Manual grouping
val map = mutableMapOf<String, MutableList<Item>>()
for (item in items) {
    map.getOrPut(item.category) { mutableListOf() }.add(item)
}

// βœ…
val map = items.groupBy { it.category }
// ❌ Manual fold
var total = 0
for (order in orders) total += order.amount

// βœ…
val total = orders.sumOf { it.amount }

Use associate, associateBy, partition, flatMap, zip instead of manual equivalents.


2. Null Safety {#null-safety}

// ❌ Unnecessary null check when Elvis suffices
val name: String
if (user?.name != null) {
    name = user.name
} else {
    name = "Guest"
}

// βœ…
val name = user?.name ?: "Guest"
// ❌ Double null check
if (response != null && response.body != null) {
    process(response.body!!)
}

// βœ…
response?.body?.let { process(it) }
// ❌ !! without guard
val value = nullable!!.doSomething()

// βœ… Make the non-null contract explicit at the boundary
val value = requireNotNull(nullable) { "nullable must be set before calling X" }.doSomething()
// Or return early:
val n = nullable ?: return

3. Functions & Lambdas {#functions}

// ❌ Single-expression function with unnecessary block body
fun double(x: Int): Int {
    return x * 2
}

// βœ…
fun double(x: Int) = x * 2
// ❌ Lambda capturing unused parameter
items.forEach { item -> doSomething() }

// βœ…
items.forEach { doSomething() }
// ❌ Redundant with/apply nesting
val builder = Builder()
builder.setName("x")
builder.setAge(1)
val result = builder.build()

// βœ…
val result = Builder().apply {
    setName("x")
    setAge(1)
}.build()

Prefer let, run, apply, also, with over repeated receiver references β€” but don't nest more than 2 levels deep.


4. Classes & Objects {#classes}

// ❌ Mutable class for immutable data
class Point {
    var x: Int = 0
    var y: Int = 0
}

// βœ…
data class Point(val x: Int, val y: Int)
// ❌ Companion object just to hold a constant
class Foo {
    companion object {
        val TAG = "Foo"
    }
}

// βœ… β€” top-level if only used in this file
private const val TAG = "Foo"
class Foo
// ❌ Enum with when that has to be updated in two places
enum class Status { ACTIVE, INACTIVE }
fun label(s: Status) = when(s) { Status.ACTIVE -> "Active"; Status.INACTIVE -> "Inactive" }

// βœ… β€” put display logic on the enum itself
enum class Status(val label: String) { ACTIVE("Active"), INACTIVE("Inactive") }

Sealed classes/interfaces over enum when variants carry different data.


5. Coroutines & Flow {#coroutines}

// ❌ Unnecessary async/await pair when result is used immediately
val result = async { fetchData() }.await()

// βœ…
val result = fetchData() // just suspend fun, no async needed
// ❌ Collecting in a loop
while (true) {
    val value = channel.receive()
    process(value)
}

// βœ…
channel.consumeEach { process(it) }
// or for Flow:
flow.collect { process(it) }
// ❌ StateFlow + manual emit boilerplate
private val _state = MutableStateFlow(initial)
val state: StateFlow<State> = _state
// ... in many places: _state.value = newValue

// βœ… β€” use update{} for atomic mutation
_state.update { it.copy(field = newValue) }

Don't launch coroutines in constructors or init blocks. Don't use GlobalScope.


6. Compose UI {#compose}

// ❌ Unnecessary remember for derived state that's cheap to compute
val displayName = remember { user.firstName + " " + user.lastName }


…
Pertanyaan umum

Apa itu 'scope functions'?

Ini adalah fungsi khusus Kotlin (seperti `let`, `run`, `apply`) yang membantu menjalankan blok kode pada sebuah objek, sehingga kode lebih mudah dibaca.

Apa itu 'Null Safety'?

Ini adalah fitur Kotlin yang membantu mencegah error umum yang disebabkan oleh variabel yang seharusnya memiliki nilai, tetapi ternyata kosong (null). Panduan ini menunjukkan cara menanganinya dengan aman.

Apa itu 'Coroutines'?

Ini adalah cara untuk menangani tugas yang memakan waktu (seperti mengambil data dari internet) tanpa membuat aplikasi macet. Panduan ini memberikan saran cara menggunakannya secara efisien.

Detail

Deskripsi asli dari pembuat

Language-specific super-code guidelines for kotlin.

repo: sickn33/agentic-awesome-skills

Popularitas

Bintang GitHub
48k
Peringkat
#5894
Lisensi
MIT
Diperbarui
15/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa