Code-fu mantra

Lately, I’ve been living by some mantras when it comes to dealing with code and large code bases. Not sure if its for everyone, but it works for me.

This mantras of course pertain to Objective-C and the Mac in general.

  1. Subclass as little as possible, composition is better.
  2. Keep implementation files around 500 lines of actual real code, beyond that, you might want to think of refactoring into logical components.
  3. Group logical components by systems.
  4. Readability trumps “generalization”.
  5. Curly brackets are okay if code blocks are greater than 1 single line of code.
  6. Return as early as possible (Wil Shipley).
  7. Companion to returning as early as possible, try not to nest code blocks 3 levels deep.
  8. Async is not always the best solution (refer to mantra #7).
  9. Design for self containment, but more importantly, in areas of responsibility.
  10. Designing public APIs (things that will not change) will probably give you decoupled objects.
  11. Retain/Release is better than autorelease (until ARC of course).
  12. Protocols are awesome.
  13. Composition of data structures at the expense of space might save you time.
  14. Less objects more primitive types.

Thoughts comments? Eager to hear about yours.

10.8: Objective-C Enhancements

cocoaheads:

Objective-C Enhancements

OS X v10.8 includes the following enhancements to Objective-C:

  • Default synthesis of accessor methods for declared properties

  • Type-safe enums

  • New Core Foundation attributes that allow you to specify custom retain semantics

  • Object literals for NSArrayNSDictionary, and NSNumber

  • Streamlined object subscripting

Important Beginning in OS X v10.8, garbage collection is deprecated.

Can’t help be reblog this. Bloody marvelous. 

MNCoder

Its been about 3 weeks, but I’m finally done with a piece of work that will be used (hopefully) in the iCloud capable version of Mindnode.

MNCoder is what I’d call a clever hack using NSKeyedArchiver and NSKeyedUnarchiver to help with portability of certain Apple classes between iOS and the Mac. If you worked on both platforms, you’d know that iOS has its own implementation for colors and fonts. With iOS 4 this has been extended to NSAttributedStrings that take Core Text attributes instead of the nicer AppKit additions to NSAttributedStrings.

While I’d admit this is a somewhat complicated way of going about things, 2KSLOC to be exact, I figured this was the most straightforward and intuitive way to help people understand and possibly contribute additional code in the future.

What MNCoder does is that it uses a neutral platform independent NSCoding compliant object to store the base attributes that is needed to reconstruct an equivalent on either platform. Using #ifs, the object will give you the appropriate object. Adding to the system is NSCoder which helps with the finding and replacement of objects at runtime when doing serialization.

Its pretty much like old school language translation. Translate your source language into a intermediate version and then translate that to the target language. Nothing too fancy and easily understood.

The work is released under a BSD license which Markus has nicely given his approval of so here’s hoping it’ll have more contributors as it goes along.

Nice side effect of this is that I’ve had extensive time with Core Text and I’d say its pretty fun.

A little advice to companies

Before you engage a developer to build a mobile app for you, consider the following points,

  1. Are you shoving your web page into an app?
  2. Are you providing a catalogue of products/services/etc. with no useful way to purchase them beyond being just a showcase?
  3. Are you adding a “game” so that people will actually use your app?
  4. Are you using the app as nothing more than a marketing effort?

If you have answered yes to the above, here’s some cost saving advice.

    Don’t do it.