Objective C coding conventions

 

Like you probably reviewed some code sheets before, you must agree that sometimes, developers tend to define their own specific coding conventions adapting the guidelines for their habits.

 

As wikipedia says “Coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices and methods for each aspect of a piece program written in this language.”

As those conventions typically cover organization standards, are highly recommended to be used, because as an overall effect they improve the readability of the source code, and make software maintenance much easier.

 

Recently @dannygreg has posted a tweet where he announced that his team published on Github their own Objective C coding conventions. 

The posted file is a short list of tips and tricks that I invite you to discover. 

Direct download .zip File

 

 

Code Documentation

 

For the methods defined in header files (suffixed in .h), my preferred approach is this one:

 

/**

 * @abstract : Web Service call designed to fetch all TaskInformations for the given CategoryId and UserId

 * @param    : categoryId taskInformation's categoryId

 * @param    : userId questInformation's userId

 * @return   : -

 */

- (void)getTaskInformationByCategoryId:(long)categoryId andUserId:(long)userId;

 

and for the implementation (method) files (which normally have a file extension .m) I prefer to use //  followed by a TAB in order to write a specific comment above ones of the most complex operations.

 

//  only on zoom out

if (zoomScale < 1) {

    //  if the scene is shifted

    if (diff.x < 0) {

        //  bring it to 0 in order to avoid blank spaces

    // on the edges after zoom out

        if ((diff.x + ((sceneWidth - sceneWidth*zoomScale) *zoomScale)) > 0){

            diff.x = 0;

        }

        else{

            diff.x += ((sceneWidth - sceneWidth*zoomScale) *zoomScale);

        }

    }

}

 

 

 

Properties Declaration

@property (attributes) type name;

 

 

The following code snippet illustrates a header file. As we can see from below there are some TAB spaces between the instance variables name and their type. This approach provides a higher readability level on the source code, an also looks prettier :) . If we take a closer look at the two methods defined here, we can see that there is a white space between the method’s type identifier ( - ) and it’s return type (for the same reasons).

 

//

//  AccountCell.h

//

//  Created by Dorin-Bogdan Danciu on 7/15/12.

//  Copyright (c) 2012 Dorin-Bogdan Danciu. All rights reserved.

//

 

#import

@class NBAccount;

@interface AccountCell : UITableViewCell

@property (retain, nonatomic) IBOutlet UILabel      *accountNrLabel;

@property (retain, nonatomic) IBOutlet UILabel      *totalLabel;

@property (retain, nonatomic) IBOutlet UIView       *separator;

@property (retain, nonatomic) IBOutlet UILabel      *initialSumLabel;

@property (retain, nonatomic) IBOutlet UILabel      *percentLabel;

@property (retain, nonatomic) IBOutlet UILabel      *revenueLabel;

- (void)customizeForIndex:(int)index bound:(BOOL)bound;

- (void)dispalyAccount:(NBAccount *)account;

@end

 

 

 

 

NOTE: This article is constantly updated!

 

 

Posted by dorindanciu 27/09/2012 at 04:01PM


Comments

Leave a response

Leave a comment