Writing as little code as possible to accomplish the same tasks was always my goal. Since I’m an iOS developer, doing that with Objective-C was very tricky. However, with Swift it’s a whole new experience.

Imagine when you start working on a project and everything looks great. Then you start implementing line by line, and you soon wake up with a deadline a week ahead of you and find yourself writing way more random code than you'd ever want.

That was the case for me so many times and I always had an excuse of “it’s faster that way, and I’m short on time”.

Well, I grew older and wiser and... it's not.

I want to share with you my favorite best practices that have proven to be very effective in avoiding that situation from happening, making your code reusable and easy to read at the same time:

1 - If you ever copy and paste your code, it means you need a function for that. You can’t even imagine how many times you’ll reuse those functions later on.

Handy Functions

2 - Keep all common UI elements and settings in a separate class for easy reuse, just as the CSS files work on the web.

Default elements

3 - Use MVC to it’s maximum. Everybody knows, and most iOS developers claim to follow the Model-View-Controller pattern, but naming classes appropriately and putting them in corresponding folders isn't enough.

For me, the most tricky part is the controller, since it has control over other parts and it can easily end up with the huge stack of code that does everything. And that’s wrong.

Unlike what most of the tutorials teach, you should, for example, create a separate class to handle the use of a microphone instead of adding it to the controller itself. That way, in your next project, you’ll just drag the file and you won’t have to write a single line of code.

Audio Manager

4 - Use subclassing to its maximum. I always subclass the most used classes in the project, at least to unify behaviour through the app and reuse as much code as possible.

The best example is a simple function that you can call from any view controller in the app to notify the user that an error occurred. If you want to alter the way you present the errors later on, it’ll be a single function to change.

Subclassing

5 - Create initialisers with all mandatory elements to setup the class. It saves a ton of time not to have to think about what you need to add or change for an instance to work properly, and you end up with just a single line of code to do that.

MyPerfectOneLinerClass

6 - Last, but not least: research! The developer community is very supportive, so much so that there is 99% chance the line of code you're about to write has already been written, revised and tested against bugs hundreds of times.

Take advantage of this, as it significantly reduces the number of bugs if you're aware of what can go wrong before writing a single character.

On top of all that, just have fun while doing it and remember: never write the same line of code twice!

Ready for a UX Audit? Book a free call

Found this article useful? You might like these ones too!