Proper coding: Implementing static data in your project

Welcome back!

I know it’s obvious we all want to make awesome things. And with amazing new frameworks like ARKit, Vision or Core ML there’s a lot of stuff to discover. But today I want to talk about another topic. I’m coding for almost 2,5 years now, and one of things I found (and find) most complicated is getting the right architecture for my projects. As a beginning programmer I didn’t start off with creating the perfect architectures.  If the app was doing what it supposed to do, I was the luckiest man on the face of the earth. But since I became a bit addicted to coding, I also wanted to do it the right way, with design patterns that make sense. What made it more difficult for me was the fact that I had a hard time finding the right articles and books about this stuff.

Anyway, after a while I got a bit of hang on what’s good and bad practice. And with a little help form my coding friends I also find some good blogs about the topic. To name one, I like this one about SOLID programming principles by Marco Santarossa.

To be totally honest, I still have got a lot to learn. Still I wanted to share some things I have found out, starting today. Let’s begin with a simple one;

What’s a proper architecture for putting some static data in your project?

Say, I have an app which contains some ’Tips & tricks’ brought to you by some Zen master. Our aim is to display this tips in a random way to the user in a viewController. The way to achieve this is actually (which may not be a surprise) quite simple.

  1. We create a struct named Tip which contains the various properties of a tip we want to describe
  2. We create a class TipsHelper to create some actual tips and give them back in a random fashion
  3. We inject an instance of Tip into the VC, where it is used to give content to the views.


Creating the struct

This one is simple. The advantage using a struct and not a class is that a struct does not need initializers. The only question is; what aspects of a tip do we need (which properties should our struct contain)? In this case I figured out a Tip always have a title, some text (the actual tip itself) and optionally an image.


Creating the class

So we have our Tip-struct, but we still we don’t have any actual tips yet. So, let’s get this over with right away. We are going to create a separate class that we name TipsHelper. We create some tips with a tipTitle and some tipText. We don’t use the image-property by filling in nil (remember, the tipImage property is optional, which means we don’t have to provide an image!).

Okay, we have our tips now, but we still don’t have a way in which they can be presented randomly. We need to create a function that gives back a random Tip.


Implementing it in the VC

The only thing left for now is to inject the tips in the viewController in which we need them. In this case we use them to give in the texts of 2 labels.


And that’s all for today. This example is very simple, but I think it gives a very good idea how to separate your code in a proper way. If you have any suggestions for better or alternative ways, please let me know. I’m still learning!

Happy coding!