When learning a new programming language, you're not just learning syntax, built-in libraries, tooling, terminology, formatting style, and so on. There is also a somewhat vaguely defined idea of what constitutes good code, a way of performing some tasks that fits well with the language and has evolved together with it over time. In Swift, such code is often referred to as Swifty code. This is in no way a well-defined term, and experts in the language may disagree on some points. Here, we will only cover things where there seems to be a consensus. The list is by no means exhaustive, and there are exceptions to many of these.

Many of these points are covered in Apple's official guidelines (https://swift.org/documentation/api-design-guidelines/). We strongly recommend reading it; it's a fairly short page and a very easy read.

Don't put semicolons at the end of lines. That is pointless in Swift. You can, however, use a semicolon to write two statements on one line, but that is not something you should do very often.

Languages without optionals have various ways of signaling the absence of a value: "" for strings, -1 for positive integers, null for objects, and so on. Swift, thankfully, only has one – nil. Always use optionals if a value can be empty.

Use Int for most integers, even if you only need positive values or smaller values that can fit in Int8, Int16, or Int32. Otherwise, you will have to do a lot of conversions since Swift does not do this automatically, not even when it is guaranteed to be safe.

Unless the order is significant, place a parameter taking a closure last in the function definition so that it can be used with trailing closure syntax. Place parameters with default values second to last.

Put underscores in long numeric literals, so they are easier to read:

If you need to change a value after you have returned it, use this code:

Use defer instead: