#3 Swift code refactor in action - price $$$

#3 Swift code refactor in action - a sneaky problem hidden in code snippet

Swift code refactor in action 👨🏻‍💻 Take a close look at the validate function. There’s a sneaky problem hidden in this code snippet. What will the function call return when passed nil? What problem is hidden here? First, the guard statement is redundant here. We can simplify the function to ⤵️ func validate(password: String?) -> Bool { password?.count ?? 0 > 8 } There’s no need to wrap password?.count ?? 0 in parentheses since the ?? operator already has higher precedence than >. ...

April 9, 2025 · 2 min · Maciej Gomolka
#2 Swift code refactor in action - price $$$

#2 Swift code refactor in action - price $$$

Swift code refactor in action 👨🏻‍💻 Today, let’s talk about refactoring for clarity, maintainability, and scalability! This time, the scenario is calculating the final price depending on the price and membership status. This initial code has a few code smells: 1️⃣ nested ifs - impacts general readability, making the code hard to understand, 2️⃣ duplicated conditions - “price > 100” which violates the Don’t Repeat Yourself principle, 3️⃣ lack of scalability - not possible to easily add a new discount, it’d require to rework everything. Checkout the gif or the post on my website to see how I solve these code smells and make the code cool, clean and scalable! ...

December 3, 2024 · 2 min · Maciej Gomolka
#1 Swift code refactor in action - user profile name

#1 Swift code refactor in action - user profile name

Swift code refactor in action 👨🏻‍💻 Common scenario: formatting user profile name - I bet any of you faced this kind of task. At first glance, it look straightforward, but when you take a closer look, you’ll notice two potential improvements: 1️⃣ One single return - simplification of the function flow. 2️⃣ Centralised formatting logic - reduces the chance of bugs. Check out the animated gif and the code where I refactor to address these issues. ...

November 20, 2024 · 2 min · Maciej Gomolka