The State Pattern

What is a State Pattern? The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class. Source What problems does it solve? Complex conditional logic: When an object’s behavior depends on its internal state, it often leads to complex conditional statements. The State pattern simplifies this by encapsulating each state and its behavior in separate classes, making the code more readable and maintainable....

March 10, 2024 · 2 min · Dmytro Chumakov

The Dependency Inversion Principle

What is a Dependency Inversion Principle? The Dependency Inversion Principle means that high-level modules should not depend on low-level modules. Source Source What problems does it solve? The Dependency Inversion Principle (DIP) helps solve: Rigidity Fragility Immobility problems Real-world code example Violation of DIP // High-level module directly depending on low-level modules class MessageService { func sendMessageViaEmail(message: String) { let emailSender = EmailSender() emailSender.sendMessage(message: message) } func sendMessageViaSMS(message: String) { let smsSender = SMSSender() smsSender....

March 5, 2024 · 2 min · Dmytro Chumakov

Big O notation

What is a Big O notation? The Big O notation helps identify algorithm efficiency. It can measure computation and memory growth with respect to input. Real-world code example O(n) — Linear Time func containsValue(array: [Int], value: Int) -> Bool { for element in array { if element == value { return true } } return false } O(1) — Constant Time func findFirstElement(array: [Int]) -> Int? { return array.first } Thank you for reading!...

February 21, 2024 · 1 min · Dmytro Chumakov

Combine — Basics

What is Combine? Combine Framework provides an API for processing async events over time such as user-input, network response, and other dynamic data. What is the purpose of Combine? The purpose of Combine is to simplify the management of async events and data streams. Publishers Publisher declares that a type can transit a sequence of values over time. A publisher delivers elements to one or more Subscriber instances. class PostService { func fetchPosts() -> AnyPublisher<[Post], Error> { guard let url = URL(string: "https://jsonplaceholder....

February 7, 2024 · 4 min · Dmytro Chumakov

Modern Concurrency

When was it introduced? It was introduced in Swift 5.5 at WWDC 2021. You can find the more comprehensive info about Modern Concurrency in Swift Concurrency Manifesto. What are actors? Actors eliminate shared mutable state and explicit synchronization through deep copying of all the data that passed to an actor to a message sent and preventing direct access to actor state. Actors are reference types. actor DatabaseManager { private var data: [String: String] = [:] func readData(key: String) -> String?...

February 4, 2024 · 4 min · Dmytro Chumakov