What's the purpose of adding spacings between operators? [closed]
Asked Answered
C

8

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.

Charleycharlie answered 12/12, 2012 at 8:37 Comment(8)
I find the latter less readable.Sounder
To be honest, I think the latter is less writable and no more readable.Charleycharlie
"...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.Impanation
It's a matter of experience / preference. I second chris, but perhaps only because I've always coded like that.Bitner
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.Sounder
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 []).Turfy
Another thing people like to have is a white space between the if and the parenthesized controlling expression: if (x > 0).Tanika
Itsforreadabiltysoyoudon'thavetospendtoomuchtimedecodingrubbish.Kinetics
A
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")

Alibi answered 12/12, 2012 at 8:45 Comment(0)
A
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.

Aborticide answered 12/12, 2012 at 8:41 Comment(2)
I think if you get used to write maths equations etc, you tend to prefer the more compact way of expresion.Charleycharlie
@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.Sounder
A
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;
}
Adz answered 12/12, 2012 at 8:40 Comment(1)
I'd even go further and have two spaces around the AND and OR operators.Geographical
P
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.

Petulah answered 12/12, 2012 at 8:40 Comment(0)
I
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 (.

Inconsistent answered 12/12, 2012 at 8:42 Comment(0)
T
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.

Temperament answered 12/12, 2012 at 8:46 Comment(0)
U
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.

Unapproachable answered 12/12, 2012 at 8:41 Comment(0)
K
-3

Readability. Compare:

// C++

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

# Python

if x > 0:
    # some
    # code
Kangaroo answered 12/12, 2012 at 8:46 Comment(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.)Inconsecutive
@EdwardLoper yes, I think that the Python one is more readable. What's the point of having the parenthesis and the curly braces?Kangaroo

© 2022 - 2024 — McMap. All rights reserved.