The Proper Way to Structure Your Swift Code

Structure your Swift code in ViewControllers with Burger Code Method

Emin Emini
3 min readNov 26, 2021
Illustration by The Simple Designers on Behance

Have you ever had to continue a pre-started project and found it messy? Everything was messed and unorganized. If you’re coding for a long time now, you probably went through that, and we can feel each other. Even if you don’t, then this article can help you keep writing a nice organized code.

Let me introduce you to a method I call Burger Code.

Why Burger Code? Ok, first let’s elaborate on the burger. Everyone knows what the burger is. There are two buns, patty (or some patties), and cheese or salad.

Illustration by Minna So on Dribble

So:

Bun
Cheese/Salad/Sauce
Patty (Patties)
Bun

And yeah, sometimes some fries or soda.

But, what has to do code with a burger?

Ok, we have Outlets, Properties, Functions of Views, Actions, and different functions.

Illustration by The Simple Designers on Behance (modified by me)

Outlets

Outlets and Actions are the same, cause if you have a UIButton, in your ViewController, you probably will have an Outlet and Action of the same Button. So, you keep them in one class, Outlets at the top, Actions at the bottom. You wrap “everything” between these two.

Properties

Under Outlets, you put properties if you have any.

View Functions (Life Cycle)

The main part comes here, which no View Controller can work, viewDidLoad(). So, viewDidLoad() is the pattie of the burger. Without it, a burger is not a burger, it’s the same in View Controllers.

And yeah, you can have a double pattie burger or triple. So, that means you can have viewWillAppear(), viewDidDissapear(), etc…

Actions

To make it visually clear to you, I intentionally put Actions again at the end, cause they come at the end of the class, like a bun.

Other Functions

What are ‘other functions’? They can be any kind of functions besides ‘view funcs’ and ‘action funcs’. It can be, TableView functions, CollectionView functions, etc.
Why do I mark them as Soda or Fries?
Cause we don’t mix them with the burger. So, we put them separated from the class, and we put them on an extension.
In this way, we keep our ViewControllers nice and clean. Also, you can put extensions on separate files if you wish, you just keep them aside from your class.

Practice

Enough of theory and explanations, let see it in practice.

And yes, please use comments as I did. You don’t have to mention (Bun, Pattie, Fries, etc.). Just use // MARK: — Outlets, // MARK: — Actions, and so on.

Hope this helped you, and you loved it. Also, sorry if I made you hungry 💙

If you want to see more articles like this, please make sure you clap it and share it 👏🏼.

Tweet this article 🐦.

--

--

Emin Emini
Emin Emini

Written by Emin Emini

Skilled Software Engineer & Team Lead with 12+ years in industry. Passionate about tech (AI, ML, Apple), outdoor adventures, and continuous self-growth.

Responses (14)