Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Personally I prefer space around operators. For me it makes the code much more readable. What are the arguments not have space around them? Line length?

An example from the Python style guide (I agree with this):


  i = i + 1
  submitted += 1
  x = x * 2 - 1
  hypot2 = x * x + y * y
  c = (a + b) * (a - b)


  submitted +=1
  x = x*2 - 1
  hypot2 = x*x + y*y
  c = (a+b) * (a-b)
share|improve this question

closed as primarily opinion-based by MichaelT, Martijn Pieters, gnat, Bart van Ingen Schenau, Kilian Foth Nov 26 '13 at 14:19

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

Food for thought from the Sun FORTRAN Reference Manual: "Consistently separating words by spaces became a general custom about the tenth century A.D., and lasted until about 1957, when FORTRAN abandoned the practice." – Blrfl Jan 26 '12 at 22:00
Also, COBOL considers spaces a must, but I guess this is a parsing requirement. – NoChance Jan 27 '12 at 1:17

15 Answers 15

up vote 47 down vote accepted

I must confess I like the with the combination according to precedence.

  1. x*x + y*y immediately know what's going on
  2. (x * x) + (y * y) spaces are okay, too, but then make it obvious
  3. x * x + y * y okay but I spend more time looking at it than in the above example
  4. x*x+y*y urghs
share|improve this answer
Number 1 is the best. – bigown Sep 11 '10 at 13:35
I normally put spaces around every operator, but I can see the value of indicating precedence. +1 – Tim Goodman Sep 11 '10 at 17:28
What about x+y * a+b? No, to explicitly empathize operator precedence you have to rely on parenthesis. – Lorenzo Sep 12 '10 at 10:56
@Lorenzo But why would anyone code that way? They would have been x + y*a + b – alternative Sep 23 '10 at 0:11
@mathepic: by mistake I suppose... so it is best to distribute spaces equally to avoid mistakes. – Lorenzo Sep 23 '10 at 8:06

Spaces rock. I like to imagine that people who prefer no spaces live in very cramped, very crowded cubicles.

share|improve this answer
My spacebar has a toll, you insensitive clod! :( – greyfade Sep 11 '10 at 15:27
Intimate zone for operators :) – pramodc84 Sep 14 '10 at 2:43
+ 1 (including the space) – deltreme Oct 6 '10 at 6:52
And I said, I don't care if they lay me off either, because I told, I told Bill that if they change coding style one more time, then, then I'm, I'm quitting. And, and I told Don too, because they've changed coding style four times already this year, and I used to indent my code with 4 spaces, and I could read the code, it was readable, but then, they switched from 4 spaces to 2 spaces, but I kept my 4 spaces because it looks better, and I kept the spaces between operands and it's not okay, but if they take away my spaces between operands, then, then I'll set the building on fire... – gablin Jan 10 '11 at 19:53
I can remember when the programmers world included stuff like greyfade's spacebar troll. We used to carry around trays of hollerith cards. And there was a per card read charge just to read your deck in. And diskstorage was also outrageously expensive. So not using unneccesary spaces (or comments), and grouping as many statements per line of source were important for optimizing your overall project cost. That said, there is just the right amount of white space that provides optimum readability (same with name length) – Omega Centauri Jan 10 '11 at 22:45

Let me put it this way:

P u t t i n g s p a c e s e v e r y w h e r e d o e s n ' t r e a l l y h e l p.

Use whitespaces to visually support the structure of your code.

But understand: The clearness of your codes structure is not proportional to the amount of whitespace you use. So don't overdo it. Personally, I often code on my laptop and I can tell you, putting two new lines after each block doesn't increase readability, because it prevents me from seeing a whole unit of code at once. But then again, these are my personal constraints.

Adding whitespaces between operators is not cannonically better. Any mathematician will tell you that 4ab is more readable than 4 * a * b. It's a matter of culture and habit.

What you really should do is sit down with your team and try to get a feeling for how much whitespace is too little and how much is too much and then stick to that. Because ultimately, you don't do this to please some higher power (or some style committee that acts as if it were one), but for the people who have to maintain that code.

share|improve this answer
Let me put it this way: Spacesaregoodbecausetheyhelpyouseperatethingswhicharehardertoseparatewithoutthem‌​andit'saveryconsistentandeasyruletofollowinsteadoftalkingaboutitandfiguringoutthe‌​"right"amountofspacesneededforthisprojectetc. – ubershmekel Apr 29 '12 at 6:50
pwned. Use spaces, end of question. – dtbarne Sep 4 at 16:50

Spaces give some room for breath and make the code a lot more readable.

share|improve this answer

All coding standards I've ever used (in Java, C and C++) required spaces around most/all binary operators. For me, if the coding standard says so, that is good enough reason.

Some people would argue that coding standards are fascist, cramp their style, make them less efficient programmers, etc. But these arguments miss the point. Coding standards are about making your code readable and maintainable by other people.

