← Back to Kotlin Course | Chapter 14: JVM Interop & Best Practices | Lesson 6 of 7

Idiomatic Kotlin लिखना

Idiomatic Kotlin का मतलब है code को उस तरह लिखना जैसे अनुभवी Kotlin developers स्वाभाविक रूप से लिखेंगे, language के अपने shortcuts उपयोग करते हुए दूसरी languages की पुरानी आदतों के बजाय।

Preferring Expressions Over Statements

Idiomatic Kotlin expression-based constructs को प्राथमिकता देता है -- expressions के रूप में if/when, single-expression functions -- दूसरी languages से copy किए गए verbose statement-based equivalents के बजाय।

उदाहरण: Preferring Expressions Over Statements

markup
fun describe(n: Int) = when {
    n < 0 -> "negative"
    n == 0 -> "zero"
    else -> "positive"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `describe(-5)` to the console, with a trailing newline
    println(describe(-5))
    // Print `describe(0)` to the console, with a trailing newline
    println(describe(0))
    // Print `describe(10)` to the console, with a trailing newline
    println(describe(10))
}

Using Data Classes Instead of Manual Boilerplate

manually equals(), hashCode(), और toString() लिखने के बजाय data class का सहारा लेना idiomatic Kotlin की एक पहचान है, code और संभावित bugs दोनों बचाते हुए।

उदाहरण: Using Data Classes Instead of Manual Boilerplate

markup
// Define a data class `Point`
data class Point(val x: Int, val y: Int)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `p1`, set to `Point(1, 2)`
    val p1 = Point(1, 2)
    // Declare a read-only variable `p2`, set to `Point(1, 2)`
    val p2 = Point(1, 2)
    // Print "Equal: ${p1 == p2}, toString: $p1" to the console, with a trailing newline
    println("Equal: ${p1 == p2}, toString: $p1")
}

Embracing Null Safety Idioms

Idiomatic Kotlin built-in null safety न रखने वाली languages से copy किए manual null checks के बजाय ?., ?:, और safe scope functions उपयोग करता है।

उदाहरण: Embracing Null Safety Idioms

markup
// Define the function `greetOrDefault` taking `name` and returning a String
fun greetOrDefault(name: String?): String = name?.let { "Hello, $it!" } ?: "Hello, stranger!"

// Entry point: execution of the program starts here
fun main() {
    // Print `greetOrDefault("Kotlin")` to the console, with a trailing newline
    println(greetOrDefault("Kotlin"))
    // Print `greetOrDefault(null)` to the console, with a trailing newline
    println(greetOrDefault(null))
}

Favoring Immutability and Small Functions

Idiomatic Kotlin var के बजाय val, default रूप से mutable के बजाय read-only collections, और बड़ी, सब-कुछ-करने वाली utility classes के बजाय छोटे, अच्छी तरह नामित functions (extensions सहित) की ओर झुकता है।

उदाहरण: Favoring Immutability and Small Functions

markup
fun List<Int>.average2(): Double = if (isEmpty()) 0.0 else sum().toDouble() / size

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `scores`, set to `listOf(80, 90, 70)`
    val scores = listOf(80, 90, 70)
    // Print "Average: ${scores.average2()}" to the console, with a trailing newline
    println("Average: ${scores.average2()}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Kotlin में Java-style code लिखना, जैसे manual getter/setter methods या verbose null checks, बजाय Kotlin की built-in properties और null-safety operators उपयोग करने के।
  2. expression-oriented features जैसे expressions के रूप में if/when और single-expression functions को नज़रअंदाज़ करना, अनावश्यक रूप से verbose code पैदा करते हुए।
  3. advanced features (जैसे भारी operator overloading या अत्यधिक DSLs) का ज़्यादा उपयोग करना जहां साधारण, सरल code ज़्यादा साफ़ होता।
चैप्टर सारांश
  • var के बजाय val, verbose blocks के बजाय expression bodies, और manual null checks के बजाय built-in null-safety operators को प्राथमिकता दें।
  • manually equals/hashCode/toString लिखने के बजाय साधारण data holders के लिए data classes उपयोग करें।
  • बड़ी utility classes के बजाय immutable collections और छोटे, केंद्रित extension functions को प्राथमिकता दें।
  • Idiomatic code readability की कीमत पर brevity के बजाय स्पष्टता और संक्षिप्तता दोनों को प्राथमिकता देता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.