Swift 5: Why Is Protocol-Oriented Programming Essential
Hi iOS Developers,
Do you know what a “God Class” is? It’s a class that has tons of different categories of functions and variables in one huge file. Your apps likely have some “God Classes” in them and you didn’t even know it!. You still have no idea about what a “God Class” is? Are you aware that it could destroy your codebase? Continue reading and I’ll teach you how to kill these evil things.
What’s a “God Class”?
It usually exists in the Controllers, if you are working with the MVC design pattern, and in the ViewModels, if you are working with the MVVM design pattern. Here’s see an example of a classic “God Class” in the MVVM design pattern.
Let’s say you are building a dating app. You probably will need a UserViewModel
to store data and perform the business logic.
struct User {
let id: UUID
var name: String?
var phone: String?
var email: String?
}class UserViewModel {
private(set) var user: User?
init(id: String) {
self.user = fetchUser(with: id)
} func fetchUser(with id: String) -> User? {
//fetch user data from the CoreData and optionally return a User object
} func verifyPhone() {
if…