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

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

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.

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”.

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. Give this project a few months of a break and it will be easier for you to write the thing from scratch than to try to understand “what the poet had in mind”.

To (try to) ensure the inputs and outputs stay at least a bit clear, you will need to add comments to explain the details of what you expect those Strings to actually contain.

From the above function definition alone we only know we’re looking for some identifier. We may understand from the context of the app what that may be, but what is the result of this function exactly?

Will it give us some kind of person’s name? Or maybe a large chunk of JSON data? Maybe a specific number of animal emojis such as a 🐐 and a 🐠? For no reason. But a String can be all those, so make sure that your code will handle every possible case now. I think you see my point.

In reality, nobody knows what those are until they check the actual code of the function.

We have powerful IDEs (such as Xcode), which provide developers with previews of possible completions. They are useless in this case. Imagine what you would think if you were working on someone else’s code and you stumbled upon the above definition as an Xcode suggestion. My guess is: some rude words will be included in your reaction.

You can’t understand what is happening until you check the code or documentation. And chances are, if you see this kind of String usage, you won’t find any docs for that package or pod or repository. *And also, most docs can’t be trusted because we don’t like to write docs, maintain docs and keep up with the changes in the code. *There is nothing that would force developers to keep the docs and comments in sync.

swift swift-programming programming-languages ios-app-development programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Top Swift iOS App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Best Swift App Development Company

Apple Swift App Development India: Mobile App Development India is an expert iOS Swift App Development company in India. We develop secure and robust mobile app solutions based on new Apple Swift language.

Hire iOS App Developer

Are you looking to transform your idea into an iPhone application? Hire iPhone programmer team from **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** to ensure the best results, utilizing all the latest trends in iOS app...

Top iOS App Development Company in USA

AppClues Infotech is a top iOS mobile app development company in USA offering custom iPhone app development services for startups & enterprises of all sizes.