GDPR 101 for Businesses
Written by Sundaresan Sekar - 25 May 2018 4 minutes readGDPR By now, you must be receiving a flurry of emails from your favorite apps and tools about the updates in their privacy policy and terms and condit...
Type less. Read less. Produce more.
Part 1 of this post covered tips such as basic concepts in coding for a Swift developer beginner. In Part 2, we will take a look at more advanced concepts:
6. Enum to Type Safe:
Ex) Ticket Selling
// Simply Bad
switch person {
case "Senior": print("Pay $7")
case "Junior": print("Pay $3")
case "Kid": print("Pay $4")
default: print("Yo Dude!") }
“Senior”, “Junior”, “Kid” → you are hard coding. You are literally typing all these string values for each case. That’s a complete no no. I explained what happens when you write too much in Part 1. We never enjoy typing!
// Beautiful Code
enum People { case senior, junior, kid }
var person = People.senior
switch person {
case .senior: print("Pay $7")
case .junior: print("Pay $3")
case .kid: print("Pay $4")
}
You will never make a typo because “.senior”, “.junior”, “.kid” highlight themselves.
7. Nil Coalescing:
Ex) User choosing Twitter theme color
// Long Code
var userChosenColor: String?
var defaultColor = "Blue"
var colorToUse = ""
if let Color = userChosenColor { colorToUse = Color } else
{ colorToUse = defaultColor }
Too long. Let’s cut out the extra bits.
// Concise
var colorToUse = userChosenColor ?? defaultColor
The code above states, if userChosenColor returns nil, choose defaultColor (blue). If not, choose userChosenColor.
8. Conditional Coalescing:
Ex) Increase height if you have spiky hair
// Simply Verbose
var currentHeight = 185
var hasSpikyHair = true
var finalHeight = 0
if hasSpikyHair { finalHeight = currentHeight + 5}
else { finalHeight = currentHeight }
Too long, cut out the extra bits.
// Lovely Code
finalHeight = currentHeight + (hasSpikyHair ? 5: 0)
The code above states, if hasSpikeHair is true, add 5 to the final height, if not add zero.
9. Functional Programming:
Ex) Get even numbers
// Imperative (a.k.a boring)
var newEvens = [Int]()
for i in 1...10 {
if i % 2 == 0 { newEvens.append(i) } }
print(newEvens) // [2, 4, 6, 8, 10]
We don’t need to see the entire process. We are wasting our time reviewing how your for-loop looks like. Let’s make it explicit.
// Declarative
var evens = Array(1...10).filter { $0 % 2 == 0 }
print(evens) // [2, 4, 6, 8, 10]
10. Closure vs Func:
// Normal Function
func sum(x: Int, y: Int) -> Int { return x + y }
var result = sum(x: 5, y: 6) // 11
You need not memorize the name of the function and the variable — You just need one.
// Closure
var sumUsingClosure: (Int, Int) -> (Int) = { $0 + $1 }
sumUsingClosure(5, 6) // 11
That’s it! These are some basic tips that can make the life of a Swift developer easier. We hope this post was helpful and informative to all the Swift beginners out there. As Swift evolves as a programming language, tips lists like this will grow too. We look forward to seeing how Swift changes in the years to come and are excited to use more of the language in our apps.
Do you have any tips that made your life easier in Swift? Do share them with us!
Digital Marketing | Cloud Services | Mobile App Development | Web development
GDPR By now, you must be receiving a flurry of emails from your favorite apps and tools about the updates in their privacy policy and terms and condit...
Landing pages are everywhere. Products, Courses and Internet marketers who are tirelessly working on a beach towards making you rich - everyone uses l...
Back in the non-digital era, the marketing teams would need to wait for the Android/iOS application development team to complete the products and hand...