Don’t Store Static Content in Code

Sticking large blocks of static content directly in code is a bad idea, but I see it all the time. You keep your image data separate, so why have a 2000 word privacy policy stuck in the code directly? Loading content like this from static text files, as an HTML string into a web view, or keeping configuration data in a property list is a much better choice, and what I do pretty much anytime I can.

If you have code like this in your app, you’re doing it wrong:

Instead, if you’re just talking about static text with some styling, you could keep all of that in an HTML document and do something like this:

Let’s say you had a list of documents like this you needed to display, like terms and conditions and about, which were all listed as rows in a table view. Instead of coding what each row is, you could save a lot of code by putting that information into a property list. If you store the info for each document as an item in array — with keys like title and documentName — you can load that array in your view controller and then pretty much automate this whole process with two classes and a few lines of code.

On top of getting clutter out of your code and saving the amount of code you write, there’s other nice things you can do now too. For example, maybe you want to be able to update any of these documents remotely. All you need to do is check a URL, download a copy of the files into your cache directory, and check for if there’s a match there before you display the one you bundled with the app.

Getting a feel for where you can get static content of out of code and into external resource files lets you not only write better code, but less of it.