Kotlin: Functional or OOP? Choosing the Right Approach

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Kotlin: Two Worlds in One Language

\n\n

Have you ever wondered why some developers write in Kotlin as if it were an improved Java, while others turn their code into a cascade of lambdas and call chains? The answer is simple: Kotlin is a multi-paradigm language that gets along equally well with both object-oriented programming (OOP) and functional programming (FP).

\n\n

But how can a beginner not get lost in this diversity? And how can an experienced developer avoid falling into "spaghetti code" due to mixing styles? In this article, we will break down how these approaches differ, where and which one to apply, and how Kotlin helps combine their best sides. Get ready: there will be plenty of code and no boredom.

\n\n

If you are learning Kotlin for Android development or backend, understanding these paradigms is your ticket to clean, readable, and maintainable code. Let's go!

\n\n

Object-Oriented Kotlin: Classics in a Modern Wrapper

\n\n

The object-oriented approach is about entities. You have classes, classes have properties and methods. Everything the program does is the interaction of objects. Kotlin takes the best from Java but adds syntactic sugar: data class, object, lazy properties, and much more.

\n\n

Classes and Inheritance: Example

\n\n

Imagine we are writing a system for an online store. The OOP approach would look like this:

\n\n
// Base class\nopen class Product(val name: String, val price: Double) {\n    open fun getDescription(): String {\n        return "$name costs $price rub."\n    }\n}\n\n// Inheritance\nclass DiscountedProduct(name: String, price: Double, val discount: Double) : Product(name, price) {\n    override fun getDescription(): String {\n        val finalPrice = price * (1 - discount / 100)\n        return "$name with a $discount% discount costs ${"%.2f".format(finalPrice)} rub."\n    }\n}\n\nfun main() {\n    val apple = Product("Apple", 50.0)\n    val saleApple = DiscountedProduct("Apple", 50.0, 20.0)\n    \n    println(apple.getDescription())  // Apple costs 50.0 rub.\n    println(saleApple.getDescription()) // Apple with a 20% discount costs 40.00 rub.\n}
\n\n

Everything here is logical: there is hierarchy, polymorphism, encapsulation. But there are also downsides: the code becomes heavy if there are many classes. And if we just need to apply a discount to any product, we would have to create subclasses.

\n\n

When does OOP rule?

\n
    \n
  • Complex business models — when entities have many states and behaviors.
  • \n
  • UI components — Activity, Fragment, View — all of this is classic OOP.
  • \n
  • Working with databases — ORM (e.g., Room) uses reflection and annotations tied to classes.
  • \n
\n\n

Functional Kotlin: Conciseness and Safety

\n\n

Functional programming is about computations, not states. Instead of mutating objects, we use immutable data and pure functions. Kotlin does not force you to write in a functional style, but provides all the tools for it: lambdas, higher-order functions, monad-like constructs (e.g., Result).

\n\n

Higher-Order Functions: Example

\n\n

Let's solve the same discount problem, but functionally:

\n\n
data class Product(val name: String, val price: Double)\n\nfun applyDiscount(discount: Double): (Product) -> Product = { product ->\n    val finalPrice = product.price * (1 - discount / 100)\n    product.copy(price = finalPrice)\n}\n\nfun main() {\n    val apple = Product("Apple", 50.0)\n    val saleApple = applyDiscount(20.0)(apple)\n    \n    println("${apple.name} costs ${apple.price} rub.")           // Apple costs 50.0 rub.\n    println("${saleApple.name} with a discount costs ${saleApple.price} rub.") // Apple with a discount costs 40.0 rub.\n}
\n\n

Notice: we are not creating a new class. We simply pass data through a function

Blogs

Book Recommendations