Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Clearly, this:

if( foo( 5, 6, 7 ))
  do_something( arr[ 5 ]);

if more readable than:

if (foo(5, 6, 7))

yet very few people use whitespace inside their parens. The venerable indent program doesn't really provide useful options for this behavior (the option to include whitespace inside parens adds a space between consecutive occurrences, which is pretty hideous), and certainly some people will argue that it's a waste of horizontal space (but those same people would probably omit the spaces after the comma as well, so their opinion is clearly suspect), but surely there's some valid reason that this style is not more popular.

Why don't you use any whitespace inside your parentheses and/or braces?

share|improve this question

closed as not constructive by Mark Trapp Oct 2 '11 at 19:58

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.

Your assertion that the first is "clearly" more readable is what makes this question argumentative. –  Greg Hewgill Sep 23 '10 at 1:33
I think having white-spaces in beginning doesnt help but in comparision case : if((a>b) || (a>c)) rest of code; white spaces are needed –  GoodSp33d Sep 23 '10 at 5:16
I don't agree AT ALL that the first one is clearly more readable. For me, the second one is much more readable, and therefore preferred. –  René Nyffenegger Sep 23 '10 at 11:58
Clearly, you cannot see clearly. I think this kind of bias breeds contempt amongst programmers. Even if "This is very clear" is clear to you, it is far more clear to someone else when written as "これは非常に明確である". –  gahooa May 24 '12 at 21:59
I agree... yet StackExchange is a horrid place to find any kind of consolation regarding ANYTHING that is even remotely against the knee-jerked-grain. You go on and space things however you like... and DON'T mind all the haters and "non-constructivators". –  alex gray Dec 9 '12 at 13:51

6 Answers 6

up vote 13 down vote accepted

It's clear to me that the only place you did not add whitespace is the only place I might - between the consecutive ones:

  if ( foo(5, 6, 7) )

I'll only use whitespace with parens to break up a series of them to make matching easier.

share|improve this answer
That is a great technique, the only time I've seen that adding whitespace inside parentheses aids readability. –  fearoffours Sep 23 '10 at 7:19

I don't find it more readable at all. In fact, if you are passing a number of arguments, I'd argue the extra white space makes it less readable because it would be more spread out on the line and it could cause wrapping to become a bigger problem.

share|improve this answer
Agree that the second form is more readable. I do prefer to have a space after the each comma in the argument list, as shown in both examples. Leaving out that whitespace does make it hard to read. –  tcrosley Sep 22 '10 at 23:57
@tcrosley - I totally agree about the white space after the comma's; you have to include that. –  Jeff Siver Sep 23 '10 at 13:36

No, in your current form it is clearly less readable... It's harder to see where the parentheses start and end. Also, the outer parentheses of the statements have an inconsistent amount of spaces. Also, it seems unnatural that the parentheses are touching the if word.

With all these corrections, you are using way too much white space over the place and that would introduce characters shattered all over the place. Hence, the normal way tends to be more readable.

For long if conditions, the distance that you eyes have to travel even comes into play.

share|improve this answer

I don't use whitespace inside parentheses other than to separate arguments to function calls. However, I do add a whitespace between the parameter list and name of the function, i.e.: foo (a, b, c). Same with indexes into arrays (foo [index]). For me, this makes things a bit more readable. Here's a full example:

if ((a > b) || (d < e)) {
    foo (bar);
    i = f [index];

On a side note, I did write a project using the excess-whitespace approach:

if ( ( a > b ) || ( d < e ) ) {
    foo ( bar );
    i = f [ index ];

In retrospect, this didn't make the code more readable at all; in fact, it made it less readable since the entire code was usually equally spread out and made it hard to see what belonged with what. Also, it made lines too long and needed to wrap around on the next line, which further decreased readability.

share|improve this answer
When spaces are put everwhere like that, or just as bad, every single line is followed by a blank line (I worked on source like that just a few monthsago) - it's something like using a different font or line spacing. Just give text editors the ability to alter intercharcter spacing (except in names like 'foo') or interline spacing, and each programmer can have as they like w/o forcing excess space upon everyone. Even better: source code editors that format it custom for each user. –  DarenW Oct 14 '10 at 20:05
Could you edit to include an example of how you use whitespace to "separate arguments to function calls"? –  Noumenon Mar 6 '13 at 8:33
@Noumenon: Compare foo (a, b, c) to foo (a,b,c). In the former whitespace is used to separate the arguments, which is not used in the latter. –  gablin Mar 6 '13 at 15:04

I guess the problem is that it's not clear (to me at least). I find the second snippet much more readable.

I view the opening '[' as belonging to the index '5' - it looks weird (to me) pinned to the variable name and to the closing parenthesis instead of the index.

I was going to say that I like spaces after commas in parameter values, but you included those in both examples "foo( 5, 6, 7 )", and I fully agree that this convention is much easier to read than "foo(5,6,7)"

share|improve this answer

It makes it look like a list and not like function arguments. It takes you a second or two to realize its functions arguments with out the parenthesis right next to the numbers.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.