small medium large xlarge

Swift Style: An Opinionated Guide to an Opinionated Language

by

Cover image for Swift Style

Swift Style

An Opinionated Guide to an Opinionated Language

by

Discover the do’s 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. 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

Writing a style guide is tricky, because developers have such strong opinions on style that it can be scary to jump into the fray. But in this book, Erica is brave enough to offer strong personal recommendations and opinions based on battlewon experience, and she backs them up with solid arguments and reasoning, while ultimately encouraging you to make your own decisions. If you’re looking for an in-depth exploration of Swift style, this is the book for you. You’re guaranteed to improve your style—and learn a few great tricks along the way!

- Ray Wenderlich

Founder, Razeward, LLC

I really enjoyed reading this book—it’s comprehensive and thorough while staying concise. The book reflects the growing Swift community, including the open source world. Importantly, rationale is presented for everything discussed, which makes for a nuanced and educational book. I felt a lot of my assumptions being challenged, and while I didn’t always agree with the conclusions of the book, I was always left with something to think about. And, of course, I learned a lot too. I’d highly recommend this to any Swift developer.

- Ash Furrow

Developer, Artsy

When I hear the term “style guide,” I imagine a burdensome block of dictates, laid down by some mid-career code Stalinist still trying to win an office argument from 2005. That’s what makes Erica Sadun’s Swift Style such a delightful surprise. She brings out the natural charm and sensibility of Swift with clear examples, practical reasoning, and a healthy bit of whimsy.

- Chris Adamson

Author, "iOS SDK 10 Development"

Written by one of the Swift community’s most respected and active contributors, and with substantial care and attention to detail, this is a concise and thoughtful exploration of the many issues to be considered when developing your own Swift style. This is a worthy addition to any Swift developer’s library, and one that’s sure to help you write better, more maintainable code.

- Anthony Gray

Author, "Swift Pocket Reference"

Swift Style is one of those rare “must have” books—a style guide filled with opinions, options, and history written by someone who helped shape the language. We each benefit when all of us follow these guidelines. Every Swift developer will find themselves referring to this book often. It is our Strunk and White.

- Daniel Steinberg

Trainer, speaker, author, Dim Sum Thinking

Swift Style was everything I had hoped: carefully considered, straight to the point, and most importantly, immediately applicable to any serious project.

- Paul Hudson

Author

This is a book about the why, not the what. Even if you change nothing about how you write Swift, Swift Style will make you think about why you make a stylistic choice and the impact that’ll have when you (or someone else) have to read your code later.

- Christina Moulton

President, Team Mobile

See All Reviews

Choose Your Format(s)

  • $32.95 In Stock

    Save $11.95 on the combo pack.

  • $24.95 In Stock
  • $19.95 In Stock
  • Ebooks are DRM free.

  • Ebook delivery options.

About this Title

Skill-meter-3-6
Pages: 224
Published: 2017-03-10
Release: P1.0 (2017-04-04)
ISBN: 978-1-68050-235-0

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. This is the first 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. Strengthen code bracing for easy readability. Style your closures for safety and resilience. Perfect spacing and layout. Master literal initialization and typing. 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

Recent version of the Swift programming language

Contents & Extracts

  • Welcome to Swift Style
    • How This Book Got Here
    • Your Code Doesn’t Smell
    • What’s in This Book
    • Contributing to This Book
    • Online Resources
    • Ready to Get Going?
    • Credits
    • Thanks
  • Structure Your Code for Readability
    • Taking Control of Swift Structure
    • Understanding Swift Semicolons
    • Styling Colinear Braces
    • Hugging Parentheses
    • Wrapping Argument Lists
    • Coaligning Assignments
    • Improving Closure Hygiene excerpt
    • 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
  • Adopt Conventional Styling
    • Adopting Conventional Spacing
    • Mandating Maximum Line Widths
    • Selecting Colon Styles excerpt
    • Placing Attributes
    • Formatting Number Literals
    • Balancing Literals and Types
    • Constructing Collections with Literals
    • Optional Sugar
    • Mitigating Optional Constipation
    • Converting to Tuples
    • Considering Comma-First Styles
    • Wrapping Up
  • Establish Preferred Practices
    • Testing Assumptions
    • Choosing Optionals
    • Converting Collection Lookup to Optionals
    • Casting Conditionally
    • Chaining Calls
    • Moving from Thrown Errors to Optionals
    • Unwrapping Variables
    • Mapping in Condition Clauses
    • Iterating Collections of Optionals
    • Working with Optional Collections
    • Choosing Result Types
    • Adding Lazy Evaluation
    • Selecting Sequences and Strides
    • Looping
    • Indexing and Enumerating Collections
    • Switch Statements
    • Declaring Number Constants and Variables
    • Implementing Getters and Setters
    • Returning Void
    • Grouping Initializers
    • Using Call Site Inferencing
    • Evaluating Case Binding Syntax excerpt
    • Using If/Guard-Case
    • Choosing Capture Modifiers
    • 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
    • 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
    • Improving Code Descriptions
    • Avoiding Clever
    • Wrapping Up
  • Good Code

Author

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.