Kotlin: Dirty Code vs. Clean Code

Halil Özel
3 min readJul 31, 2024

Hey Folks, Today we will see with examples how to write dirty and clean code in Kotlin. We can write cleaner codes to improve ourselves and take a step forward.

Kotlin

Dirty 💩 Code: A Code Smell 👃

Dirty code is often characterized by:

  • Poor readability: Difficult to understand the code’s purpose.
  • Lack of maintainability: Hard to modify or extend without introducing bugs.
  • Inefficiency: Performs poorly in terms of speed or memory usage.
Kotlin | Dirty Code

This code, while functional, exhibits several issues:

  • Default number: 0.0 is a default initial value without clear meaning.
  • Mutable variable: totalPrice is mutable, potentially leading to unexpected behavior.
  • Imperative style: The code is procedural rather than declarative.

Clean 🧼 Code: A Breath of Fresh Air 💦

Clean code is:

  • Readable: Easy to understand its intent.
  • Maintainable: Simple to modify and extend.

--

--