#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
#5 XCTest vs. Swift Testing - Conditional disabling - when a test needs a nap

#5 XCTest vs. Swift Testing - Conditional disabling - when a test needs a nap

Today we check the diff in conditional disabling. In XCTest there is XCTSkipIf function that takes Bool argument to decide whether a test should run or not. In Swift Testing there’s ā€œdisableā€ trait accepting Bool argument and behaving like XCTSkipIf from XCTest. XCTSkipIf - are you surprised this kind of function exists? To be honest - I was I can admit I learned about it when preparing this post. This already shows how often I’ll be using the Swift Testing version of it, but never say never! ...

November 28, 2024 Ā· 1 min Ā· Maciej Gomolka
#4 XCTest vs. Swift Testing - Disable tests - handle with care

#4 XCTest vs. Swift Testing - Disable tests - handle with care

This week with Swift Testing starts with checking how test disabling differs from XCTest. In XCTest, Xcode identifies a function as a test only if its name starts with the ā€œtestā€ prefix, so putting e.g. ā€œdisabledā€ instead makes the test inactive. Swift Testing simplifies that approach by introducing the @Test macro with a .disabled trait that you can pass as an argument. What’s the benefit? You no longer need to modify each test name to disable it. What’s more, you can include context directly within the trait to justify why the test is disabled. ...

November 27, 2024 Ā· 2 min Ā· Maciej Gomolka
Two types of Swift macros

Two types of Swift macros

What is macro? šŸ’” Macro is a feature that generates code during compilation. Unlike macros in C, which work like ā€œfind and replaceā€, Swift macros are type-safe and context aware, making them powerful tools reducing boilerplate code. Two types of macros attached - use @ prefix, tied to a declaration adding extra logic to it, like: @Test, @Model, @Observable freestanding - use # prefix, standalone code that can be invoked independently as a part of the code, like #expect, #Predicate, #warning Example of attached macro ā¤µļø ...

November 26, 2024 Ā· 1 min Ā· Maciej Gomolka
#3 XCTest vs. Swift Testing - Unwrapping optionals

#3 XCTest vs. Swift Testing - Unwrapping optionals

Optionals are a core of Swift - we deal with them daily, both in production and testing code. Whether you write tests with XCTest or Swift Testing, unwrapping optionals is a common case. In XCTest there is XCTUnwrap operator. Swift Testing introduces #require macro. Is there any difference between them? Not really! Both require the test function to handle exceptions and try keyword before. Gif ā¤µļø Code ā¤µļø XCTest ...

November 21, 2024 Ā· 1 min Ā· Maciej Gomolka