Swift Style: An Opinionated Guide to an Opinionated Language

Swift Style: An Opinionated Guide to an Opinionated Language

by Erica Sadun
Swift Style: An Opinionated Guide to an Opinionated Language

Swift Style: An Opinionated Guide to an Opinionated Language

by Erica Sadun

eBook

$19.49  $25.99 Save 25% Current price is $19.49, Original price is $25.99. You Save 25%.

Available on Compatible NOOK devices, the free NOOK App and in My Digital Library.
WANT A NOOK?  Explore Now

Related collections and offers


Overview

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


Product Details

ISBN-13: 9781680504828
Publisher: Pragmatic Bookshelf
Publication date: 03/30/2017
Sold by: Barnes & Noble
Format: eBook
Pages: 226
File size: 3 MB

About the Author

Erica Sadun enjoys deep diving into technology and has written, co-written, and contributed to dozens of books about computing and digital media. Sadun 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.

Table of Contents

Welcome to Swift Style xi

1 Structure Your Code for Readability 1

Taking Control of Swift Structure 1

Understanding Swift Semicolons 13

Styling Colinear Braces 15

Hugging Parentheses 17

Wrapping Argument Lists 17

Coaligning Assignments 19

Improving Closure Hygiene 19

Choosing Trailing Closures 24

Laying Out Partial Application 27

Laying Out Complex Guard Statements 29

Laying Out Ternaries 30

Binary Conditionals 31

Laying Out Long Collections 31

Weighing Late Property Declaration 33

Wrapping Up 34

2 Adopt Conventional Styling 35

Adopting Conventional Spacing 36

Mandating Maximum Line Widths 38

Selecting Colon Styles 38

Placing Attributes 41

Formatting Number Literals 42

Balancing Literals and Types 45

Constructing Collections with Literals 50

Optional Sugar 56

Mitigating Optional Constipation 56

Converting to Tuples 57

Considering Comma-First Styles 58

Wrapping Up 58

3 Establish Preferred Practices 61

Testing Assumptions 62

Choosing Optionals 66

Converting Collection Lookup to Optionals 71

Casting Conditionally 73

Chaining Calls 75

Moving from Thrown Errors to Optionals 77

Unwrapping Variables 77

Mapping in Condition Clauses 77

Iterating Collections of Optionals 78

Working with Optional Collections 79

Choosing Result Types 80

Adding Lazy Evaluation 81

Selecting Sequences and Strides 83

Looping 87

Indexing and Enumerating Collections 90

Switch Statements 92

Declaring Number Constants and Variables 98

Implementing Getters and Setters 98

Returning Void 100

Grouping Initializers 102

Using Call Site Inferencing 104

Evaluating Case-Binding Syntax 108

Using If/Guard-Case 112

Choosing Capture Modifiers 112

Other Practices 114

Wrapping Up 118

4 Design the Right APIs 121

Adopting Access Control 122

Avoiding Global Symbols 128

Nesting Functions 131

Nesting Types 132

Designing Singletons 134

Adding Custom Operators 136

Naming Generic Parameters 144

Naming Symbols 145

Plurality 150

Choosing Label Names 153

Initializers 159

Convenience Initializers 160

Naming Methods and Functions 161

Tips for Naming 163

Mutating Variations 163

Computed Properties vs. Methods 164

Adding Defaults 164

Protocols 166

Generic Beautification 167

Adding Typealiases 168

Choosing Value vs. Reference Types 169

Writing Good Errors 170

Wrapping Up 171

5 Look to the Past and the Future 173

Reconciling Past You vs. Future You 173

Documenting in Real Time 174

Adding Structured Markup 175

Commenting Well 179

Organizing with Bookmarks 183

Improving Code Descriptions 185

Avoiding Clever 187

Wrapping Up 188

6 Good Code 189

Index 191

From the B&N Reads Blog

Customer Reviews