0Pricing
Kotlin Multiplatform Academy · Lesson

Business Rules as Pure Functions

Keep logic testable by avoiding platform side effects.

Business Rules as Pure Functions is a free Kotlin Multiplatform 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 Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Pure Function

A pure function returns the same output for the same input and touches nothing outside itself. That makes it perfect for shared code.

No Hidden Side Effects

Pure means no logging, no network, no saving to disk. The function just takes values in and gives a result back.

fun shippingFee(weight: Double): Double = if (weight > 5) 9.0 else 4.0

Why Purity Wins in KMP

Pure functions have no platform dependencies, so they compile and behave identically on Android, iOS and desktop. 🎯

Easy to Test

With no hidden state, a test is just input in, expected output checked. No mocks, no setup, no flaky surprises.

assertEquals(9.0, shippingFee(6.0))

Rules as Small Functions

Express each business rule as its own tiny function. Small pieces compose into bigger logic without tangling.

fun isFreeShipping(total: Double) = total >= 50.0

Compose the Rules

Combine small rules to make decisions. Each part stays pure, so the whole composition stays predictable and testable.

fun finalShipping(total: Double, weight: Double) =
  if (isFreeShipping(total)) 0.0 else shippingFee(weight)

Pass Time as a Parameter

Reading the clock makes a function impure. Instead, pass the current time in so the rule stays deterministic.

fun isPeakHour(hour: Int): Boolean = hour in 17..20

Avoid Global Mutable State

Reading or changing a global variable breaks purity. Keep all the data a rule needs in its parameters.

Return Values, Not Mutations

Instead of editing the input, return a new value. This avoids surprises when the same object is used elsewhere.

fun withDiscount(price: Double, off: Double) = price * (1 - off)

Pure Logic, Impure Edges

Keep your rules pure at the core, and let the edges of the app handle effects like saving or showing results.

Logic Never Drifts

Because pure rules live once in shared code, a change updates both apps at once. Business logic can never drift apart again.

Quick Check

Which trait makes a function pure and ideal for shared KMP code?

Recap: Keep It Pure

You wrote business rules as small, side-effect-free functions that test easily and run the same on every target. ✅

Frequently asked questions

Is the “Business Rules as Pure Functions” lesson free?

Yes — the full text of “Business Rules as Pure Functions” is free to read here on the web, and the Kotlin Multiplatform 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 Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “Business Rules as Pure Functions”?

Keep logic testable by avoiding platform side effects. You practise Kotlin Multiplatform 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 Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform 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 “Business Rules as Pure Functions” 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 Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform 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

  1. A Shared Price & Tax Calculator
  2. Format Currency & Numbers
  3. Business Rules as Pure Functions
  4. Keep UI Out of Shared Code
← Back to Kotlin Multiplatform Academy