3 Ways to Name Parameters in Swift Parametrised Tests

3 Ways to Name Parameters in Swift Parametrised Tests

Mind your argument names in Swift Testing’s parametrised tests! As a follow up to my recent post on refactoring to use Swift Testing’s parametrised tests, I’m diving into the crucial - yet often overlooked topic of how to name your parametrised test inputs. Option 1: First Named Tuples Only the first tuple is named, all others rely on positional matching to (a, b, result). ✅ Minimal boilerplate for small input sets ❌ Readability drops after adding more cases ❌ Easy to mix up arguments position ❌ Hard to scan or extend func add(_ a: Int, _ b: Int) -> Int { a + b } ... @Test(arguments: [ (a: 1, b: 2, result: 3), (10, 15, 25), (1, -5, -4), (-1, -5, -6), (0, 0, 0), (1000, 1000, 2000), (10000, 50000, 60000), (-10, -3, -13) ]) func add_returnsCorrectSum(a: Int, b: Int, result: Int) { #expect(add(a, b) == result) } Option 2: Named Tuples Every tuple entry explicitly names all its fields (a: …, b: …, result: …) for all cases. ...

April 24, 2025 · 3 min · Maciej Gomolka
Swift Testing Challange - Can you refactor this?

Swift Testing Challange - Can you refactor this?

Intro Have you already started using Swift Testing instead of XCTest? I’m curious to see how you can refactor the test function (add_returnsCorrectSum) from the code snippet to use all powers of the Swift Testing framework. Could you explain what benefits does your refactored version has compared to my original code snippet? My approach In both approaches the test gives the same result. The point is the refactored version uses a Swift Testing parametrised test, and this makes a real difference. ...

April 17, 2025 · 2 min · Maciej Gomolka
#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
Do You Know the Difference Between Imperative, Functional, and Reactive Programming?

Imperative, Functional, Functional Reactive: Do you know the difference?

Paradigms Imperative In the imperative approach, we have a sequence of instructions that describe step by step how the program’s state is modified. Let’s look on the example ⤵️ var value = 0 func increment() { value += 1 // Mutating the state } print(value) // 0 increment() print(value) // 1 In the code we have a mutable variable value and a function increment that mutates the state (value). We first print the initial value, then increment it, and finally print the updated value. ...

February 25, 2025 · 5 min · Maciej Gomolka

Imperative, Functional, Functional Reactive

Do You Know the Difference Between Imperative, Functional, and Reactive Programming? Test your understanding of imperative, functional, and functional reactive programming with this interactive quiz. These questions cover the key concepts from the main post to help you solidify your knowledge for your next interview. --- primary_color: green secondary_color: lightgray text_color: black shuffle_questions: false shuffle_answers: true --- # What is functional programming about? 1. [ ] Writing functions that may modify state as needed 1. [x] Avoiding side effects and state mutation 1. [ ] Relying on side effects to manage state transitions 1. [ ] Emphasizing object-oriented design patterns # In functional programming, why must functions avoid side effects? 1. [ ] To improve performance 1. [x] To prevent unpredictable state changes 1. [ ] To enable state mutation 1. [ ] To allow recursion # Which paradigm describes a sequence of instructions that modify the program’s state step by step? 1. [ ] Functional 1. [ ] Functional Reactive 1. [x] Imperative 1. [ ] Declarative # What is considered a side effect in a function? 1. [ ] Returning a computed value 1. [x] Modifying a variable outside its scope 1. [ ] Calling another pure function 1. [ ] Using recursion # How is state typically managed in functional programming? 1. [ ] By mutating global variables 1. [x] By transforming old state into new state 1. [ ] By ignoring state changes 1. [ ] By centralizing state in a single object # Which pattern is used by functional reactive programming? 1. [ ] Singleton 1. [ ] Factory 1. [ ] Delegate and Callback 1. [x] Publisher and Subscriber # What is the primary benefit of using pure functions? 1. [ ] They allow state mutations 1. [x] They are easier to test and debug 1. [ ] They increase code complexity 1. [ ] They require global variables # Examine the code below. Does it fully follow the functional programming principles? ```swift var value = 0 func increment(_ value: Int) -> Int { value + 1 } value = increment(value) print(value) ``` 1. [ ] Yes, it's correct 1. [ ] No, the function has side effects 1. [x] No, it mutates state by reassigning a mutable variable 1. [ ] No, it mutates the "value" variable inside the increment function Didn’t get the full result? No worries! All the answers are in the blog post HERE ...