share|improve this answer
You think that's Fascist? How about this? – Mark C Oct 6 '10 at 7:21
Ummm ... thank you for sharing Mark. – Stephen C Oct 6 '10 at 12:59

Readability is more important. So go for the 1st method.

share|improve this answer

Proper syntax highlighting, which is a must-have for every IDE, makes spaces less useful. Highlighting makes names of variables visually distinguishable from math operations and assignment operators. This just means that how to set spaces is not a very important question.

I usually select a pattern depending on a language. If a language is abundant in spaces (such as OCaml or Haskell, where Space is a delimiter of function arguments), I use more of them:

let filtered = List.filter (fun x -> (x * 2) + 5 > a) list in
if ((List.length filtered) > 0) then ...

But if spaces aren't used as valuable syntactic constructs (C, C++, Perl), I tend to avoid them:

int decision=calculate(x*2,5,a) || (y*3<100);
if (decision){

Syntax highlighting (which seems to be turned off here) makes the latter example less of a mess.

share|improve this answer
I hate your C code. – R. Martinho Fernandes Sep 15 '10 at 3:25
If your code is that complex, use spaces and add additional well-named function/method calls, don't just trim spaces. – Bill K Nov 9 '11 at 16:29
@Bill, apparently, we have different notions of whether a piece of code is complex. To me, syntactical complexity doesn't immediately render the code "complex." – Pavel Shved Nov 10 '11 at 9:40
@Pavel If you can't read through code as though it was a book on your first pass-through, it will slow someone down some day. Slowing the next person down should be your biggest concern when programming (even over functionality). Note: The next person is most likely to be you in a week, and the time you spent making your code readable will be repaid immediately, generally before the first release. Also small final methods used for calculations are free--it doesn't take ANY cpu time to make your code readable--generally. – Bill K Nov 10 '11 at 17:00
I'm just wondering why that C code slows you down, Bill. Personally I'm not finding a problem with it, I might have chosen to space out the assignment operator, but the lack of spaces really helps reading the tuple (x*2,5,a) and the expression y*3<100 Besides, get a load of the J programming language (or Mandarin?), syntactic complexity is fairly subjective... What you prefer seems more noisy to me, so I am siding with Pavel on this one. It seems though, the vast majority of voters side with you. – ninegrid Mar 23 at 4:30

I like spaces. But fortunately they have invented code formatters so you can format the code he way you like.

share|improve this answer
No! You have to be consistent. Relaying on some code formatter is not a good way to be CONSISTENT. – iharob 2 days ago

My personal preference used to be to not go with the spaces: when writing C / C++ code I never used them around operators.

But since moving into the C# space, with Visual Studio automatically formatting my code with spaces around the operators, I realized that I actually didn't care. I'm perfectly happy with the spaces just as I used to be perfectly happy without them.

share|improve this answer

Space in code is like a fresh air breath. Always go with spacing between operators, if you want to highlight something just use parenthesis.

share|improve this answer

We have a couple of hundred years of prior art typesetting mathematics. I can't think of any reason not to follow that practice. Put spaces around operators, but not around '(' and ')'. I would also not put spaces around an exponentiation operator, especially if it is the circumflex (^). I am in some agreement with posters who suggested using spacing to match operator precedence.

share|improve this answer

I always do the spaces more because I like the way it makes my code look as a whole, instead of any readability when figuring out what expressions do. I don't see a difference either way as far as that goes, but something about having those spaces between everything makes the code feel much less cramped and more pleasing to me.

share|improve this answer

Spaces help in separating and therefore parsing the code. Where does a token end, and where does another one start?

In Pavels example:

int decision=calculate(x*2,5,a) || (y*3<100);

there is an ambiguity between european number formatting, which leads me to think for a very short time about

calculate (x * 2,5, a)

as in

calculate (x * 2.5, a)
  • of course just for the fraction of a second, but many short annoyances and distractions will make you tired after hours. If it is intermixed with output, where you write

    print ("calculate (x * 2, 5, a)")

and have to avoid ambiguity, which isn't in the expression from the programming languages perspective, you don't wan't to use different standards there.

You might have an editor, which highlights the commas, the ciphers and the variable name in different colors, but when posting it here or in email, the highlightening might get lost/will be lost.

I'm not sure how editors decide, what to mark for copy actions if you double click or triple click on an expression. Blanks could be helpful for reasonable decisions there.

share|improve this answer

Depends on if you are writing or reading the code. Being a solo coder I have found that my coding (ahem) standards have layers to them. When I am writing the function, or it is in progress, I just use basics, but when the function or module has jelled I will often put in the most OCD of coding style -- so I can tell when I am in a 'pristine' or finished function (or various stages thereof).

share|improve this answer

Consider that someone with aging eyes might need to read your code. Spaces around operators make code physically easier to read.

share|improve this answer

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