Here's a snippet of code from my iOS app:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you like my hat?"
message:@"If so, please help spread the word by rating our app now?"
delegate:nil
cancelButtonTitle:@"No Thanks"
otherButtonTitles:@"Sure!", @"Maybe Later", nil
];
Why does Xcode indent lines so bloody far? Being an old Perl, Ruby, and JavaScript monkey, I would be more inclined to indent it manually like this:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Do you like my hat?"
message: @"If so, please help spread the word by rating our app now?"
delegate: nil
cancelButtonTitle: @"No Thanks"
otherButtonTitles: @"Sure!", @"Maybe Later", nil
];
So the parameter names and values are all left-aligned, and indented only one level (4 spaces for me). This uses far less screen real-estate, and it's less likely I'll have to deal with wrapping lines (which make the far-right indented stuff even trickier to read) on my MacBook Air.
However, I assume there's some reason Apple prefers the first method, and therefore has Xcode indent that way. Or is there?
So I have two question:
Not trying to start a holy war here; I'm more curious about what tend to be the best practices among Objective-C programmers, how Xcode helps with those practices, and to find out if there is a good reason for the default way Xcode does it.
Update: Commenters and answers point out that the default formatting aligns the parameters at their colons. I should have remembered that before posting, since I have of course noticed it, and when the longest item isn't indented much, it can look pretty nice. But I find that, usually, the longest item is indented quite a lot. For example:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Do you like my hat?"
message:@"If so, please help spread the word by rating our app now?"
delegate:nil
cancelButtonTitle:@"No Thanks"
otherButtonTitles:@"Sure!", @"Maybe Later", nil
];
Why is that? Even if I wanted them to align at the colons (and I've often hand-formatted them to do so), it seems silly to indent them so much. Why does it insist on indenting to the level of the opening colon? Why not just one indentation level, with the closing bracket out-dented to show the end of the indentation "block"? For example:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Do you like my hat?"
message:@"If so, please help spread the word by rating our app now?"
delegate:nil
cancelButtonTitle:@"No Thanks"
otherButtonTitles:@"Sure!", @"Maybe Later", nil
];
This seems to be a better default than the Google style guide's line length recommendation, no?
Is there some way to tweak Xcode to format the parameters this way?
1) The Objective-c indent convention is aligned at the colon sign when multiple parameters are given.
example
function:
- (void)functionWithOne:(NSString *)one two:(NSString *)two tree:(NSString *)three;
Has to be formatted to:
- (void)functionWithOne:(NSString *)one
two:(NSString *)two
three:(NSString *)three;
I came from Java, and it was really hard for me to get used to at first. But after trying it different a few times, this is really the nicest way.
2) I don't think (I am not sure) that you can change that in xcode. Apple's convention is pretty clear how it is.
EDIT: Invocations. I personaly start with the first parameter and align the rest like:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you like my hat?"
message:@"If so, please help spread the word by rating our app now?"
delegate:nil
cancelButtonTitle:@"No Thanks"
otherButtonTitles:@"Sure!", @"Maybe Later", nil];
I'm not sure how to format it in Xcode, but two options for code formatting tools are:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With