Member-only story
Kotlin: Dirty Code vs. Clean Code
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.
Press enter or click to view image in full size![]()
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.
Press enter or click to view image in full size![]()
This code, while functional, exhibits several issues:
- Default number:
0.0is a default initial value without clear meaning. - Mutable variable:
totalPriceis 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.
