Simple tips for coding better (Swift iOS)

Camilo López
3 min readAug 15, 2017

--

Hello everybody, today I’ve decided to write about a topic that I think it’s pretty important “tips for coding better”.

I think this is important because Swift give us a lot of interesting tools in order to avoid mistakes during the create apps process, but most of us (at the beginning) usually don’t use them. And believe me they will save a lot of problems.

No more tears please.

Avoid Strings — Use Enums

If you have use another languages probably know what a constant is, if you don’t:

In computer programming, a constant is a value that cannot be altered by the program during normal execution

Enums is the way to keep organized all your constants, for example:

enum Countries:String {
case usa
case colombia
case mexico
case germany
}

And now, I ask you to think how to compare a string variable in oder to show the right flag.

let country = "colombia"if country == "Colombia"{
}
if country == "Columbia"{
}
if country == "Deutschland"{
}

This process can produce a lot of mistakes in your app, because normally developers could make a mistake while they are typing.

Enums to the rescue

let country = "colombia"if country == Countries.colombia.rawValue{
}

With the code above you will be pretty sure that you are typing rightly your conditional.

Create classes oriented to reusability

Although, this is probably the most obvious advise ever, it’s okay to say it again: Reusability

Nobody likes long code files, they are boring, hard to reading and actually it is annoying to work in a file when everything it’s totally messed up.

If you have something like this:

override func viewDidLoad() {     
super.viewDidLoad()
let shadowPath = UIBezierPath(rect: self.view.bounds)
let borderView = UIView()
borderView.frame = self.bounds
borderView.layer.masksToBounds = true
borderView.layer.shadowColor = color
borderView.layer.shadowOpacity = shadowOpacity
borderView.layer.shadowOffset = shadowOffset
borderView.layer.shadowRadius = shadowRadius
borderView.layer.shadowPath = shadowPath.cgPath
self.addSubview(borderView)
}

Probably you could change for something like this:

override func viewDidLoad() {     
super.viewDidLoad()
createBorderView()}func createBorderView(){ let shadowPath = UIBezierPath(rect: self.view.bounds)
let borderView = UIView()
self.createBordersToAView(//paremeters here)
self.addSubview(borderView)
}func createBordersToAView(color:CGColor, shadowOpacity:Float, shadowOffset: CGSize, shadowRadius:CGFloat){ borderView.frame = self.bounds
borderView.layer.masksToBounds = true
borderView.layer.shadowColor = color
borderView.layer.shadowOpacity = shadowOpacity
borderView.layer.shadowOffset = shadowOffset
borderView.layer.shadowRadius = shadowRadius
borderView.layer.shadowPath = shadowPath.cgPath

}

Use Extensions

Extensions are probably the most important feature for reausability in Swift.

I’ll show you how to save time and reuse all your code with the same example:

You can put this code in your project (not inside a class)

extension UIView{
public func setShadowsContainer(color:CGColor, shadowOpacity:Float, shadowOffset: CGSize, shadowRadius:CGFloat){
let shadowPath = UIBezierPath(rect: self.bounds)
let borderView = UIView()
borderView.frame = self.bounds
borderView.layer.masksToBounds = true
borderView.layer.shadowColor = color
borderView.layer.shadowOpacity = shadowOpacity
borderView.layer.shadowOffset = shadowOffset
borderView.layer.shadowRadius = shadowRadius
borderView.layer.shadowPath = shadowPath.cgPath
self.addSubview(borderView)
}
}

And finally, your code will look like:

override func viewDidLoad() {     
super.viewDidLoad()
createBorderView()}func createBorderView(){let shadowPath = UIBezierPath(rect: self.view.bounds)
let borderView = UIView()
borderView.setShadowsContainer(//paremeters here)
self.addSubview(borderView)
}

--

--

Camilo López

A Web developer and currently focused on Mobile Development Swift/Java