Chuyển đến nội dung
Skillpedia
Lập trình chung48k · #5894

kotlin

Skill này làm gì

Hướng dẫn này dạy cách viết code Kotlin hiệu quả và rõ ràng hơn theo các thực hành tốt nhất. Nó chỉ cách dùng các tính năng ngôn ngữ để đơn giản hóa tác vụ lập trình thông thường.

Khi nào nên dùng

  • ✓Khi học lập trình Kotlin.
  • ✓Khi xem xét hoặc cải thiện code Kotlin có sẵn.
  • ✓Khi muốn code Kotlin ngắn gọn và dễ đọc hơn.

Liên kết skill

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

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/super-code/kotlin/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm sao để code Kotlin này gọn hơn khi dùng scope functions?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Hướng dẫn so sánh ví dụ “xấu” và “tốt” để minh họa thực hành tốt nhất. Nó tập trung vào việc dùng các công cụ có sẵn của Kotlin để tránh code lặp lại hoặc phức tạp.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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 }


…
Câu hỏi thường gặp

“Scope functions” là gì?

Đây là các hàm đặc biệt của Kotlin (như `let`, `run`, `apply`) giúp thực thi một khối code trên một đối tượng, khiến code dễ đọc hơn.

“Null Safety” là gì?

Đây là tính năng của Kotlin giúp ngăn các lỗi thường gặp do biến lẽ ra phải có giá trị nhưng lại rỗng (null). Hướng dẫn chỉ cách xử lý điều này an toàn.

“Coroutines” là gì?

Coroutines là cách xử lý các tác vụ tốn thời gian (như tải dữ liệu từ internet) mà không làm đứng ứng dụng. Hướng dẫn gợi ý cách dùng chúng hiệu quả.

Chi tiết

Mô tả gốc của tác giả

Language-specific super-code guidelines for kotlin.

repo: sickn33/agentic-awesome-skills

Độ phổ biến

Sao GitHub
48k
Vị trí xếp hạng
#5894
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự