Podcast Search

Developing Perspective

#103: Coding Sins.

Select a time to play from there. The line being spoken is highlighted as the audio plays.

0:00:00Hello, and welcome to Developing Perspective.

0:00:03Developing Perspective is a podcast discussing news of note in iOS development, Apple, and

0:00:07the like.

0:00:08I'm your host, David Smith.

0:00:09I'm an independent iOS and Mac developer based in Heron, Virginia.

0:00:12This is show number 103.

0:00:15Today is Tuesday, January 8th.

0:00:17Developing Perspective is never longer than 15 minutes, so let's get started.

0:00:20All right, so the topic I'm going to be talking about today, it relates to some, I guess you

0:00:24could call it best practice, some lessons learned, some things like that.

0:00:28And this is coming out of a great series of articles that I've linked in the show notes

0:00:32that got started by a post by Ash Furrow talking about the seven deadly sins of a modern objective

0:00:38sea.

0:00:39And there was a great follow up to that written by Brett Simmons on Inessential talking about

0:00:45some of those and adding into some of his own.

0:00:47And there just are a lot of really good things to kind of talk about.

0:00:50And this is, if you haven't already sort of caught on, this is going to be a fairly technical

0:00:54This is a technical episode talking about fairly low level

0:00:57things that an iOS developer or a Mac developer

0:01:01would want to be aware of, would want to think about,

0:01:03and would want to wrap their arms around.

0:01:07This isn't one of my higher level, more hand-wavy ones.

0:01:10So first I'm going to start off by talking about where it all

0:01:13started, which is Ash Furrow's post,

0:01:15The Seven Deadly Sins of the Modern Objective-C.

0:01:18And the first one he talks about,

0:01:19which I very much agree with, is it's

0:01:22very dangerous to have one big nib file or zip file.

0:01:27And really what you're trying to avoid there

0:01:29is you're trying to avoid the situation where A, there's

0:01:33the performance hit of having this massive view hierarchy all

0:01:37put into a nib, because you have to load all of that dynamically

0:01:41or at once when it loads.

0:01:43And there's ways you can get around it in storyboarding.

0:01:46But it's very dangerous to have too much of interface

0:01:49in one nib file.

0:01:50You can load multiple nibs.

0:01:52And this is actually an example of something

0:01:53that I do in Check the Weather, for example, where

0:01:57the actual layout is all done with all

0:02:01the different components or different nibs,

0:02:03I guess is what I'm trying to get at.

0:02:05The top, the left, right, and bottom

0:02:07are all individual nibs, as well as the main dashboard,

0:02:10if you're familiar with the app or if you can go look it up.

0:02:13But they're all loaded independently,

0:02:15rather than putting it into one big nib file

0:02:17and then just moving views around.

0:02:18And a lot of that is for the performance reason.

0:02:20I don't load the side panels until the first time

0:02:22the user wants to do it.

0:02:23And it's something that I think in general

0:02:25is something that you really want to make sure

0:02:28that you avoid.

0:02:29And it's good practice and good cleanliness

0:02:31too to separate as much as you can things

0:02:34into different concerns.

0:02:36The next thing I want to talk about, which is his number two,

0:02:39is not using dot syntax consistently.

0:02:42A lot of people go back and forth about dot syntax

0:02:45and whether it's the right thing to do.

0:02:47Usually what it comes down to is where your background is.

0:02:51If you're a long time Objective C programmer who

0:02:53has been doing it since before the dot syntax was introduced,

0:02:56you are probably anti it.

0:02:58If you're coming from a language like Java, where it's

0:03:01essentially that's the way that you would think about it,

0:03:04you're probably more apt to use it.

0:03:05Generally, I think the best thing he's saying here

0:03:07is make sure you're using it consistently.

0:03:09I think dot syntax works really well.

0:03:11And the way I tend to use it is I

0:03:13tend to use it pretty much exclusively for properties.

0:03:16That's pretty much the only-- and essentially,

0:03:19it's properties accessing internal IVRs.

0:03:21I never use dot syntax when calling a method,

0:03:24or even though you could.

0:03:25I've seen an interesting thing recently.

0:03:27Someone was talking about how they've

0:03:28started using it for methods without side effects, which

0:03:33I'm not so sure I'm convinced on either.

0:03:36Because then you start getting into, well,

0:03:37how do you define a side effect?

0:03:38How do you know if it has a side effect?

0:03:40An example of that would be calling, for example,

0:03:42dot class on something.

0:03:44So it sort of condenses it.

0:03:46But any time you're using something, you have to make sure you use it consistently.

0:03:49I think that's the more important thing.

0:03:53The rest of his things, there's not a lot that I necessarily wanted to get into that

0:03:56I didn't have as strong opinions about.

0:03:58But I wanted to move over into Brett Simmons' response, which I think had a ton of really

0:04:03good ones.

0:04:04And his blog post is titled, "Coder's in the Hands of an Angry God," which is obviously

0:04:12a follow-up to Seven Deadly Sins of the Modern Objective-C. And I think the thing that he

0:04:16said that really caught my attention and something that I've been thinking about but never really

0:04:21got into, one of the things Ash mentioned is that he says we don't do enough automated

0:04:25testing. And I think that's maybe true. I come from a Rails background where that was

0:04:30a much bigger thing. But in Objective-C, I've never found a really good way to do it. And

0:04:34it's really hard and it's really annoying and there's not great tools for it. And a

0:04:37lot of people say, "Well, you know, you just need to work on it." And that would encourage

0:04:41tool makers, and so on.

0:04:43But the reality is, automated testing doesn't necessarily,

0:04:47it only pays off in very narrow areas in applications,

0:04:52especially because a lot of it's UI based,

0:04:54and all these types of problems that are tricky.

0:04:57There's a lot of asynchronous code,

0:04:59especially since GCD was introduced in iOS 4,

0:05:02there's a lot of things that make it really tricky

0:05:05to do good test coverage.

0:05:06But I think Brett's response is, I think,

0:05:08very, very reasonable, and something that I've been

0:05:11I'm looking for-- and I think this is a great way--

0:05:14is instead of that, to start introducing more NS asserts

0:05:18into my code.

0:05:19If you're not familiar with it, basically, NS assert

0:05:21is you say NS assert, and then you pass in two parameters.

0:05:25You pass in an expression who should

0:05:28result to a Boolean and a description, basically.

0:05:32And if that Boolean ever evaluates to false,

0:05:38I think it is.

0:05:41Yeah, I think it's false.

0:05:44The assertion will blow up, and your app will stop.

0:05:48And you'll be told, this assertion,

0:05:51the thing that you said should always be true,

0:05:54is in fact not.

0:05:55And the reality of what that does

0:05:57is it lets you put these checks into your code that'll catch

0:06:01problems much more quickly.

0:06:03And I think he has a great example.

0:06:06NS assert not NS thread as main thread is what he's testing for.

0:06:11And so it's a great way to put that into some code

0:06:13that you know should only ever be called in the background.

0:06:16And so if it is called on the main thread,

0:06:18you want that app, you want it to be very aware.

0:06:24So when you're testing, you're going to catch that.

0:06:26Now, the other thing that's tricky with NS assert

0:06:29is in theory, it should be fine to leave in there for production

0:06:33once you've deployed it, but it always

0:06:34makes me a little nervous when I'm putting in something

0:06:37in my code that I know will crash rather than putting

0:06:40the app into a funny state.

0:06:42That's always a kind of tricky thing.

0:06:43In theory, if you're writing good assertions,

0:06:44it shouldn't matter.

0:06:45But that's the only part that I'm always a little uneasy about.

0:06:48But generally speaking, that just

0:06:49means you have to be very careful and thoughtful about how

0:06:52you use them rather than going-- sort of just putting them

0:06:54in willy-nilly.

0:06:56The next areas that I wanted to talk about

0:06:58is something that he actually has-- Brent actually

0:07:00had a whole blog post about, which

0:07:02is talking about the UITableViewCell is not a controller.

0:07:05There's been a whole series of things

0:07:07that he talked about this.

0:07:08But the big thing that I think he mentioned here

0:07:11that I thought was really clever or is important

0:07:14is that to try as much as we can to make sure

0:07:17that our UITableViewCells don't know very much about the model

0:07:22they're displaying.

0:07:23For them not to turn into the controller,

0:07:25for them to not be doing a lot of the stuff that

