Swift: Use Interface Protocol just like “.h” file in Objc does

MonGuNare
MonGuNare
Jul 22, 2017 · 4 min read

I am a huge fan of Objective-C language as much as Swift. I totally agree with that using Swift, I can create my code more grammatically just like a novel, which has great advantage for code readability aspect.

Code reading is always fun!

However, one of my favorite feature in Objective-C is, it has separated header file that explains me how the certain class works and provides functions. So whenever I need to use 3rd party module, the first thing I’ll take is open up the header file and look through all functionalities. Unfortunately in Swift, I have experienced many times that I look though all enormous codes in one class file; some of codes that I have to know but most of codes is ignorable.

How can I design a class that also has high readability for those who wants to use it? Here is some suggestion; using //MARK: notation and interface protocol.

Time to coding! yeah!

Let’s do it

Let’s say I’m making a ScheduleReminder that reminds me whole schedules for today every morning. I can set a AlarmingTime which has two integer values, hour and minute. Here is a simple code example;

There are two properties, the schedule lists property as public (actually, default access scope is Internal but let’s say public in here) and alarmingTime as private. Also it has three public methods for add, modify and delete the schedule and two utility methods as private.

Now, I’m done and pass this module to my colleague. What action can be expected when the first time my colleague give this module? Of course, he’ll look through all codes and figure out the available functions he can use.

Fortunately, this ScheduleReminder class has only few properties and methods which is not take so long time reading through all codes, but what if the module has hundred and thousands lines of code? I’m quite sure that we’re having meeting just after 5 minute I gave it to him.

As a perspective of readability, there is one possible solution; put //MARK: notation between public and private functions.

This approach has a several advantage. Putting all functions which have same access scope in together, the other developer can easily find out what function this module provides and doesn’t need to read and understand all codes. The other advantage of this way is, you can understand the module structure at a glance using breadcrumb menu.

However, using //MARK: notation is too fragile as maintenance side. What if the other developer adds a public function at the end of code? What if the modify function is changed to private function during refactoring and what if developer forget to put “private” notation in front of function that has to be private? Do we fix the notation position as well every time? Since a //MARK: notation is a sort of COMMENT, it is a cost for maintain itself.

Another option I can choose is, using protocol to specify all features in a protocol file. I prefer to name it as interface; in this example, it can be named like ScheduleReminderInterface.

For the use case, declare property as ScheduleReminderInterface and initialize it.

Now, I can create a new Swift file and put the protocol codes in there, it will work just like what Objc header file does.

The best benefit of using interface protocol is, the other developers don’t need to care about how long and how complex the module is but simply figure out what they needs. Plus, since ScheduleReminderInterface is a protocol, the class who has this interface is not affected by code changing for ScheduleReminder class at all.

Conclusion

Providing interface protocol is a one way to improve code readability and maintenance. The famous phrase says that “There is no silver bullet”, this approach is not solve every problem perfectly but think though, more cards for architecture we have, better way we can find out for problems I believe.

MonGuNare

Written by

MonGuNare

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade