When to Use Each
Trade-offs.
When to Use Each is a free Kotlin Academy lesson on CoddyKit — lesson 4 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.
Two Tools, Different Jobs
Infix functions are about readability; tailrec functions are about safe, efficient recursion. They solve unrelated problems.
infix fun Int.add(o: Int) = this + o
tailrec fun sum(n: Int, acc: Int = 0): Int =
if (n == 0) acc else sum(n - 1, acc + n)
fun main() {
println(2 add 3)
println(sum(10))
}Choose Infix for Fluent APIs
Reach for infix when a call reads better without dots and parentheses, especially in DSLs and pair-building.
fun main() {
val pair = "id" to 100
println(pair)
}Choose tailrec for Deep Recursion
Use tailrec when a recursive algorithm could recurse deeply enough to overflow the stack.
tailrec fun count(n: Int, acc: Int = 0): Int =
if (n == 0) acc else count(n - 1, acc + 1)
fun main() {
println(count(50000))
}Infix Trade-offs
Infix removes punctuation, which can reduce clarity for unfamiliar readers and complicates precedence. Use it only when it clearly helps.
infix fun Int.scaledBy(f: Int) = this * f
fun main() {
println(5 scaledBy 3)
}tailrec Trade-offs
Making code tail-recursive often requires an accumulator, which can feel less direct than a plain loop. Weigh expressiveness against simplicity.
tailrec fun product(n: Int, acc: Long = 1): Long =
if (n <= 1) acc else product(n - 1, acc * n)
fun main() {
println(product(6))
}When a Loop Is Simpler
If a plain for or while loop is clearer than tail recursion, prefer the loop. Recursion is a tool, not a requirement.
fun sumLoop(n: Int): Int {
var total = 0
for (i in 1..n) total += i
return total
}
fun main() {
println(sumLoop(10))
}When a Named Call Is Clearer
If an infix name is not obviously a binary relation, a normal method call communicates intent better.
data class Temp(val c: Int) {
fun isWarmerThan(o: Temp) = c > o.c
}
fun main() {
println(Temp(30).isWarmerThan(Temp(20)))
}Combining Both
The two features are independent; a single program can use infix for readable calls and tailrec for safe recursion.
infix fun Int.upto(n: Int) = (this..n).toList()
tailrec fun last(list: List<Int>): Int =
if (list.size == 1) list[0] else last(list.drop(1))
fun main() {
println(last(1 upto 5))
}Readability Checklist
Ask: does infix make the call clearer to a newcomer? Does tailrec prevent a real overflow risk? If not, prefer plain functions and loops.
fun main() {
val r = (1..5).sum()
println(r)
}Performance Note
tailrec gives loop-like performance with constant stack space. Infix has no runtime cost; it is purely syntactic.
tailrec fun pow(base: Int, exp: Int, acc: Long = 1): Long =
if (exp == 0) acc else pow(base, exp - 1, acc * base)
fun main() {
println(pow(2, 10))
}Decision Summary
Infix: fluent, one-argument binary-style calls. Tailrec: recursion that must scale safely. Neither is a default; both are deliberate choices.
infix fun String.joinWith(o: String) = "$this-$o"
fun main() {
println("a" joinWith "b")
}Quick Check
Test your understanding of when to use each feature.
Recap
You learned when to use each:
- Use
infixfor fluent, readable binary-style calls and DSLs. - Use
tailrecfor recursion that must scale without stack overflow. - Prefer plain loops or named functions when they are clearer.
- The two features are independent and can be combined.
infix fun Int.by(o: Int) = this * o
tailrec fun dec(n: Int): Int = if (n == 0) 0 else dec(n - 1)
fun main() {
println(3 by 4)
println(dec(1000))
}Frequently asked questions
Is the “When to Use Each” lesson free?
Yes — the full text of “When to Use Each” 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 “When to Use Each”?
Trade-offs. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “When to Use Each” 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.
All lessons in this course
- Infix Functions
- Building DSL-like APIs
- tailrec Functions
- When to Use Each