A couple of compact code idioms

May 23rd, 2012

Since we write code once but then read it many times there is an obvious desire to write less code so we have to read less code later. One of the 'local optimizations' I personally do is to compress if statements so they are easier to read. But first there is a thing that you should know: never skip curly braces! Formally if you have a single statement in if clause you can skip them but it could easily lead to logical errors, especially if you don't care to format code properly.

The first idiom is a read–only property that returns a mutable collection (or any other object actually). I typically follow this pattern in model objects. You define a read–only property, private ivar and getter method which initializes ivar if necessary and returns it. Turns out there is a nice way to write this method in a single line.

The second idiom is a parameter check for nil value: if you have to put it into collection you probably would like to use [NSNull null] to avoid crash. Normally you would write if statement but there is a shorter way to write it using just an expression. The first part is nil check and the second part is assignment which evaluates only if the first part is true.

Below is a simple class that demonstrates this; hope you will find it useful.

@interface Test ()

@property(readonly) NSMutableDictionary *dict;


@implementation Test {
  NSMutableDictionary *_dict;

- (NSMutableDictionary *)dict {
  return _dict ? _dict : (_dict = [[NSMutableDictionary alloc] init]);

- (void)setBoogie:(id)boogie forKey:(NSString *)key {
  !boogie && (boogie = [NSNull null]);
  !key && (key = @"default");
  [self.dict setObject:boogie forKey:key];


App Store top numbers

May 17th, 2012

So here are those numbers: 40k downloads per day for free app to get in top 25 list and 3,5k downloads per day for paid.

How Many Daily Downloads Does It Take To Reach The Top Of The App Store?

Make Art, Make Money — At The Same Time

May 2nd, 2012