views:

202

answers:

12

When you have line of code with a parenthetical statement, do you include a space before the parenthetical?

For example, should it be

if()

or

if ()

Thanks in advance!

+1  A: 

It's definitely a personal preference (or a team preference when working with other developers) and really doesn't matter in the long run, but I (and everybody else I know) prefer no space.

Whatever you choose, just be consistent. Nothing bothers me more than looking at code that has multiple coding styles all mixed together with no consistency.

Justin Niessner
I would have to agree with that!! No space for me, I find that easier to read personally.
jimplode
+1 for consistency - I can't stand messy code
Brian
+5  A: 

Subjective, but I prefer having the space because it looks cleaner, or maybe because that's just how I learnt to 'do it' with language constructs such as switch, if, while and for.

You could do it without the space if you like it to 'look like' a function call, because I don't use a space for function calls.

Delan Azabani
+1. I prefer the space.. it's not a function call.
Fosco
Yep. I use the space because it's not a function call so I explicitly *don't* want it to look like one. Better yet, no brackets at all, in languages that allow it (eg Python).
bobince
A: 

Visual Studio automatically changes it to if (), so I follow that.

I think it's marginally more readable, but the brackets provide some space already as it is so I think this is one of those totally-doesn't-matter things.

Rei Miyasaka
@Rei - I don't know what version of Visual Studio you have, what language you're working in, or what code formatting extensions you have...but mine has never done this (and I would spend a day trying to figure out how to disable it if it did).
Justin Niessner
C# in VS2010. It's been doing it since VS2008 or possibly even VS2005.
Rei Miyasaka
My version of Visual Studio does this, but it could be ReSharper I suppose.
Scott Anderson
@Scott: It's not ReSharper; I checked.
Steven Sudit
+1  A: 

If you are the only developer on the project, pick whatever you prefer, otherwise stick with whatever convention the team is using.

mikerobi
A: 

In php it doesn't matter, but you probably knew that. I'm putting a space in between. I think it's more readable, probably because for functions I put no space in between and 'if' is not a function :)

zilverdistel
+2  A: 

I prefer keeping it to the default behavior of the IDE that I use, in order to reduce the risk of the source control system flagging insignificant differences between my edits and those of my co-workers. The same goes for other styling issues such as how many spaces to indent, whether to indent using space or tab and so on.

Fredrik Mörk
A: 

I prefer not to use a space, for me it makes it easier to read later and many of the people I know prefer no space as well for reading purposes.

sange
A: 

You should use a space because not all languages use bracket delimited if statements. Leaving a space is the safer habit to get into.

DeadMG
A: 

Interpreters and compilers ignore it, So you can ignore it too!!!

Jalal Amini
-1 Consistency in style counts because maintaining code is hard and it doesn't matter how much compilers ignore your keyboard mashing, the compiler doesn't maintain your code.
annakata
A: 

It depends on if the compiler/language ignores white-spaces. (all though off the top of my head i can't think of any that would balk at the "if" statement with a space). Outside of the language specifics, it's your preference. That being said, I prefer no space as it makes the code look more concise.

Joel B
A: 

It’s a matter of taste. I personally use the version with the space if () in order to distinguish this visually from a function call where I don’t use a space (as in mathematics).

Sven
A: 

Do whichever, but learn to read either without getting caught up about it.

Same deal as with bracket style.

There, that's about as meaningful an answer I can give to this.

Rei Miyasaka