Use Cases
Domain modeling.
Use Cases is a free Kotlin Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Modeling the Domain
Value classes shine in domain modeling. They turn anonymous primitives into meaningful, mistake-proof types without runtime cost.
Let us walk through realistic scenarios.
Typed Identifiers
Distinct ID types prevent passing a user id where an order id belongs — a common and costly bug.
@JvmInline
value class UserId(val value: Long)
@JvmInline
value class OrderId(val value: Long)
fun fetchUser(id: UserId) = "user " + id.value
fun main() {
println(fetchUser(UserId(7)))
}Units of Measure
Wrapping numbers in unit types stops you adding meters to seconds by accident and documents the meaning.
@JvmInline
value class Kilograms(val value: Double)
fun describe(weight: Kilograms) = weight.value.toString() + " kg"
fun main() {
println(describe(Kilograms(72.5)))
}Validated Strings
Combine a value class with an init block to guarantee invariants like a non-blank email.
@JvmInline
value class Email(val value: String) {
init {
require(value.contains("@")) { "invalid email" }
}
}
fun main() {
println(Email("a@b.com").value)
}Money With Behavior
Represent money in the smallest unit and add operations, avoiding floating-point currency errors.
@JvmInline
value class Cents(val value: Long) {
operator fun plus(other: Cents) = Cents(value + other.value)
fun format() = "$" + value / 100.0
}
fun main() {
val total = Cents(1500) + Cents(250)
println(total.format())
}Self-Documenting APIs
A function signature with value classes is far clearer than one with three loose integers.
@JvmInline
value class Width(val px: Int)
@JvmInline
value class Height(val px: Int)
fun area(w: Width, h: Height) = w.px * h.px
fun main() {
println(area(Width(4), Height(5)))
}Wrapping Sensitive Data
A value class can override toString to mask secrets in logs while keeping the raw value accessible.
@JvmInline
value class Token(val value: String) {
override fun toString() = "Token(***)"
}
fun main() {
val t = Token("abc123")
println(t) // masked
println(t.value) // real value when needed
}Enforcing Ranges
Use value classes to bottle up range rules in one place, so invalid states cannot be constructed anywhere.
@JvmInline
value class Rating(val stars: Int) {
init {
require(stars in 1..5) { "stars must be 1..5" }
}
}
fun main() {
println(Rating(4).stars)
}Smart Constructors
Pair a value class with a companion factory that returns null on invalid input, avoiding exceptions for expected failures.
@JvmInline
value class Age(val value: Int) {
companion object {
fun of(n: Int): Age? = if (n in 0..150) Age(n) else null
}
}
fun main() {
println(Age.of(30)?.value)
println(Age.of(-5))
}Patterns Summary
Common value class patterns:
- Typed IDs to prevent mix-ups
- Units of measure
- Validated primitives with
init - Domain operations via operators
- Masked sensitive values
Cheap Safety
Because value classes are usually inlined, you get all this domain clarity and safety at essentially zero runtime cost. They are a low-friction way to make illegal states unrepresentable.
Quick Check
Test your understanding of value class use cases.
Recap
You saw practical value class use cases:
- Typed IDs, units, validated strings
- Money and domain operations
- Masking secrets and smart constructors
Next: the limitations of value classes.
Frequently asked questions
Is the “Use Cases” lesson free?
Yes — the full text of “Use Cases” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Use Cases”?
Domain modeling. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Use Cases” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.