純粋関数としてのビジネスルール
プラットフォームの副作用を避け、ロジックをテスト可能に保ちます
「純粋関数としてのビジネスルール」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.0Why 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.0Compose 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..20Avoid 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. ✅
よくある質問
「純粋関数としてのビジネスルール」レッスンは無料ですか?
はい。「純粋関数としてのビジネスルール」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「純粋関数としてのビジネスルール」で何を学びますか?
プラットフォームの副作用を避け、ロジックをテスト可能に保ちます ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「純粋関数としてのビジネスルール」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 共有Price・Tax Calculator
- 通貨と数値のフォーマット
- 純粋関数としてのビジネスルール
- UIを共有コードから分離する