7

I basically learnt C/C++ programming by myself, so I don't know much about good programming habit. One thing always make me wonder is why people always like to add spacing between operators in their codes like:

   if(x > 0) 

instead of

   if(x>0)

Are there any particular reasons for that? We know the compiler simply ignores such spacings, and I don't think the latter expression is less readable.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user0002128
  • 2,785
  • 2
  • 23
  • 40
  • 10
    I find the latter less readable. – chris Dec 12 '12 at 08:38
  • To be honest, I think the latter is less writable and no more readable. – user0002128 Dec 12 '12 at 08:38
  • 12
    "...I dont think the latter expression is less readable." Oh it certainly is less readable, and ugly too. Additionally, I would add a space between the if and the opening parenthesis. – jtepe Dec 12 '12 at 08:39
  • It's a matter of experience / preference. I second chris, but perhaps only because I've always coded like that. – irrelephant Dec 12 '12 at 08:40
  • This is the thing about every coding style imaginable - each person has their own opinion on it. The only valid argument of readability is one that includes science of our sight and brain. – chris Dec 12 '12 at 08:40
  • 3
    It is advantageous to distinguish between keywords (followed by a space) and function calls (not followed by a space), and it is easier to read the expressions when there are spaces around operators like `>` (but not around very tightly binding operators like `.` and `->` and `[]`). – Jonathan Leffler Dec 12 '12 at 08:41
  • 5
    Another thing people like to have is a white space between the `if` and the parenthesized controlling expression: `if (x > 0)`. – ouah Dec 12 '12 at 08:42
  • 14
    Itsforreadabiltysoyoudon'thavetospendtoomuchtimedecodingrubbish. – paxdiablo Dec 12 '12 at 08:44

8 Answers8

16

Sometimes space is necessary because the maximal munch priciple of the C/C++ lexer. Consider x and y are both pointers to int, expression

*x/*y

is illegal because the lexer will treat /* as comment. So in this case, a space is necessary:

*x / *y

(From book "Expert C Programming")

Lei Mou
  • 2,562
  • 1
  • 21
  • 29
8

I doubt that always happen, as you claim. In general, when working on a large project, there are conventions in place on whether spaces are to be added or not.

I'd apply spaces on a case-by-case basis:

a+b+c+d

is more readable, IMO, than

a + b + c + d

however

a+b*c+d

is less readable than

a + b*c + d

I'd say follow the conventions first, and afterwards think about readability. Consistent code is more beautiful.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • I think if you get used to write maths equations etc, you tend to prefer the more compact way of expresion. – user0002128 Dec 12 '12 at 08:45
  • 1
    @user1748356, Typically mathematical equations have blocks, much like polynomials, that you can stick together to separate them from other blocks. In the answer, this is shown with the two multiplicands and the asterisk as one group. – chris Dec 12 '12 at 08:47
5

It's simply good manner. You can write your code as you want, but this is a kind of "standard" way.

Also makes the code more readable.

Two examples to make you understand.

1) Space less

 if(x<0.3&&y>2||!std::rand(x-y)&&!condition){
 std::cout<<++x?0:1<<std::endln;
 }

2) With good formatting:

 if (x < 0.3 && y > 2 || !std::rand(x - y) && !condition) {
    std::cout << ++x ? 0 : 1 << std::endln;
}
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
5

The compiler doesn't care about whitespaces. It's just about readability.

Some people prefer whitespaces around operators, some don't. It's a matter of personal preference.

The only thing that matters is that when you work in a team, that you all agree to follow a uniform style (not just in regard to this, but also about a lot of other details), because a mix of both is harder to read than a uniform way, even when it's the one you like least.

Philipp
  • 67,764
  • 9
  • 118
  • 153
2

I think the main reason is code readability (and, to me, it's a very important reason).

To me, with more spaces the code opens up and becomes more readable (and so easier to understand and modify)

My style is like this:

if (x > 0) 
{
  ....
}

Note the space between if and the open parenthesis (.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
1

I agree with what others say that the code look more readable to the majority of people. Some people would not think it is more readable, but you have to assume others will be looking at the code in the future and would benefit from the more readable style.

Dr Zimmerman
  • 419
  • 1
  • 4
  • 5
0

Coding style is generally driven by a desire to make the source code easier to read. However, there is some amount of subjectivity in terms of which style is more or less readable than the other. I think most people would agree, though, that using a mix of both styles in the same file would be a bad choice.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
-3

Readability. Compare:

// C++

if (x > 0)
{
    // some
    // code
}

# Python

if x > 0:
    # some
    # code
Stefan
  • 4,166
  • 3
  • 33
  • 47
  • 2
    I'm not sure what the purpose of your example is -- is one of these supposed to be more readable than the other? (They both seem perfectly readable to me.) – Edward Loper Dec 14 '12 at 17:21
  • @EdwardLoper yes, I think that the Python one is more readable. What's the point of having the parenthesis and the curly braces? – Stefan Dec 26 '12 at 18:41