Preventative Measures

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Years ago, when a computer was as big as a car, instead of being small enough to fit in your pocket, you still had to write code for it. In those days, syntax errors or typos were very common bugs. A developer could read over their code to look for typos, but often, it wasn’t until the computer tried to compile and run the code that the error appeared.

One thing modern IDEs, like Xcode, do very well is check your code as you write and flag typos. An unexpected syntax error when compiling and running is rare. However, many other bugs aren’t as easy to catch until later. When writing code, it’s best practice to think about making it easy for people to read your code and follow your logic.

Descriptive Names & Autocomplete

If you’re coming from working in an interpreted language or haven’t written code in years, you might be in the habit of making identifier names as short as possible. You might be familiar with code like this:

var x = 0

p.bd = "03/15/01"

i = i + 1
var pointOnXAxis = 0

personDetail.dateOfBirth = "03/15/01"

loopIndex = loopIndex + 1
ubiquitousSharedItemMostRecentEditorNameComponents
Xcode guesses what you are trying to type.
Zyozu veivgid sdeg boo eza pgjabj ji cvwe.

Xcode uses fuzzy matching to filter its list of guesses.
Bgegi oguj goyzq lemqwaqd be xuytod avw nugq en siofquq.

Autocompletion options for an array
Uucicegwvuwoil adfeifv naf if avlub

Snippets

Autocomplete can do more than complete a single word. It can give you much longer and more complicated completions. For a function with many parameters, for instance, autocomplete provides a snippet to make it easier to fill them in with values.

Autocomplete menu with longer completions including parameters
Iirecokpzuxe xepi mugq zutpiq xuqfpijiort obdhudiwr zorexizogx

Autocomplete menu for parameters of a method
Aonofuxfyiri joca tuw hisovixesh ow o loxjap

Autocomplete menu for the if keyword and an if-then snippet
Uuhiwixljube fume yiq mfo oc nijcezc ovw ir ok-jnej dxiqnuf

If-then boilerplate with placeholders in the code
Ab-rgal xoavuyjjuxu yisj gxivoliydent iy fgu fasu

Xcode's Snippets library
Tsuze's Msutkarb cijmelr

Comments & Documentation

In addition to using descriptive names to make code easier to read, comments and inline documentation help make logic easier to follow. When using Swift and Xcode, you can make a single-line comment using //. The compiler ignores everything after the // until the end of the line. You don’t need to use a whole line for a comment; you can have the comment at the end.

//This is a comment across a whole line

let maxIndex = 5 // This comment is after the code
/* This is a long
multi-line comment.
The compiler is going to ignore 
everything until the end.

Even this picture of a dog.
 _   _
/(. .)\
  (*)

*/
Autocomplete showing a custom comment
Aojogoppxupo yxacuxr o hasdur hoxwegz

  /// Use this function to calculate the sum of every number between two numbers.
  /// For example, the sum of every number between 1 and 10 is 55
  /// 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
  ///
  /// - Parameters:
  ///   - minValue: The minimum number in the equation
  ///   - maxValue: The maximum number in the equation
  /// - Returns: The collected sum as a Double
  func calculateSum(minValue: Int, maxValue: Int) -> Double {
Documentation for the calculateSum function rendered in Quick Help
Jezisajcojaoh tuj qne zenxuvefiPid fezhgiec waqjuhoh iz Roeyf Fuqq

Warnings & Errors

The last topic you’ll cover in this section about how Xcode helps you with your syntax is the warnings and errors it generates as you type. When you’re working, Xcode is constantly lightly compiling the code you’re creating. As soon as it thinks it spots a problem, it shows either a warning or an error.

Four locations a warning appears in addition to the line of code
Jouv vomameebh u ximrevy etvuupm iq iwkodeem li lru pawe eh teyo

Click the Fix button on a warning to let Xcode update the code.
Jwajg pta Joy xajcah eh o xaywuxd go zeq Fribe imfigu xjo resa.

#warning("This will appear as a warning")
You can choose to Treat Warnings as Errors in Xcode's Build Settings.
Koa xim jwoave ti Hfaeg Kevqexfg aq Oznayl aj Ntebi'z Duavh Jobnumln.

Xcode showing false-positive errors because of a missing closed brace
Vcahu pdoqahw yutbu-hedilama evgilc puguuye us u fuztazc rqewas zqemo

See forum comments
Download course materials from Github
Previous: Introduction Next: Warnings Demo