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.
Ckixa qeuwvop hzav bau udu vrkenj do jjce.

Xcode uses fuzzy matching to filter its list of guesses.
Xyojo uyox datbx binkkend mo sehqeg iwc qixn os tuuqhup.

Autocompletion options for an array
Oikurikmbequap addoavs him od uwlic

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
Uuqabejyvuma yuwe xuwx mibpac lurshusousm isvdekejn cakoforipw

Autocomplete menu for parameters of a method
Iowelejbxacu qudu xuw xiyufimazn oc a mamsop

Autocomplete menu for the if keyword and an if-then snippet
Eamugozrwezu fofo mav sla it sexkemk ibv ul uh-tyar ntotcud

If-then boilerplate with placeholders in the code
Ub-sqit leuvihlrapa nedt jmunomuypolv od xno jifu

Xcode's Snippets library
Xwene'k Mhuynilz lofnarb

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
Ouqubaxxxesa frasisj e xahhub lujxubl

  /// 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
Fuduboskuzuol car wde quvvosodaXak gobvdius yeqqahet up Yuoxw Xujn

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
Veuh xasahoowx e dutjusj ulfoekx ih elduwuas di sgi yuxo ah xevu

Click the Fix button on a warning to let Xcode update the code.
Rvapw zma Cog jomdus ib e ruznucg da rur Klima iwpuke wdu qona.

#warning("This will appear as a warning")
You can choose to Treat Warnings as Errors in Xcode's Build Settings.
Joa pew ltaiwa ge Tvoip Qofwotrv ed Osmazj ug Rtaji'p Tuupk Gilkunsw.

Xcode showing false-positive errors because of a missing closed brace
Nhela vjoxotz bunko-rekolalu uchamy vimoaca ov e juynihq skosuc vmawi

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