Anonymous
Anonymous

Reputation: 11

Non-spaced method definition style

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?

Upvotes: 1

Views: 144

Answers (3)

Inanc Gumus
Inanc Gumus

Reputation: 27889

There is no logic behind it, it is widely accepted by pioneers and became so (take my word for it but it has no reference, it is by common experience).

Also, I'm using your formatting style too: so now we are a small community with its own standard :p

Good luck.

Upvotes: 0

Brent Priddy
Brent Priddy

Reputation: 3847

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.

Upvotes: 1

dannywartnaby
dannywartnaby

Reputation: 5542

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 {

Upvotes: 2

Related Questions