tags:

views:

52

answers:

3

what would be a good way to format this piece of code?

UIBarButtonItem *prev = [[UIBarButtonItem alloc] initWithTitle:@"Prev"
                        style:UIBarButtonItemStyleBordered
                        target:self
                        action:@selector(setDateRangeClicked:)];
+4  A: 

You can, of course, do whatever you want. I like lining up the colons:

UIBarButtonItem *prev;
prev = [[UIBarButtonItem alloc] initWithTitle:@"Prev"      
                                        style:UIBarButtonItemStyleBordered
                                       target:self
                                       action:@selector(setDateRangeClicked:)];

I split up the declaration and the assignment just to get rid of the scroll bar.

Carl Norum
Hopefully, you'll see my answer, too. Carl's answer is completely correct. Mine is more of a meta-answer.
bbum
+4  A: 

For me, it is quite a simple rule of thumb.

(1) Turn on "Tabs Indent: [Always]" under the "Syntax-aware indenting" of the "Indentation" preferences pane of the Xcode preferences panel.

(2) In your source, select-all and hit <tab>.

Whatever formatting results, stick with it. Get used to it. Embrace it. Doing so means two things:

  • you can "select-all" and <tab> at any time to reformat your source file without worrying about lots of changes

  • if Xcode generates wildly wrong formatting, it is almost exclusively because you have a syntax error in your code.

Any number of folks will argue for any number of very specific formatting guidelines. All well and good but, ultimately, letting the tools do all the work will reduce time consumed and increase the # of problems the tools will detect automatically.

You are still responsible for sticking in newlines where you find them aesthetically pleasing.

bbum
Select all and <TAB> replaces all my code with a tab character. Do you have this set up as a keyboard shortcut?
nevan
It sounds like you've mapped "Indent Selection" to the Tab key. That's an interesting idea.
Darren
+3  A: 

Taking the question with another meaning, the best way to format that code is to use Xcode.

Highlight the code to format, then to to Edit -> Format -> Re-Indent and you will have your code in the "Standard" Xcode formatting.

I've mapped it to the keyboard shortcut alt-cmd-[ as I was used to TextMate. It's buried under lots of menus, so a keyboard shortcut is handy for this. If you use TextMate, click inside the block to format and press ctrl-Q. TextMate has some problems with this style of formatting though.

nevan