4 Fun Reasons to Avoid Using “Primitive” Swift Types in Your Code

It may be tempting to simply use Int and String everywhere, but this can quickly turn against you.

Paul Pela
The Startup

--

Here is a list of four fun reasons you should avoid using data types such as Int and String to pass along and process the data in your Swift code. And a list of alternatives to use instead, so you can write much better code with just a few small changes to your current habits.

Note: I’ll troll around a bit because technical articles can be boring.

Note that I use the word Fun in a slightly sarcastic way, as in: “Losing is Fun”.

Photo by Josh Marshall on Unsplash

1. Primitive types aren’t documenting your code

When you see a function definition like this one, do you instantly know what both of the String will actually be?

func getData(for identifier: String) -> String {

Here, String can be anything, both the input String and the output String. If you’re developing the code alone, you will probably remember what is the typical content of those two.

I say probably, because as any developer knows — by Monday you may not remember what the code written on Friday does. It’s something I can promise you…

--

--

Paul Pela
The Startup

Future dad, 9to5: tech support agent. I write about the User Experience of learning programming.