Pages

Thursday, August 22, 2013

Coding style for iOS app

Coding standard can be a great benefit as it makes it easy to read any code, and is a good practices which leads to less defects.

As writing a code according to coding guidelines is really very impressive and effective as well as very important.
Still many of us don’t follow any, here we have a very small and crisp idea of coding standard for iOS developers.
Contents

• Interface Files
• Implementation Files
• UIControls Prefixes List
• Before Deliver Source Code

Interface Files

The Interface Files/ .h files are header files. It is called header file because it only contains the ‘head’ or a class, in other words, all the properties and functions that any other outside class should know about, the ‘public face’ of a class. It does not contain implementation logics at all.
Standard to follow for .h file -
The interface files should be as small as possible and serve as a reference for what the class does and how to interact with it
• System imports go before project imports
• Don’t import anything that’s not needed for defining the interface
• Only expose the minimum necessary for other classes to interact. Everything else goes into implementation file
• Comment any non-obvious method

// MyClass.h

import
#import "MyOtherClass.h"

@interface MyClass : NSObject
@property (nonatomic, retain) MyOtherClass *blog;
@property (nonatomic, readonly, retain) NSArray *readonlyThing;
@property (nonatomic, assign) id delegate;
/**
Explain in a few words about the methods
*/
- (void)myClassMethods;
@end


Implementation Files

Implementation Files/.m file contains all the implementation details of all the functions declared in .h and more. It also contains declaration and implementation of private functions that outside classes cannot use, or should not care about.
Standard to follow for .m file -
• If the interface file has declared readonly properties, redeclare them as readwrite to be able to use the accessors
• Declare any internal variable and methods in the implementation
• dealloc is the first method, followed by initialization methods (If not using ARC)
• Use “#pragma mark -” to group methods into sections

// MyClass.m

#import "MyClass.h"
#import "ClassDependencies.h"

@interface MyClass ()
@property (nonatomic, retain) NSArray *readonlyThing;
/** Private methods */
- (void)prepareStuff;
@end
@implementation MyClass {
NSMutableArray *_things;
BOOL _flag;
}
@synthesize blog = _blog;
@synthesize delegate = _delegate;
@synthesize readonlyThing = _readonlyThing;
- (void)dealloc {
[super dealloc];
self.blog = nil;
self.readonlyThing = nil;
[_things release];
// ...
}
- (void)init {
self = [super init];
if (self) {
// Initialize
_things = [[NSMutableArray alloc] init];
}
return self;
}
#pragma mark - Public methods
- (void)myClassMethods {
[self prepareStuff];
// Do actual stuff
}
#pragma mark - Private methods
- (void)prepareStuff {
// Prepare stuff
// self.readonlyThing = ...
}
@end


UIControls Prefixes List

Here goes some common prefixes for UIControls, that we should use as a prefix in declaration of UIControls Object, this make variables readable and easy to find out.

UIControl                                Prefix
UILabel                                  lbl
UIButton                                 btn
UISegmentControl                         segCntrl
UITextfield                              txtFld
UISlider                                 sldr
UISwitch                                 switch
UIActivityIndicatorView                  ai
UIProgressView                           pv
UITableView                              tblView
UIImageView                              imgView
UITextView                               txtView
UIWebView                                webView
MKMapView                                mapView
UIScrollView                             srlView
UIDatePicker                             datePickerp
UIPickerView                             picker
UIView                                   view
UIWindow                                 window
UINavigationBar                          navBar
UINavigationItem                         navItem
UISearchBar                              searchbar
UIToolBar                                toolbar
UITabBar                                 tabbar


Before Deliver Project

Before We deliver our project to client, we should take care of following things.

• Check for whitespace.
• No NSLog in commits.
• No commented out code.
• Don’t avoid compiler warnings.
• Analyze Source Code and handle memory leaks.
• Source Code should be proper documented.

Happy Coding.. 

1 comment: