Code Examples
November 15, 2012 -The point of sample code is to show you just enough code to communicate a concept. Sometimes code for a given demonstration (such as showing how to use parts CoreData or CoreText) requires a bit of boilerplate in order to show the concept.
For example, let’s consider a sample project to demonstrate how to draw text in a spiral using CoreText. You would need code to allow you to actually display your CoreText example code. This code has nothing to do with what you’re trying to demonstrate, but it is necessary for the project to function. Now you don’t want to bundle your sample project with an entire production-ready, functioning app because that would be way more code than a developer wants to look at to learn about CoreText’s ability to draw in a spiral. This is a super cool feature, we want to make sure people know how to make this happen. In an effort to reduce the amount of code included in your sample project, you might chose to do things that you would never do in a production app. You might violate all kinds of development principles and have one object handle 45 different jobs. You might put all kinds of functionality into only a few methods. This means a few things for the person reading your sample code:
- Just because you see it done in example code, even from Apple, doesn’t mean that is how you should do things in your own app.
- The code is to show you a concept, not write your app for you.
- They probably aren’t properly handling errors.
So kids, never copy-paste code out of an example into your app blindly. It is for educational purposes and not to save you the thought and time needed to write your app.