0
0
Kotlinprogramming~20 mins

Why DSLs improve readability in Kotlin - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DSL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin DSL code?

Consider this Kotlin DSL snippet that builds a simple HTML-like structure. What will be printed?

Kotlin
fun html(block: Html.() -> Unit): Html {
    val html = Html()
    html.block()
    return html
}

class Html {
    private val children = mutableListOf<String>()
    fun body(block: Body.() -> Unit) {
        val body = Body()
        body.block()
        children.add(body.toString())
    }
    override fun toString() = "<html>" + children.joinToString() + "</html>"
}

class Body {
    private val children = mutableListOf<String>()
    fun p(text: String) {
        children.add("<p>$text</p>")
    }
    override fun toString() = "<body>" + children.joinToString() + "</body>"
}

fun main() {
    val result = html {
        body {
            p("Hello, DSL!")
        }
    }
    println(result)
}
AHello, DSL!
B<html><body>Hello, DSL!</body></html>
C<html><body><p>Hello, DSL!</p></body></html>
D<html><p>Hello, DSL!</p></html>
Attempts:
2 left
💡 Hint

Look at how the toString() methods build the string with tags.

🧠 Conceptual
intermediate
1:30remaining
Why do DSLs improve code readability?

Which of the following best explains why Domain Specific Languages (DSLs) improve readability in code?

AThey always reduce the number of lines of code by half.
BThey replace all variables with numbers to simplify code.
CThey force programmers to write code in a new programming language.
DThey use syntax and structure close to the problem domain, making code easier to understand.
Attempts:
2 left
💡 Hint

Think about how DSLs relate to the problem they solve.

🔧 Debug
advanced
2:00remaining
Identify the error in this Kotlin DSL snippet

What error will this Kotlin DSL code produce when run?

Kotlin
fun html(block: Html.() -> Unit): Html {
    val html = Html()
    html.block()
    return html
}

class Html {
    private val children = mutableListOf<String>()
    fun body(block: Body.() -> Unit) {
        val body = Body()
        block(body)
        children.add(body.toString())
    }
    override fun toString() = "<html>" + children.joinToString() + "</html>"
}

class Body {
    private val children = mutableListOf<String>()
    fun p(text: String) {
        children.add("<p>$text</p>")
    }
    override fun toString() = "<body>" + children.joinToString() + "</body>"
}

fun main() {
    val result = html {
        body {
            p("Hello, DSL!")
        }
    }
    println(result)
}
AType mismatch error: Required Body.() -> Unit but found (Body) -> Unit
BNullPointerException at runtime
CNo error, prints <html><body><p>Hello, DSL!</p></body></html>
DSyntax error: missing colon after function declaration
Attempts:
2 left
💡 Hint

Check how the lambda is called inside body function.

📝 Syntax
advanced
2:00remaining
Which Kotlin DSL snippet correctly defines a nested structure?

Which option correctly defines a Kotlin DSL to build a nested list of items?

A
fun list(block: ListBuilder.() -&gt; Unit) = ListBuilder().apply(block).build

class ListBuilder {
    private val items = mutableListOf&lt;String&gt;()
    fun item(value: String) { items.add(value) }
    fun build() = items
}

val result = list {
    item("apple")
    item("banana")
}
println(result)
B
fun list(block: ListBuilder.() -&gt; Unit) = ListBuilder().apply(block).build()

class ListBuilder {
    private val items = mutableListOf&lt;String&gt;()
    fun item(value: String) { items.add(value) }
    fun build() = items
}

val result = list {
    item("apple")
    item("banana")
}
println(result)
C
fun list(block: ListBuilder.() -&gt; Unit) = ListBuilder().apply(block).build()

class ListBuilder {
    private val items = mutableListOf&lt;String&gt;()
    fun item(value: String) { items.add(value) }
    fun build() = items
}

val result = list {
    item("apple")
    item("banana")
}
println(result.toString())
D
fun list(block: ListBuilder.() -&gt; Unit) = ListBuilder().apply(block).build()

class ListBuilder {
    private val items = mutableListOf&lt;String&gt;()
    fun item(value: String) { items.add(value) }
    fun build() = items
}

val result = list {
    item("apple")
    item("banana")
}
println(result())
Attempts:
2 left
💡 Hint

Check how build() is called and how result is printed.

🚀 Application
expert
2:30remaining
How does a Kotlin DSL improve readability in a real-world scenario?

Imagine you are writing configuration code for a web server. Which Kotlin DSL style snippet improves readability the most?

A
serverConfig {
    port = 8080
    ssl {
        enabled = true
        keyStore = "/path/to/keystore"
    }
}
B
serverConfig.setPort(8080)
serverConfig.getSsl().setEnabled(true)
serverConfig.getSsl().setKeyStore("/path/to/keystore")
CserverConfig.port(8080).ssl(true, "/path/to/keystore")
DserverConfig.configure(port=8080, sslEnabled=true, keyStorePath="/path/to/keystore")
Attempts:
2 left
💡 Hint

Think about which style reads like natural instructions and groups related settings.