Clean code – a way to a solid app

Our app – Manga Rock – has been on the app store for more than a week. Since it was the first week, i’ve been busy helping out our business people to ensure we would have a successful first week launch. Hence, I didn’t have much time to write code or blog about coding.

Anyway, today, I finally have some time to write about something that I’ve learnt last week.  This week’s blog topic is about Clean Code.

Before we continue, let be clear that I am no expert in this field. I don’t consider my code is clean at all. Point in case, Manga Rock still has bugs and its code quality is nowhere near to what’s considered clean code. That’s why, when we started working on next version of Manga Rock, I decided to take some time to learn how to write better codes in Obj C. This blog post serves as my learning journal on this topic.

What is clean code?

Just try googling “what is clean code”, you will see plenty of websites talking about what it is all about. And I think most of you know what it is about. For me, good codes are self-documentary codes – those codes that you can understand what they do without looking at their documentations.

I like the picture below, and I think it portraits clearly the difference between clean code (good code) and bad code.


Why there are so many “WTF” in bad codes? The very reason is that bad codes are not written for people to read.

Bad codes, messy codes or spaghetti codes are COSTLY.

Why costly? Because, bad codes to take more time and effort to maintain, fix bug and introduce new features. And in some cases, it’s so bad that it’s faster to write everything from beginning rather improving from the existing bad codes.

Tips on writing clean codes

Naming on classes, methods and variables.

  • Classes:  Noun, not verbs and no generic terms.  Example: NSArray, NSDictionary, NSUIView
  • Methods: For behaviors, the name should start with verb. Example: [downloadQueue addToQueue:newDownloadItem];
  • Variables: Don’t use single letter variables or any kinds of short forms (except for looping variable, or some common terms like: app, info, min, max). Learn from Apple, use full name. Example: managedObjectContext, persistentStoreCoordinator.

No code duplication – please no copy and paste codes.

Keep your class and methods at manageable size.

Reduce coupling among your classes

  • Delegates (most preferable)
  • Notifications
  • Key-value observing (least preferable)

The Boy Scout Rule: “Always check in code cleaner than before you touched it”

  • Before you check in any codes, just make sure it’s more readable. However, try to make it work first before clean it up.

Above are some handy tips about how to make your codes cleaner. And I learnt all those from watching a presentation from Mac Developer Network site. The presentation has more stuffs than I mentioned here so if you are interested in learning more, you can check it out here. I will update this blog with more information regarding about this topic whenever I learn a thing or two about it.

That’s it for this week blog post. I need to come back to my house-cleaning for Manga Rock’s codes. Hopefully, with this learning, Manga Rock will become more solid and there will be no more bugs such as this one. And don’t worry, we are not only fixing bugs but also introduce some new features of the coming update as well. So stay tuned for the news.

Some tips about Memory managament in Objective C

Despite the fact that I started programming in Pascal  at the age of 12, I have to admit Objective C is not something simple that I can learn and produce something useful in a few weeks. Why is it not simple? I think the main reason for me is dealing with memory.
Memory handling is, maybe, easy at first, but when you start writing something a little more complicated, you will start to have a good amount of sleepless nights debugging errors. Because when memory is not being handled properly, your program will be very unstable, meaning it will crash at very random point and it is very hard to debug.
Honestly, I still don’t find myself very confident about this topic yet. However, after 1 year learning Objective C and the 4 months developing Manga Rock, I did learn a thing or two that I think it’s worth sharing. Hopefully, these below tips will help you a bit when dealing with memory management in Objective C.

1. Memory debugging with NSZombie and Instruments

Using NSZombie and Instruments to debug is helpful when you are access an object with an reference count equals to 0. So check out this link and learn how to use it so when your program gives an error like “Bad Access”, you will know how to debug.

However, when you are still inexperienced with Objective C, and you are so afraid of memory leaks like me when I first started, you will tend to over release an object. If your program ever crashes without giving any kind of error, trust me, the first thing you should do is to find out if you are over releasing any object.

2. Collection classes and memory management

When you access object in a collection  like NSArray or NSMutableArray, it’s better to retain it and release it once you are done with it. Like the example below:

NSArray *pages = [data sortedArrayUsingDescriptors:sortDescriptors];
for (int i = 0; i < [pages count]; i++) {
     Page *p = [[pages objectAtIndex:i] retain];
     [self addToOperationQueueWithPage:[pages objectAtIndex:i] atPosition:i];
     [p release];

Why you need to do this? If your program is not using multi-threading, it’s fine to not follow this. But if your program does use any kind of multi-threading like NSThread or NSOperation, I strongly recommend you to follow the above pattern. Because if you don’t retain the object you are accessing, your program may crash when you are trying to access any object of the array and at the same time, other thread has just release that array object.

3. Nil object

This link from the iPhone OS Development Blog is a good link to explain the relationship between Nil object and memory handling. I do find that assigning the object to nil after releasing it does help my application, Manga Rock much more stable. And I think the reason is that in Manga Rock, I use threading a lot to make the application more responsive, so by assigning the object to nil, I will be sure that the object get deallocated.

4. Object Copying

If you are dealing with any mutable object like NSMutableString, it’s better to make a copy of the object before using it for anything else. This will help you to prevent the value of object unexpectedly (like by other thread).

Anyway, for now I can remember only these above tips about memory management that really helped me to make Manga Rock a solid application. So if I get to learn more about memory management in Objective C, I will definitely create another post and share with you all. And if you know anything useful about dealing with memory management in Objective C, please share with me by commenting on this post .

One last note for people who just started learning Objective C,  picking up a good book like Programming in Objective-C and reading through the memory management chapter of the book is a good start. After that you should read through Apple document about memory management. Doing those readings will definitely help to save you from a lot of sleepless nights. Don’t be like me just jump straight to coding without any proper learning :).