Use Cases
Domain modeling.
Use Cases is a free Kotlin Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
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.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Use Cases” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Use Cases”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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.