small medium large xlarge

Swift Style, Second Edition: An Opinionated Guide to an Opinionated Language


Cover image for Swift Style, Second Edition

Swift Style, Second Edition

An Opinionated Guide to an Opinionated Language


Discover the dos and don’ts involved in crafting readable Swift code as you explore common Swift coding challenges and the best practices that address them. From spacing, bracing, and semicolons to proper API style, discover the whys behind each recommendation, and add to or establish your own house style guidelines. Refreshed to cover Swift 4 and 5, this practical, powerful, and opinionated guide offers the best practices you need to know to work successfully in this equally opinionated programming language.

Customer Reviews

This is one of the most important resources on the language. A must-have on every
Swift programmer’s desk.

- Paris Buttfield-Addison

Founder, Secret Lab Pty. Ltd.

Swift Style, Second Edition is an opinionated style guide that gives readers a clear
road map of best practices they should be following in their own projects. This is
a must-have for any Swift development team, as it touches subjects and arguments
for styling that can dramatically improve any project.

- Grant Isom

Team Lead, Associate Senior Software Engineer, Cerner Corp.

One of the books I always have with me, this gold mine taught me to be a better
programmer, collaborator, and critic of Swift code. Also great for winning arguments.

- Mars Geldard

Author, "Practical Artificial Intelligence with Swift"

Style is inherently subjective, so it is unlikely you will agree with all of the recommendations
this book contains. Fortunately, that is not the goal of Swift Style, Second Edition. Instead, it takes a close look at all of the details and considerations
that go into intentionally and thoughtfully choosing your own personal or house

- Matthew Johnson

Owner and Principal Software Engineer, Anandabits, LLC

See All Reviews

Choose Your Format(s)

  • $21.95 In Stock
  • Ebooks are DRM free.

  • Ebook delivery options.

About this Title

Pages: 241
Published: 2019-03-26
Release: P1.0 (2019-03-26)
ISBN: 978-1-68050-627-3

Apple’s Swift programming language has finally reached stability, and developers are demanding to know how to program the language properly. Swift Style guides you through the ins and outs of Swift programming best practices. Refreshed for Swift 4 and 5, this is the best practices book for serious, professional Swift programmers and for programmers who want to shine their skills to be hired in this demanding market.

A style guide offers a consistent experience of well-crafted code that lets you focus on the code’s underlying meaning, intent, and implementation. This book doesn’t offer canonical answers on Swift coding style. It explores the areas of Swift where structure comes into play. Whether you’re developing a personal style or a house style, there are always ways to enhance your code choices. You’ll find here the ideas and principles to establish or enhance your own best style practices.

Begin with simple syntactical styling. Perfect spacing and layout. Master literal initialization and typing. Strengthen code bracing for easy readability. Style your closures for safety and resilience. Optimize control flow layout and improve conditional style choices. Transition from Objective-C and move code into Swift the right way. Boost API design using proper naming and labeling. Elevate defaulted arguments and variadics to their right places. Finally, Erica offers her own broad recommendations on good coding practice.

What You Need

A recent version of the Swift programming language.


Contents & Extracts

  • Welcome to Swift Style
    • How This Book Got Here
    • What’s in This Book
    • Contributing to This Book
    • Online Resources
    • Language Evolution
    • Ready to Get Going?
    • Credits
    • Thanks
  • Adopt Conventional Styling
    • Reaching for Consistency
    • Understanding Inattentional Blindness
    • Adopting Conventional Spacing
    • Mandating Maximum Line Widths
    • Selecting Colon Styles
    • Placing Attributes
    • Moving Variables and Constants to the Left
    • Formatting String Literals
    • Formatting Number Literals
    • Balancing Inferred and Explicit Typing
    • Constructing Collections with Literals
    • Optional Sugar
    • Mitigating Optional Constipation
    • Converting to Tuples
    • Considering Comma-First Styles
    • Wrapping Up
  • Structure Your Code for Readability
    • Taking Control of Swift Structure
    • Understanding Swift Semicolons
    • Structuring Single-Line Clauses
    • Hugging Parentheses
    • Wrapping Argument Lists
    • Pushing Declaration Argument Parentheses Leftward
    • Coaligning Assignments
    • Improving Closure Hygiene
    • Choosing Trailing Closures
    • Laying Out Partial Application
    • Laying Out Complex Guard Statements
    • Laying Out Ternaries
    • Binary Conditionals
    • Laying Out Long Collections
    • Weighing Late Property Declaration
    • Wrapping Up
  • Establish Preferred Practices
    • Testing Assumptions
    • Choosing Optionals
    • Casting Conditionally
    • Chaining Calls
    • Moving from Thrown Errors to Optionals
    • Unwrapping Variables
    • Distinguishing flatMap from compactMap
    • Mapping in Condition Clauses
    • Iterating Collections of Optionals
    • Working with Optional Collections
    • Adding Lazy Evaluation
    • Selecting Sequences and Strides for Numeric Progressions
    • Looping
    • Indexing and Enumerating Collections
    • Switch Statements
    • Declaring Number Constants and Variables
    • Implementing Getters and Setters
    • Returning Void
    • Grouping Initializers
    • Using Call Site Type Inference
    • Evaluating Case-Binding Syntax
    • Using if/guard-case
    • Choosing Capture Modifiers
    • Toggling Boolean Values in Place
    • Testing Sequences for Boolean Logic
    • Double-Testing Boolean Logic
    • Boolean Optionals
    • Using Division Logic
    • Other Practices
    • Wrapping Up
  • Design the Right APIs
    • Adopting Access Control
    • Avoiding Global Symbols
    • Nesting Functions
    • Nesting Types
    • Designing Singletons
    • Adding Custom Operators
    • Naming Generic Parameters
    • Naming Symbols
    • Plurality
    • Choosing Label Names
    • Initializers
    • Convenience Initializers
    • Factory Methods
    • Naming Methods and Functions
    • Tips for Naming
    • Mutating Variations
    • Computed Properties vs. Methods
    • Adding Defaults
    • Protocols
    • Generic Beautification
    • Adding Typealiases
    • Choosing Value vs. Reference Types
    • Writing Good Errors
    • Wrapping Up
  • Look to the Past and the Future
    • Reconciling Past You vs. Future You
    • Documenting in Real Time
    • Adding Structured Markup
    • Commenting Well
    • Organizing with Bookmarks
    • Removing Boilerplate Code
    • Addressing Consistent Fatal Outcome
    • Improving Code Descriptions
    • Avoiding Clever
    • Wrapping Up
  • Good Code


Erica Sadun enjoys deep diving into technology and has written, co-written, and contributed to dozens of books about computing and digital media. Erica has blogged at TUAW, Ars Technica, O’Reilly, and Lifehacker, and has (to date) authored or co-authored more Swift proposals than anyone, including everyone on Apple’s Core Team.