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.