Swift

Swift

Made by DeepSource

Always specify message when invoking fatalError() SW-R1021

Anti-pattern
Major

When invoking fatalError() in Swift, it is essential to provide a descriptive message to help with debugging and understanding the cause of the crash. This is because fatalError() is essentially an assertion that always fails, and the message provided helps identify the source of the failure.

If a message is not provided, it can be challenging to track down the issue that caused the crash. The developer would have to rely on the stack trace, which is often not sufficient to identify the root cause of the problem.

Additionally, providing a message helps with communication and collaboration between developers. If a codebase has many fatalError() calls, it can be much easier for other developers to understand the intent of the code if each call includes a descriptive message.

To fix this issue, always specify a message when invoking fatalError(). Here's an example:

Bad Practice

fatalError()

Recommended

fatalError("Unexpected nil value encountered")