Instruction

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

Procedural Programming vs. Object-Oriented Programming

You write computer programs to solve problems, and you usually break the problem into smaller parts, taking advantage of the “divide and conquer” principle. Object-oriented programming lets you think about problem-solving in a different way than procedural programming.

In procedural programming, you solve problems by designing procedures (functions) to work on data. Function calls and data can be anywhere in the program. If you need to change a function’s inputs or outputs, you must search the whole program and hope you find everything that you need to update. There’s a greater possibility of duplicated code — another source of errors when you make changes to the code.

In object-oriented programming, objects are the core component: They contain their own data and functions, and they interact with each other. You solve problems by designing objects and their interactions. You’re more likely to reuse code than to duplicate it, and you can more easily find bugs because you know exactly how and where data can be changed.

In the demo, you’ll take a quick look at the difference between procedural programming and object-oriented programming. But before you do that, take a moment to gain some foundational knowledge about structs and classes.

Structs & Classes

Object-oriented programming organizes app development around objects. Most apps manage objects like users and the items they use or create in the app — notes, to-dos, items to buy or sell, images or videos, social connections like followers or followed people etc. Here’s how you create objects:

Principles of OOP

There are four fundamental principles that underlie OOP. Here’s what they are and how they can benefit your development process:

What’s Good About OOP?

So what are the advantages of OOP? Some reasons why OOP is a powerful tool in app development include:

Protocols/Interfaces

To model your data with inheritance, you must use classes. However, you can’t have multiple inheritance, and structs can’t use inheritance at all. Yet, Swift favors structs, and the Swift standard library contains many more structs than classes. How do you create reusable code without inheritance?

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo