Where should the spacing go?
February 8, 2011 2:01 AM   Subscribe

Where should you put the space in function defs/callls in C-like languages?

I'm currently working with a bunch of people who live by the syntax foo( a, b, c). It's a smart bunch of people and there are many of them. They say this is the most common thing they ever seen, and now I am confused.

I have seen various forms over the internet and in open source software:
  1. foo(a, b, c)
  2. foo(a,b,c)
  3. foo( a , b , c )
  4. foo (a, b, c)
just to name a few. Form #1 seems more common, but with my current environment it's not clear to me any more. Is foo( a, b, c) really more common? Is there anything about it that is more "aesthetically pleasing"? Is there anything else than pure taste and culture to evaluate these differences?

I guess a summary of my question is: is the form foo( a, b, c) really common, and should I prefer it over, say, foo(a, b, c)?
posted by knz to Computers & Internet (29 answers total)
 
This is a matter of religion. Therefore, option 1 is the only real option. Disagreements on this point must be settled through swift and unrelenting violence.

Seriously, though, I'm trying to come up with a reason your smart bunch of people would want to do what they want to do, and the only thing I can come up with is that actual parameters to functions are then guaranteed to always be preceded by a space, regardless of their position in the parameter list, which might make certain automated searches for them easier. (I can't imagine why you'd need to do this, though, unless you'd already done a pretty good job of screwing up your variable naming.)
posted by Vetinari at 2:11 AM on February 8, 2011 [1 favorite]


Best answer: There's no 'should', unless you're committed (dogmatically, or by having it imposed) to a particular style.

Personally I use #1.
In order of how often I see those four forms, I'd say #1, #2, #4, #3, although I'm not 100% sure about the order of #2 and #4.

#1 and #4 are closest to the way we write conventional english, so for that reason they probably have the edge aesthetically. But ultimately it's a question of personal style. Whatever makes the code readable and less error-prone for you is probably most important. In a lot of cases it's the person who writes the code that spends the most time reading it. Writing code for publication is a different matter, and you might want to follow a specific convention.
posted by le morte de bea arthur at 2:12 AM on February 8, 2011 [1 favorite]


Response by poster: Re: writing code for publication, are there any "common" styles used in publications nowadays?
posted by knz at 2:23 AM on February 8, 2011


Certain open-source projects have their own coding standards (see, e.g., the GNU coding standards, Linux kernel style), but generally, the style used for publication is the style of the organization doing the publishing. (Even the LK style starts of with an apology of sorts: "these things are personal and I'm not trying to force my views on anyone"; hence, "it's a matter of religion").
posted by Vetinari at 2:45 AM on February 8, 2011


function(a, b, c) is the only way anyone should ever write it and if anyone ever does it differently they should be burnt at the stake after being publicly exposed as being the heretics they are.

Yes I am serious.
posted by cmonkey at 3:19 AM on February 8, 2011 [3 favorites]


knz - for my own code I use #1 from your list because it's obviously superior, but really the rule is "whatever style is already used by the codebase you're writing for"... hell, I do occasional work on an old old system that uses K&R style function definitions, so that's how I format new work in that project... despite what the religious warriors will claim, keeping your codebase consistent and non-confusing is the greatest virtue...

Vetinari - I was expecting your "LK style starts with" to be the "First off, I’d suggest printing out a copy of the GNU coding standards, and NOT read it. Burn them, it’s a great symbolic gesture." comment...
posted by russm at 3:23 AM on February 8, 2011


You should do it the same way the majority of the code you're working on already does it.

In every large software project there is always some guy (and it almost always is a guy) who decides that his own whitespace religion is the One True Way, comes in on the weekend, checks every single module out of source control, redoes the whitespace and checks it back in.

Don't be that guy.

If you're starting your very own software project, you get to do the whitespace any way that pleases you.
posted by flabdablet at 4:48 AM on February 8, 2011


Best answer: There is absolutely no reason to have a leading space. Yes, it's religion, but this is the Religion of Common Fucking Sense. See also: the reason you don't indent two spaces or three spaces or four spaces or twenty-three spaces or a thousand spaces. It's stupid.

I believe that's the only logical reason. There are a host of other, less scientific reasons. Usually in situations like this it's easier to appeal to authority. Here's Google's take on it. I could spend the next half hour looking up other important people that would also agree with me but I've got better things to do (I mean, honestly, this is just How Things Are Done… any public source code you can find online will follow this pattern). Apache HTTPd? The shit that runs the fucking internet? Let's see… core.c (taken at random):
static const char *set_idcheck(cmd_parms *cmd, void *d_, int arg)
{
    core_dir_config *d = d_;
See that? Even those crazy bastards that put braces on their own lines (wrong)—even those dudes aren't crazy enough to put a space in front of the parameter list.
posted by Civil_Disobedient at 5:26 AM on February 8, 2011 [3 favorites]


Response by poster: Note that while I understand that specific *projects* may have their idiosyncraties for historical reasons, I am more interested what *people* think and use across multiple projects and what are the general trends among programmers.
posted by knz at 5:34 AM on February 8, 2011


I tell you, I once worked in a company where they were adamant about style 5:
foo ( a , b , c ) ;
with spaces between every fucking token. The lead programmer claimed the compiler parsed it faster that way :P

I use style 1, maybe sometimes style 2 if I'm being sloppy.

Now lets talk about brace placement...
posted by DarkForest at 5:55 AM on February 8, 2011


I'd say the overwhelming majority (99%+) of the code I have come across uses foo(a, b, c); That's what I normally use, or maybe foo a b c. Visual Studio also autoformats in foo(a, b, c) style, at least by default, so I'm guessing you would have to search pretty far to find anything else in that part of the programming world.
posted by Dr Dracator at 6:04 AM on February 8, 2011


Personally, for readability, I think 2 and 4 are ugly (2 probably was popular back when tiny terminals were the norm. Actual terminals, not terminal emulators). I don't have a strong opinion on 1 versus 3, but 1 is clearly more intuitive - enough space to make things clear, but without that ~confusing space between the function itself and the arguments being passed.
posted by spaceman_spiff at 6:38 AM on February 8, 2011


Apache HTTPd? The shit that runs the fucking internet? Let's see… core.c (taken at random):

That's a rather specious argument. If you look at gcc, which compiles the fucking internet, it's all GNU style, i.e. space before the opening (.
posted by Rhomboid at 6:50 AM on February 8, 2011


foo( a, b, c ) ;

What's invariant about calling foo? "foo( );"

What can vary? The actual args. So use whitespace to set them off.
posted by orthogonality at 7:27 AM on February 8, 2011 [2 favorites]


I used foo(a, b, c) for over a decade.

A few years ago I switched to foo( a, b, c ) and like it much better. (Note that this is not actually one of your four choices, although it's closest to #3.) I really like having the arguments set off a little. If I were typesetting it with a proportional font, I would use a thin space before the a and after the c; the full monospace space is a little much, but I like it more than having no space there at all.

I am definitely in the minority, though, and I don't use that spacing style at work when I'm working with files that other people touch.
posted by dfan at 7:37 AM on February 8, 2011 [1 favorite]


In the specific context of the question as asked, #1 is preferred. However, I think most of us would agree that #4 is preferred in the case of language keywords such as "for" and "if" and "while" which is a distinction often not recognized by beginners. Also, "sizeof" which is a distinction often not recognized by experts.
posted by rlk at 7:59 AM on February 8, 2011


If you're debugging on a console which selects text based on a double-click, and uses a space as a word separator, then foo( a, b, c ) makes it very simple to select a parameter for examination; for that reason I prefer, like dfan, that format over the four you show.
posted by anadem at 8:16 AM on February 8, 2011 [1 favorite]


Experiment & see what you like if you get the choice. Suck it up and deal if you don't. If you want to see something on the really strict end to figure out all the possible things you can worry about in C, BSD KNF is pretty hard-line (but oh-so-nice to read).
posted by devilsbrigade at 9:39 AM on February 8, 2011


Also, try to choose a style that can be auto-formatted with indent(1) and/or your IDE of choice. Really helps.
posted by devilsbrigade at 9:43 AM on February 8, 2011


As regards what people feel - some of the IDEs I use have auto-formatting features that are "opinionated", so I generally code however my IDE's auto-formatting prefers that I do. Why fight it?

#1 is generally how I type a function call.

See also: Curly braces on the same line vs curly braces on their own line argument.
posted by Vorteks at 12:31 PM on February 8, 2011


Also, "sizeof" which is a distinction often not recognized by experts.

This is probably because experts understand that sizeof is an operator, not a function. It doesn't require parentheses, so it doesn't match the other lexical patterns being discussed here.

return(sizeof(int));

ahhahahahaaa noobs
posted by flabdablet at 3:07 PM on February 8, 2011


This is like any other issue of style, whether in programming or writing, for instance the Serial Comma.

There is no fundamental right or wrong about these issues, but it's right for organisations to have a House Style. That way, you don't waste time. You use the House Style. Debates about the House Style itself should be had down the pub, or in some six-monthly meeting, not while you're working.
posted by AmbroseChapel at 3:40 PM on February 8, 2011


As a C instructor (for 3.5 years at EDS), we were taught to tell students that, "Here is our standard, when you get to an account, you use their standard."

I personally like the number 1 with spaces added also, next to the parens, thus: foo( a, b, c );
posted by Drasher at 5:36 PM on February 8, 2011


P.S. You might start a flame war if you asked where the braces go.
posted by Drasher at 5:38 PM on February 8, 2011


If I were typesetting it with a proportional font, I would use a thin space before the a and after the c;

For my personal projects, I use a proportional font for programming, usually Verdana. It makes it easier on the eyes.
posted by orthogonality at 10:09 PM on February 8, 2011


This is probably because experts understand that sizeof is an operator, not a function. It doesn't require parentheses, so it doesn't match the other lexical patterns being discussed here.

Real experts understand that the sizeof operator doesn't require parenthesis when applied to a variable or expression, but does when applied to a type:

int i;
unsigned size_t sv = sizeof i;
unsigned size_t st = sizeof( int );

posted by orthogonality at 10:15 PM on February 8, 2011


Real real experts understand that the parentheses around the type are there to make sizeof's operand look like a cast, not to make sizeof look like a function (which it certainly is not, since it's always evaluated at compile time). Also, if your headers don't already define size_t as an unsigned type, they're non-standard and should be fixed. So your last line really ought to read

size_t st = sizeof (int);

posted by flabdablet at 9:02 AM on February 9, 2011 [1 favorite]


Response by poster: I think there is a derail going on here. To settle the issue I have created an online form (here) to ask people I know what they think about. Preliminary answers give a clear preference for some forms rather than others. I hope I'll publish something about the results of this poll in the future.
posted by knz at 3:01 PM on February 10, 2011


The last question on your form should have a "none of the above" option.
posted by flabdablet at 5:18 PM on February 14, 2011


« Older Letters to the Departed   |   How and when to replant young grape vines? Newer »
This thread is closed to new comments.