Swift: Dirty Code vs. Clean Code

Halil Özel
3 min readAug 18, 2024

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

Swift: Dirty Code vs. Clean Code

What is Dirty Code? 💩

  • Poor readability: It’s hard to understand the code’s intent without significant effort.
  • Lack of structure: Code is haphazardly organized, making it difficult to follow the logic.
  • Inefficient: The code performs tasks in a suboptimal way, leading to performance issues.
  • Hard to maintain: Changes are challenging to implement without introducing new bugs.
  • Overly complex: Unnecessary complications make the code harder to comprehend.

Examples of Dirty Swift Code:

let x = a + b * c - d / e
let result = x > 0 ? "Positive" : "Negative"

This code is technically correct, but it lacks readability. Using meaningful variable names and breaking down complex expressions would improve clarity.

What is Clean Code? 🧼

  • Readable: It’s easy to understand the code’s purpose at a glance.
  • Well-structured: Code is organized logically, making it easy to follow.
  • Efficient: The code performs tasks…

--

--