Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How did the convention of not puttting any whitespace in Obj-C method declarations come about?

How did it become convention that there is no whitespace in the declaration of a method?

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

It seems like everyone does it, 90% of the examples I see, the generated templates, other people's code, etc., etc. I suspect it's just another vi/emacs ideological thing, but wondered if maybe there was a K&R kind of "root cause" to the behavior.

Me, I like lots of whitespace:

- (UITableViewCell*) tableView: (UITableView*) tableView
         cellForRowAtIndexPath: (NSIndexPath*) indexPath

This seems so much better to me.

like image 487
jbm Avatar asked Oct 21 '10 23:10

jbm


2 Answers

I know a lot of code styles take getting used to to be more understandable. In this case I think that Apple's syntax reflects a thought process more than a coding style, not just personal preference.

When I first started using Objective-C I had the same concern. It can be confusing to look at no whitespace in a long line of code. However, once you get a better feel for the syntax, you will notice that even in your example there is whitespace. It's used to separate the segments of the function names from the previous parameter. Once you're there it becomes easier to pick out each segment of the function and it's parameter. Function name segment on the left, param on the right, repeat.

At first I did what you have listed -- spaces after everything. However that ended up being almost as bad as no whitespace. Since there were spaces everywhere they became meaningless. I next tried putting spaces after the colon and after the param, but that didn't seem right either -- the function name and the parameters started to get confusing in some cases. I tried a few other whitespace styles and eventually came back to use apple's standard almost exactly. The only thing I differ on now is the return type -- I prefer to have a space after the + or - and a space after the return type. Even that isn't that big of a deal, being mostly personal preference rather than any specific difficulty. (Having a heavy C/C++ background I like to be able to see the return type at a glance.)

If you keep at it you'll suddenly find yourself liking apple's way over the other. =)

like image 178
slycrel Avatar answered Sep 29 '22 07:09

slycrel


Most selectors are short enough that you don't need to put every argument on it's own line. It needs some getting used to, but without the spaces, the readability then becomes actually much better. Compare:

- (id) actionWithParam: (id) param object: (id) someObject andMore: (id) another

- (id)actionWithParam:(id)param object:(id)someObject andMore:(id)another

In the first line your brain will group stuff like

param object: (id)

which doesn't belong together, while in the second version you got the parameter groups nicely separated by whitespaces. The groups are pretty dense in themselves now, but the colon and parantheses actually are enough to separate name from the parameter and type.

like image 37
w-m Avatar answered Sep 29 '22 06:09

w-m