Swift Style, 1st Edition

Book description

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.

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

Publisher resources

View/Submit Errata

Table of contents

  1.  Welcome to Swift Style
    1. How This Book Got Here
    2. Your Code Doesn’t Smell
    3. What’s in This Book
    4. Contributing to This Book
    5. Online Resources
    6. Ready to Get Going?
    7. Credits
    8. Thanks
  2. 1. Structure Your Code for Readability
    1. Taking Control of Swift Structure
    2. Understanding Swift Semicolons
    3. Styling Colinear Braces
    4. Hugging Parentheses
    5. Wrapping Argument Lists
    6. Coaligning Assignments
    7. Improving Closure Hygiene
    8. Choosing Trailing Closures
    9. Laying Out Partial Application
    10. Laying Out Complex Guard Statements
    11. Laying Out Ternaries
    12. Binary Conditionals
    13. Laying Out Long Collections
    14. Weighing Late Property Declaration
    15. Wrapping Up
  3. 2. Adopt Conventional Styling
    1. Adopting Conventional Spacing
    2. Mandating Maximum Line Widths
    3. Selecting Colon Styles
    4. Placing Attributes
    5. Formatting Number Literals
    6. Balancing Literals and Types
    7. Constructing Collections with Literals
    8. Optional Sugar
    9. Mitigating Optional Constipation
    10. Converting to Tuples
    11. Considering Comma-First Styles
    12. Wrapping Up
  4. 3. Establish Preferred Practices
    1. Testing Assumptions
    2. Choosing Optionals
    3. Converting Collection Lookup to Optionals
    4. Casting Conditionally
    5. Chaining Calls
    6. Moving from Thrown Errors to Optionals
    7. Unwrapping Variables
    8. Mapping in Condition Clauses
    9. Iterating Collections of Optionals
    10. Working with Optional Collections
    11. Choosing Result Types
    12. Adding Lazy Evaluation
    13. Selecting Sequences and Strides
    14. Looping
    15. Indexing and Enumerating Collections
    16. Switch Statements
    17. Declaring Number Constants and Variables
    18. Implementing Getters and Setters
    19. Returning Void
    20. Grouping Initializers
    21. Using Call Site Inferencing
    22. Evaluating Case-Binding Syntax
    23. Using If/Guard-Case
    24. Choosing Capture Modifiers
    25. Other Practices
    26. Wrapping Up
  5. 4. Design the Right APIs
    1. Adopting Access Control
    2. Avoiding Global Symbols
    3. Nesting Functions
    4. Nesting Types
    5. Designing Singletons
    6. Adding Custom Operators
    7. Naming Generic Parameters
    8. Naming Symbols
    9. Plurality
    10. Choosing Label Names
    11. Initializers
    12. Convenience Initializers
    13. Naming Methods and Functions
    14. Tips for Naming
    15. Mutating Variations
    16. Computed Properties vs. Methods
    17. Adding Defaults
    18. Protocols
    19. Generic Beautification
    20. Adding Typealiases
    21. Choosing Value vs. Reference Types
    22. Writing Good Errors
    23. Wrapping Up
  6. 5. Look to the Past and the Future
    1. Reconciling Past You vs. Future You
    2. Documenting in Real Time
    3. Adding Structured Markup
    4. Commenting Well
    5. Organizing with Bookmarks
    6. Improving Code Descriptions
    7. Avoiding Clever
    8. Wrapping Up
  7. 6. Good Code

Product information

  • Title: Swift Style, 1st Edition
  • Author(s): Erica Sadun
  • Release date: March 2017
  • Publisher(s): Pragmatic Bookshelf
  • ISBN: 9781680502350