views:

39

answers:

2

Hi, people. I study Obj-C and Cocoa now, and I was wondering: why everybody writes method definitions/implementations like this:

- (void)translateOriginToPoint:(NSPoint)newOrigin{

- all together, no spaces.

For me, it's way more clean to write everything with spaced like this:

- (void) translateOriginToPoint: (NSPoint) newOrigin {

But I see non-spaced style everywhere: Apple documentation, code samples in various Cocoa dev sites and blogs, i. e. that's how experienced programmers write this. Why so?

+2  A: 

If you find it easier to use spaces for your own code, and you don't expect to share it, then hey, go for it. But as you say, the majority of examples and open-sourced Objective-C code uses no spaces between method names, parameters and types. Consistency in coding style is good; code is for humans, after all. I'd strongly encourage you to embrace the standard approach.

If I had to guess, I'd suggest its because the visual appearance of methods with multiple arguments use a space to separate the arguments in the method's definition;

- (void)say:(NSString *)message withTitle:(NSString *)title {
dannywartnaby
A: 

A helpful (non flaming) formatting point, if you have a long list of parameters for your message then Xcode does format things nicely at the colon when you have a multiline message declaration (just hit enter after typing your name:(type)localVarName text and it will tab it over correctly):

- (void)handleNewConnectionFromAddress:(NSData *)addr 
                           inputStream:(NSInputStream *)istr 
                          outputStream:(NSOutputStream *)ostr 

You see this style in documentation and many Cocoa books.

Coding "style" is something of a holy war amongst developers (including where to put the {}'s) be careful about asking these questions; you might get some rather disturbing responses.

Brent Priddy