Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Space after if, while, etc (and before the paren)

Tags:

coding-style

Most coding conventions prefer putting a space after if, while, etc (if ( vs if(). Is there a particular reason for this, or is that something that is a personal quirk?

like image 326
Yuvi Avatar asked Sep 17 '11 19:09

Yuvi


People also ask

Should I put a space after if?

This rule will enforce consistency of spacing after the keywords if , else , for , while , do , switch , try , catch , finally , and with . This rule takes one argument. If it is "always" then the keywords must be followed by at least one space. If "never" then there should be no spaces following.

Should there be a space between if and?

'if' isn't a function. It's a reserved word. Use the space.

Should we give space before brackets?

One space before and none after an opening parenthesis or bracket within a sentence; no space before or after a closing parenthesis or bracket that is followed by a punctuation mark: Please read the enclosed booklet (Using Your Modem); it will help you take full advantage of your new communication tool.

Should there be a space between a word and a bracket?

Here's the rule: There should be a space before the open parenthesis and no space after it, and there should be a space after the closed parenthesis and no space before it.


2 Answers

I always thought that it was in order to differentiate them from function calls.

like image 100
K-ballo Avatar answered Sep 28 '22 09:09

K-ballo


It's very subjective question, but there are code conventions. Example in Java: https://www.oracle.com/java/technologies/javase/codeconventions-whitespace.html

"Blank spaces should be used in the following circumstances: ...A keyword followed by a parenthesis..."

while (

or

for (

But with modern IDEs it's not a critical question anymore, in my opinion.

I prefer NOT to add a space in these cases (after keywords like for, but in function/method calls function() as well. I think, function () is less readable.

like image 23
Hermann Schwarz Avatar answered Sep 28 '22 09:09

Hermann Schwarz