10 Best Tips to Swift Development from Swift expert

1. For Loop vs While Loop

Example: Print “Count” 5 times

You made the variable “i” to make sure your computer doesn’t break by printing limited numbers.

Listen, more variables → more memorization → more headache → more bugs → more life problems.

Remember the Butterfly effect.

2. Optional Unwrapping

Example: Guard let vs if let

Let’s make a program for welcoming a new user.

Do you see the pyramid of doom? Those are nasty nested code. Never. Destroy the bad, bring the good.

The difference is trendemous. If username or password has a nil value, the pretty code will early-exit the function by calling “return”. If not, it will print the welcoming message.

3. Extension

Example: Square a number

The useless variable was created to double square 6— we need not enjoy typing.

4. Generics

Example: Print all elements in an array

Too many useless functions. Let’s create just one.

5. Functional Programming

Example: Get even numbers

I don’t need to see the entire process. I am wasting my time reviewing how your for-loop looks like. Let’s make it explicit.

Functional Programming is phenomenal.
Functional Programming makes you look smart.

6. Closure vs Func

You need not memorize the name of the function and the variable — You just need one.

7. Computed Property vs Function

Example: finding a diameter of a circle

You created two mutually exclusive functions. Atrocious. Let’s connect the dot between radius and diameter.

Now, the radius and diameter variables are interdependent of each other. More connections → less extra typing → fewer typos → fewer bugs → fewer life problems. 💅

8. Enum to Type Safe

Example: Ticket Selling

“Adult”, “Child”, “Senior” → you are hard coding. You are literally typing all these string values for each case. That’s a no no. I explained what happens when you write too much. We never enjoy typing.

You will never make a typo because “.adult”, “.child”, “.senior” highlight themselves. If the switch statement encountered unknown cases beyond the scope of the designated enum, Xcode would scream with that red error (😡) on the left side. — I just couldn’t find the right emoji.

9. Nil Coalescing

Example: User choosing Twitter theme color

Too long. Let’s cut the fat.

The code above states, if userChosenColor returns nil, choose defaultColor (red). If not, choose userChosenColor.

10. Conditional Coalescing

Example: Increase height if you have spiky hair

Too long. Let’s cut the fat.

The code above states, if hasSpikeHair is true, add 5 to the final height, if not add zero.

Sandeep Joshi

Sandeep Joshi | iOS Developer

I’m Sandeep Joshi - an iOS developer at Yudiz Solutions Pvt. Ltd. - a leading iPhone App Development company. I am ambitious and intensely interested in what I do. You'll quickly find that I do not merely develop software - I develop highly maintainable, elegant code that will save you enormous sums in the long run.
mm

Sandeep Joshi iOS Developer

I’m Sandeep Joshi - an iOS developer at Yudiz Solutions Pvt. Ltd. - a leading iPhone App Development company. I am ambitious and intensely interested in what I do. You'll quickly find that I do not merely develop software - I develop highly maintainable, elegant code that will save you enormous sums in the long run.

Comments are closed.

Top