0:07:28should really be outside of them.

0:07:30A UITableViewCell should really just be a display.

0:07:33It shouldn't really care too much about what it's displaying.

0:07:37This is something I know I've run into myself, where

0:07:39it's often easy to think, well, I'll just do a custom TableView

0:07:42Cell.

0:07:42I'll pass in my model object.

0:07:43And then the cell will take care of displaying that.

0:07:47And then I've gotten bitten by that.

0:07:49An example of that is where in my audiobooks app,

0:07:53I display a lot of books.

0:07:54So I have a custom TableViewCell for displaying a book,

0:07:56a title, an author, a type, those types of things.

0:07:59And most of the time, it's all coming out of my core data

0:08:02object is what I'm displaying.

0:08:04It's my book object.

0:08:06But there's an area where I display popular books.

0:08:08And those are actually coming out of a JSON blob,

0:08:10and so they're actually a dictionary.

0:08:12And so it's kind of awkward where I was originally

0:08:16doing some hard coding to be pulling directly

0:08:18from a book, NS managed object.

0:08:21And suddenly, it's like, that doesn't really work.

0:08:23And I have to re-architect and change it.

0:08:25And I think what he's saying here

0:08:26is very true where you just want to make sure

0:08:29that your UITableView cells are as sort of dumb as to what

0:08:33they're displaying as possible.

0:08:35And you can do that in a couple of different ways,

0:08:37but what you never want to do is really

0:08:39create a tight coupling between your model and a UITableView

0:08:42cell.

0:08:43You want to put something in between there

0:08:44that gives you much more flexibility down the road.

0:08:48Other things he talks about that I think are really good--

0:08:50not turning on hosy level warnings

0:08:53or not treating warnings as errors.

0:08:55These are just kind of obvious things

0:08:57where the LLVM especially is an amazing compiler.

0:09:01And if you're doing things that-- LLVM can tell you

0:09:05tons of problems with your code well before you actually

0:09:08would need to, or far earlier than you'd notice them.

0:09:12And so anything that it tells you is probably--

0:09:15it's like, this is a warning.

0:09:17This is something you may or may not want to do.

0:09:19It's probably something you don't want to do.

0:09:21It's just in general-- and I would say--

0:09:23I've been doing this for a long time,

0:09:24but the compiler is still 10 times smarter than I am.

0:09:27So be careful about that.

0:09:28And same thing with static analyzer.

0:09:29Make sure every static analyzer issue you address.

0:09:32The static analyzer is very smart.

0:09:34It's catching really low-level problems

0:09:37that you'd almost certainly not be able to, at least not

0:09:40right away.

0:09:41He talks about making sure you're looking at the crash

0:09:43logs in iTunes Connect.

0:09:45I recently found this to be really helpful.

0:09:47I think since IOS 5, when iCloud was introduced,

0:09:50and I believe crash reports are now sent automatically,

0:09:53rather than having to be done via iTunes Sync.

0:09:55There's tons of them usually.

0:09:57They're really helpful.

0:09:58I've been able to track down lots of little problems with that.

0:10:01And any time I do an update, the first thing I do--

0:10:05or the last thing I do, the first thing

0:10:07of the actual submission process is I go in and make sure

0:10:09that I've addressed all of those.

0:10:11I pull up Refresh the Report List in iTunes Connect.

0:10:13It's really helpful.

0:10:14You don't have to do your own crash reporting or any kind

0:10:16of mechanism like that.

0:10:17It's just all baked into iTunes Connect and very helpful.

0:10:21Another thing is putting things in .h files that

0:10:24shouldn't be public.

0:10:25And this is something that I used to do a lot

0:10:27and have recently been trying really hard to fight against.

0:10:29And I think it's really helped to really clean up my code.

0:10:32It is basically-- it's a little tricky in the way

0:10:35that Objective-C does structures things,

0:10:38because you don't have the same level of-- if you're coming

0:10:41from a Java background where you had the four levels of publicity,

0:10:45basically, was it public, protected, private, and class,

0:10:50something like that.

0:10:51It's been a long time since I did good Java.

0:10:53So, but anyway, what you really want to do is,

0:10:55especially if you're going to be putting properties

0:10:57or things for a class that no one else should be aware of,

0:11:00put them as a class extension in the .m file.

