views:

270

answers:

12

A statement that could be written as:

foo=(bar*5)+baz;

is usually written in sample code (documentation, tutorials etc) as:

foo = ( bar * 5 ) + baz;

This appears to require extra work and seems counter-productive to me. Is there a rational reason for this? Is this a good coding practice, or just for sample code?

(The reason I ask this is to make sure my coding style is right, and understand why most code I see online is written like this).

+2  A: 

Its easier to read, that's it.

Alex K.
"That's it" seems to undervalue easily readable code. All code is communication, it has to be right first but readable second.
MadKeithV
+10  A: 

I don't put spaces after ( or before ), but I know people that do; other than that it's how I would write it:

foo = (bar * 5) + baz;

I think it's easier to read, and about the tiniest amount of "extra work" you could possibly create. I used to code without much spacing and now I look back and think it looks terrible. There is no "right" coding style though; if it's your project format the source code however you want

Michael Mrozek
+2  A: 

Usually I place whitespace around operators, not braces. The primary reason is readability.

Timo Westkämper
+1  A: 

To increase the readability of sample -- especially if its the first time you're looking at it and you are trying to understand how it works.

Jason W
+8  A: 

It's no extra work with an IDE that applies the spacing according to your preferences. FWIW, my favourite spacing here would be:

foo = (bar * 5) + baz;

which is not quite a lengthy as your second example, but to my mind gives the right balance between brevity and readability. And ultimately that's what it's all about. It doesn't affect what it compiles to, so if it makes it easier to read, what's wrong with it?

David M
+1  A: 

only for clarity.

aJ
+1  A: 

Because it is more readable in most cases, although I'd skip the spaces after the opening parenthesis and before the closing parenthesis. Furthermore, this doesn't have to cost more time to write, due to formatting tools like in Eclipse. Here we can write down the short version and Eclipse will insert the spaces automatically. You can even configure the style that you like.

Marc
+1  A: 

Spacing helps you see what terms are supposed to be grouped together.

The example you give is pathological: the parentheses are redundant and the spacing counterproductive, but look at these examples:

foo = bar * 5+baz;
foo = bar*5 + baz;

The spacing in the first line groups the substring 5+baz together, which in turn suggests that + has, contrary to fact, the highest precedence.

Charles Stewart
The parens might technically be redundant and unnecessary, but I would still recommend putting them in. It makes the precedence obvious at even the most casual glance.
jwismar
@jwismar: It's often a good idea to put in parentheses, because order of precedence varies between PLs and is often obscure. But I don't accept this between addition and multiplication: putting spaces around the addition operator is more readable.
Charles Stewart
+6  A: 

That's how I write all my code - and it's how you should write all of yours.

anon
It's not often you see confidence like that. +1 sir.
Paddy
The spaces let air in around my code, and keep it from getting mouldy.
Warren P
+2  A: 

As a rule of thumb, "punctuate like you would in a natural language". People read more easily if what they see matches the patterns they expect. They expect a space after semicolons, commas, etc....

jwismar
+1  A: 

If you're worrying about the extra work to type anything, you're probably running at risk of creating write-only code. I never worry about how long something is to type in, the difference of a few more characters here and there doesn't matter compared to the time you might save, by it being clearer, in a few months when you're trying to remember how it works.

Just write it whatever way looks clearest to you and the other people in your team.

ho1
A: 

Just because you CAN write code without whitespaces doesn't mean you SHOULD. Ifmysentencesdidn'thaveanyspacesthey'dbemuchhardertoreadtoo.

Distinct elements in code should be visible on their own. As you get older (or your eyesight gets worse), you'll appreciate not having to squint at the screen to make out the text, and the people you work with will appreciate the readability of your code.

RealityMonster