Why you should use spaces before your method arguments

2007-11-12 10:14:47 -08:00

You can select a rectangle that intersects the spaces. You can't do this with tabs.

Backstory: I had moved the method being called there from a file of NSCalendarDate additions to a file to NSDate additions, so I needed to remove “Calendar”, and wanted to also take out the now-extraneous alignment spacing. One rectangular edit did the job, thanks to using spaces for the alignment.

Of course, you should always use tabs for that initial indent.

5 Responses to “Why you should use spaces before your method arguments”

  1. Jonathan WIght Says:

    I hate this formatting style. Takes up huge amounts of space, and a waste of time to both create and maintain. With well named objc selectors this formatting adds nothing to the readability of the code. Hate it, hate it, hate it.

  2. Peter Hosey Says:

    Jonathan: I’m not sure whether you’ll hate this more or less:

    then = [now
    	dateByAddingYears:-0  months:-0    days:-0
    	            hours:-1 minutes:-0 seconds:-0];
  3. Nat Says:

    I wrote a BBEdit script a few years ago to align selected lines vertically underneath the first colon in the first selected line. The script could be smarter (occasionally I don’t want it to be the first colon, and it only moves subsequent lines to the right), but it’s been quite handy, and much preferable to enforcing the use of spaces everywhere.

    Jonathan: how do you format a tableview datasource method, if not in this style?

  4. Chris Thomas Says:

    FWIW, TextMate will perform this formatting if asked. Move the insertion point anywhere inside the brackets of a method call and hit control-shift-Q. TM will detect the boundaries of the method call and format the arguments aligned underneath the first colon. I believe TM will also do the right thing (adding spaces where necessary) if you have used tabs and you want to remove a rectangular slice, but I have not tested that (in recent memory).

  5. Peter Hosey Says:

    Xcode will automatically indent it, too, but it uses tabs all the way across until it can tab no more. What it should do is tab to the level of indent of the first line, then switch to spaces the rest of the way (as shown in the screenshot).

    Chris Thomas: TextMate is close, in that it uses space from the indent to the argument name, but it also uses spaces for the indent, even though it uses real tabs for indents in all other cases. I’m going to guess that that’s a bug, though.

Leave a Reply

Do not delete the second sentence.