0:11:02Don't put them in the .h file.

0:11:04The .h file should be as simple and straightforward

0:11:05as possible, because you have to remember,

0:11:07the person who's going to be having to work out

0:11:09how this class works is probably going to be you.

0:11:11And as smart as you think you are,

0:11:13your future self is likely not as smart as you.

0:11:16So be careful about that.

0:11:18Other things that he says that I think are really good

0:11:20is making sure you use pragma mark, which

0:11:22is just a great way to structure and organize your code.

0:11:25And the last one I want to talk about

0:11:26is using instruments for performance testing and memory

0:11:29analysis.

0:11:30And I think this has become especially helpful and important

0:11:33as the platform has improved and developed over time.

0:11:40There's a lot of these things where

0:11:42the things that are going to be able to differentiate

0:11:44your application is going to be speed and performance

0:11:47and reliability in a way that-- even

0:11:50it's like a weather app, right?

0:11:52I was building an app that was competing in a space that

0:11:54was incredibly competitive.

0:11:56I mean, there is a whole category with thousands

0:11:58of apps that do this.

0:11:59How do you differentiate yourself?

0:12:00And one of the things you have to do

0:12:02there is to work hard on performance

0:12:04and those types of things.

0:12:05I spent huge amounts of time in instruments

0:12:07doing-- looking at where are the slow parts of my app?

0:12:10How can I improve that?

0:12:12Looking for hiccups and bugs, looking for memory load

0:12:14over time.

0:12:15All these kinds of problems that you

0:12:17can improve your application in a way

0:12:20that the user may not necessarily

0:12:21notice in a way that is tangible.

0:12:23It's not like having an impressive graphic design

0:12:26or something where people are going to see it directly.

0:12:29But I think people will notice, and it's kind of that latent,

0:12:31this is a good app.

0:12:32It's fast.

0:12:33It's performant.

0:12:34And you, of course, want to also be doing your performance

0:12:36testing on slow devices.

0:12:38I think right now, my typical baseline

0:12:40is the iPhone 4, which I think gives me

0:12:43a pretty good coverage.

0:12:45It's not-- the 3GS I'll sometimes use,

0:12:48But it's not as much of something that I use these days, just mostly because it's a relatively

0:12:55small percentage of users, in my case, that use that.

0:12:59I'm bringing up the stats.

0:13:01It's about 6% of my users are using 3GSs, which is a pretty low number.

0:13:07Whereas compared to the iPhone 4, it's about 35%.

0:13:11And if you go iPhone 4 and up, it's 94%.

0:13:16So if I can use the iPhone 4 as a basis,

0:13:18I think it gets pretty good coverage,

0:13:19even though the 3GS technically supports iOS 6

0:13:22and all those kinds of things.

0:13:23But that's typically what I've been

0:13:24doing as a performance base.

0:13:25And it seems to work pretty well,

0:13:27especially because the 4 was the first of the A processors.

0:13:30I think it was the A4 inside of it,

0:13:32which gives you a really strong baseline to be working from.

0:13:36You can do a lot.

0:13:38If you're trying to get 60 frames a second animations

0:13:40and things, that's something that is usually

0:13:43possible on an iPhone 4 in some places on the 3GS.

0:13:45You just can't do it.

0:13:46Um, so anyway, those are just some of these thoughts.

0:13:49I said, these are great links in the show notes for you to go through and just read

0:13:52through and think about the reason I'm doing it now is again, it's just kind of

0:13:56this, as we're, you're starting into the new year and you're starting to think

0:13:59about new projects, new ways to do things.

0:14:01These are just some things to be thinking about and to kind of make sure that

0:14:04you're evaluating yourself of, you know, am I making writing as quality coded?

0:14:09I could be, are there things I could be doing that are better and things that I

0:14:11could be doing that are more useful to my future self?

0:14:15So, anyway, that's it for today's show.

0:14:18As always, if you have questions, comments, concerns, or complaints, I'm on Twitter @_DavidSmith,

0:14:22I'm on AppNet @DavidSmith.

0:14:25You can email me, david@david-smith.org, if you have questions about the show.

0:14:29Otherwise, I hope you have a good week, happy coding, and I'll talk to you on Thursday.

0:14:32Bye.