Swift and Easy

Swift feels very modern and is pleasant to read, especially compared to life with brackets in ObjC. There is a big difference between easy to read and easy to use, though, which is a claim I hear many making about Swift since WWDC.

(a valid first-response. it’s so much nicer to look at, how couldn’t it be easier!)

Please open your Apple-provide Swift iBooks and turn to page 332, Assignment and Copy Behavior for Collection Types. Go ahead and read through p338.

I hope this one jumped out at you on p336: "When you modify an element value through one array, the result is observable through the other. For arrays, copying only takes place when you perform an action that has the potential to modify the length of the array." That whole section is a bag of fun, but those two sentences are the crux of it.

Now trying explaining that section to someone new to development.

In ObjC it was pass-by-reference everywhere (for non-primitives). Now we have by-value in some cases, by-reference in others, and added rules for collections ontop of the normal by-value rules. This is a great example of Swift’s hidden complexities – extra mental baggage we need to keep on our mental stack as we work with the language. Now don’t get me wrong, there are great reasons for why it was designed like this (there are some great performance gains coming at the tradeoff of the developer needing to be aware of more), but that doesn’t mean it isn’t tricky. I’m sure it’ll be the source of many hard-to-debug mistakes.

Swift does have many big wins for us that aren’t just legibility. Apple did plenty to protect us from ourselves (which one might call making the language easier). The concept of pointers is swept under the rug, explicit optionals exist, the constants vs variables distinction, no out-of-bounds index wrapping, etc. But the depths of Swift contain some fun land mines we’ll have to be aware of when we’re trying to teach aspiring developers how to use it, so I wouldn’t be so quick to call it “easier” that ObjC.

For now I prefer saying that Swift is “more approachable.”

This is written over the summer before the final release of iOS8, so my specific example might improve before the 1.0 release.