Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build an Interactive Story App with Swift 2 Creating a Story Helper Methods

Alexander Karan
PLUS
Alexander Karan
Courses Plus Student 15,220 Points

Why did we extend the class Page

Why did we extend the class Page, could we have just not put the functions that we wrote into the Page class. Or I'm i missing something about extensions?

1 Answer

Pasan Premaratne
STAFF
Pasan Premaratne
Treehouse Teacher

Great question actually! It's mostly a matter of preference on how to organize code into groups of functionality. You can put the functions in the original type declaration, absolutely nothing wrong with that. But some people (me included) prefer to create small extensions to group functionality, particularly if that functionality is defined by a protocol.

For example, let's say you had a class, User, that conforms to several protocols like PersonType, AdministratorType, Billable, JSONDecodable, Archivable and Unarchivable.

If you added protocol conformance for all these protocols and put all the methods in the original declaration, for starters your type declaration would look like this:

class User: PersonType, AdministratorType, Billable, JSONDecodable, Archivable, Unarchivable {}

It'd be really difficult to understand which code in User provides implementation for which protocol. On the other hand if we used an extension to group the functionality, it makes our code much more readable. The original type, with information that defines it's identity, exists on its own.

All other code related to protocol implementation would go into its own extension. For ex:

extension User: PersonType {}

extension User: Billable {}

It's purely a matter of keeping your code more readable and organized.

Alexander Karan
Alexander Karan
Courses Plus Student 15,220 Points

Thank you, i thought i was doing something wrong. There seems to be many way of doing things however would i be right in saying the key point is to not tightly couple objects. So each object deals with one thing.

Pasan Premaratne
Pasan Premaratne
Treehouse Teacher

Actually, an extension isn't a separate object. When your code is compiled, it's all grouped together more or less. In this particular example, we're adding functionality to a type we created so we could add any functionality to the type declaration itself.

In general yes your point is absolutely correct. However the goal here is more about readability since it's the same object anyway. If that makes sense