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
Table of contents
- Welcome to Swift Style
-
1. 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
- 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
-
2. Adopt Conventional Styling
- Adopting Conventional Spacing
- Mandating Maximum Line Widths
- Selecting Colon Styles
- 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
-
3. 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
- Using If/Guard-Case
- Choosing Capture Modifiers
- Other Practices
- Wrapping Up
-
4. 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
- 5. Look to the Past and the Future
- 6. Good Code
Product information
- Title: Swift Style
- Author(s):
- Release date: March 2017
- Publisher(s): Pragmatic Bookshelf
- ISBN: 9781680504828
You might also like
book
iOS Development with Swift
iOS Development with Swift is a hands-on guide to creating apps for iPhone and iPad using …
book
Test-Driven iOS Development with Swift 4 - Third Edition
Use test-driven approach to develop highly-functional iOS apps with Swift 4 and Xcode 9 About This …
video
iOS Development with Swift in Motion
One billion iPhone and iPad users are waiting for the next amazing app. It's time for …
book
Hello Swift!
Hello Swift! is a how-to guide to programming iOS Apps with the Swift language, written from …