Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Objective-C Coding Style

Objective-C Coding Style

Oursky Limited

November 05, 2012
Tweet

More Decks by Oursky Limited

Other Decks in Programming

Transcript

  1. General rules For methods that represent actions an object •

    takes, start the name with a verb. E.g.: • - (void)invokeWithTarget:(id)target; • - (void)selectTabViewItem:(NSTabViewItem *)tabViewItem •
  2. General rules If the method returns an attribute of the

    • receiver, name the method after the attribute. E.g.: • - (NSSize)cellSize; Right. - (NSSize)calcCellSize; Wrong. - (NSSize)getCellSize; Wrong.
  3. General rules Make the word before the argument describe •

    the argument. E.g.: • - (id)viewWithTag:(NSInteger)aTag; Right. - (id)taggedView:(int)aTag; Wrong.
  4. General rules Don’t use “and” to link keywords that are

    • attributes of the receiver. E.g.: • - (int)runModalForDirectory:(NSString *)path file:(NSString *) name types:(NSArray *)fileTypes; Right. - (int)runModalForDirectory:(NSString *)path andFile:(NSString *)name andTypes:(NSArray *)fileTypes; Wrong.
  5. Accessor Methods Accessor methods are those methods that set and

    return • the value of a property of an object. If the property is expressed as a noun, the format is: • - (NSString *)title; • - (void)setTitle:(NSString *)aTitle; • If the property is expressed as an adjective, the format is: • - (BOOL)isEditable; • - (void)setEditable:(BOOL)flag; •
  6. Accessor Methods If the property is expressed as a verb,

    the format is: • - (BOOL)showsAlpha; • - (void)setShowsAlpha:(BOOL)flag; • The verb should be in the simple present tense. • - (void)setAcceptsGlyphInfo:(BOOL)flag; Right. - (BOOL)acceptsGlyphInfo; Right. - (void)setGlyphInfoAccepted:(BOOL)flag; Wrong. - (BOOL)glyphInfoAccepted; Wrong.
  7. Accessor Methods You may use modal verbs (verbs preceded by

    “can”, “should”, • “will”, and so on) to clarify meaning, but don’t use “do” or “does”. - (BOOL)canHide; • - (BOOL)doesAcceptGlyphInfo; •
  8. Accessor Methods Use “get” only for methods that return objects

    and values • indirectly. You should use this form for methods only when multiple items need to be returned. E.g.: • - (void)getLineDash:(float *)pattern count:(int *)count phase:(float • *)phase;
  9. Delegate Methods Start the name by identifying the class of

    the object • that’s sending the message. E.g.: • - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row; • - (BOOL)application:(NSApplication *)sender openFile:(NSString • *)filename;
  10. Delegate Methods Use “did” or “will” for methods that are

    invoked to • notify the delegate that something has happened or is about to happen. E.g.: • - (void)browserDidScroll:(NSBrowser *)sender; • - (NSUndoManager *)windowWillReturnUndoManager:(NSWindow • *)window;
  11. Naming notifications [Name of associated class] • + [Did |

    Will] • + [UniquePartOfName] • + Notification • E.g.: • NSApplicationDidBecomeActiveNotification NSWindowDidMiniaturizeNotification NSTextViewDidChangeSelectionNotification NSColorPanelColorDidChangeNotification
  12. Coding Style Position of “*”: • NSString *varName; • Method

    declaration • - (void)doSomethingWithString:(NSString *)theString{ • } A space after -/+ space No space for first argument
  13. Coding Style Method name shorter than arguments: - (void)short:(GTMFoo *)theFoo

    longKeyword:(NSRect)theRect evenLongerKeyword:(float)theInterval{ } At least 4 space indentation • Align vertically •
  14. Coding Style For constent like (#define, enums, const), use ‘k’

    as prefix: • E.g.: kInvalidHandle • #Import/#Include • Import objectiveC; • Include c/c++ • Import whole framework even when only need a few files • #import <Foundation/NSArray.h> • #import <Foundation/Foundation.h> •
  15. Coding Style BOOL trap: • BOOL defined as unsigned char

    • DO NOT compare BOOL with YES/NO • BOOL great = [foo isGreat]; • If(great == YES) • If(great) •