Other Practices

Here are some handy ways to help optimize and improve your code:

  • Prefer let constants to var variables unless you truly need mutable data. The compiler can better optimize your data when it’s guaranteed not to change. The Swift compiler is very good at detecting unmodified var use.

  • Marking classes as final enables the compiler to introduce performance improvements. These improvements depend on moving away from dynamic dispatch, which requires a runtime decision to select which implementations to call. Removing indirect calls for methods and property access greatly improves your code performance.

  • Swift’s whole-module optimization can automatically infer many final declarations by scanning and compiling an entire module at once. ...

Get Swift Style now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.