Klassen Software Solutions

Swift Coding Standards

import Foundation struct PrettyPrintHistory { var items = [PrettyPrintItem]() var currentIndex = -1 mutating func addItem(_ item: PrettyPrintItem) { items.append(item) currentIndex = items.count-1 } func currentItem() -> PrettyPrintItem { return (currentIndex < 0 ? PrettyPrintItem("") : items[currentIndex]) } mutating func next() { if (currentIndex < (items.count-1)) { currentIndex += 1 } } mutating func previous() { if (currentIndex > 0) { currentIndex -= 1 } } }

General Coding Principles

  1. Favour reliability and readability over strict formatting guidelines, but if you choose to break one of our guidelines you must be prepared to justify your decision.
  2. Follow Programming by Contract patterns. Primarily use the precondition method of the standard library.
  3. Write unit tests that cover your code. Your code must pass the unit tests before your pull requests will be accepted. Use the XCTest framework for your tests.
  4. Follow the Swift 5 standard.
  5. Document the public portion of your API using an Xcode compatible markup.
  6. Do not use newlines liberally. In particular, do not use them to make your code "double spaced." Newlines should be used to draw the eye to logically grouped sections of the code.
  7. Eliminate all compiler warnings.

Specific Coding Guidelines

For the most part, follow the default settings that Xcode provides. Don’t fight the tools!

The following documents provide more specific coding guidelines. If you choose to write code that breaks a recommended guideline, be prepared to justify your decision.

  1. Swift API Design Guidelines
  2. Apple's Human Interface Guidelines

In addition, please use the following:

  1. Keep lines to no more than 95 characters in length.1
  2. Keep methods and blocks to no more than 50 lines in length.2

1. Our code reviewers should be able to view two files side by side in a 12pt font on a reasonably sized desk monitor.

2. Developers should be able to see an entire block at once in a 12pt font on a laptop screen.