Best_Coding_Practices

Best Coding Practices

Intro

This is still a document maintained for my personal usage, I’m not advocating anyone that, below mentioned approach or design pattern is the best for solving a coding problem.

It varies from language to language and different background of problems. It doesn’t hurt to learn few different ways of solving the same problem and thus evaluating the efficiency while computing the time and space complexity of a problem solution.

“Good design is half the battle” - Kautilya Save

Still linking a Wiki just for better read. Wiki Coding Practices

Safely Unwrapping

It is always good practice to check for ‘undefined’ / ‘null’ or you could optional chain the variable using ‘if let’ or ‘?’Syntax depending on your target language.

if let optional = class_Name.other_Class_ObjName?.property_Val {  print("Safely unwrapped \(optional).")
}
else {
 print("Unable to retrieve the optional.")
}

Guard usage for unnecessary optional syntax

More example for avoiding unnecessary ? ! or ??

public override func isEqual(_ object: Any?) -> Bool {
 strID == (object as? VSS)?.strID
 && strSignal == (object as? VSS)?.strSignal
}

New Code

public override func isEqual(_ object: Any?) -> Bool {
 guard let vssO = object as? VSS else { return false }
 
 return strID == vssO.strID
 && strSignal == vssO.strSignal
}

Formatting

code_formatting
Code Linter

Swift Specific Tips

best_practices

Pyramid of Doom

Avoiding pyramid of doom with if else indentation.

pyramid of doom

if else if

dangling else ambiguity

Clean Code

Resources

refactoring-swift-best-practices

Authored by : Kautilya Save

GitHub