Naming Errors #swiftlang

In recent betas, Apple extended ErrorType for structs and classes, as well as enumerations. Even so, enumerations still win for quick-and-simple error reporting:

enum MyErrorEnumeration: ErrorType {case FirstError, SecondError, ...}

So what do you name your errors?

When dealing with trivial applications, feel free to use trivial names.

enum Error: ErrorType {case WrongFile, ItsMonday, IFeelCranky}

But when you expand errors to more significant use-cases, what’s a sound set of rules for naming? I don’t really have any good answers yet, but here are a few thoughts. Please pick these apart and suggest alternatives:

  • Use the word Error in enumeration names. Yes: FileProcessingError, No: FileProcessing
  • Describe the error circumstances in the enumeration case. Yes:FileNotFound, No:Missing.
  • Differentiate cases with associated values, case FileNotFound(fileName: String)
  • Don’t force enumerations when it’s easier to use classes or structs. If you need line tracking, source files, and free-form reason text, consider using a more appropriate type.

Comments are closed.