BT
x Your opinion matters! Please fill in the InfoQ Survey about your reading habits!

The New York Times Objective-C Style Guide

by Abel Avram on Aug 06, 2013 |

The New York Times has published the Objective-C coding conventions used by their iOS team, containing suggestions for source code layout, writing conditional expressions, variable names, methods, using booleans, etc.

The New York Times is known for their rigorous writing style which is detailed in a printed manual published decades ago and used as a reference by many journalists. Now, the iOS team working for the respective newspaper has undertaken the task of creating a coding guide for developers programming in Objective-C. The guide is inspired by several Objective-C and Cocoa guides authored by Apple.

Coding conventions usually cover various source code aspects such as layout – indentation, white space, braces usage –, capitalization, comment style, etc. Every developer has his own coding style, but many times he is requested to follow certain conventions when joining a new team. While some may object following strict coding guidelines, this is generally recommended in order to improve code readability and the ease of maintainability. Sun’s “Code Conventions for the Java Programming Language : Why Have Code Conventions” supports coding conventions because:

  • 40%-80% of the lifetime cost of a piece of software goes to maintenance.
  • Hardly any software is maintained for its whole life by the original author.
  • Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.
  • If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create.
  • Some of The New York Times’ conventions are:

    Spacing – don’t use tabs, use 4 spaces. Method and other braces open on the same line but close on a new line.

    Good Example

    if (user.isHappy) {
    //Do something
    }
    else {
    //Do something else
    }

    Bad Example

    if (user.isHappy) {
    //Do something
    } else {
    //Do something else
    }

    Conditionals should always use braces to avoid errors.

    Good Example

    if (!error) {
        return success;
    }

    Bad Example

    if (!error)
        return success;
    if (!error) return success;

    Variable names should be as descriptive as possible. Property definitions should be used instead of naked instance variables wherever possible.

    Good Example

    @interface NYTSection: NSObject
    
    @property (nonatomic) NSString *headline;
    
    @end

    Bad Example

    @interface NYTSection : NSObject {
        NSString *headline;
    }

    Booleansnil/NO or YES should not be used in comparisons.

    Good Example

    if (!someObject) {
    }
    if (isAwesome)
    if (![someObject boolValue])

    Bad Example

    if (someObject == nil) {
    }
    if ([someObject boolValue] == NO)
    if (isAwesome == YES) // Never do this.

    The New York Times’ Objective-C coding style guide contains conventions for other elements such as methods, naming, literals, comments, constants, singletons, etc., asking for developer feedback and suggesting following other guidelines used by Google, GitHub, Adium, Sam Soffes, CocoaDevCentral, Luke Redpath, or Marcus Zarra in case theirs are not to ones liking.

    Hello stranger!

    You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

    Get the most out of the InfoQ experience.

    Tell us what you think

    Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

    Email me replies to any of my messages in this thread
    Community comments

    Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

    Email me replies to any of my messages in this thread

    Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

    Email me replies to any of my messages in this thread

    Discuss

    Educational Content

    General Feedback
    Bugs
    Advertising
    Editorial
    InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
    Privacy policy
    BT