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

Coding Style

Coding Style Problem Overview


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?

Coding Style Solutions


Solution 1 - Coding Style

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

Solution 2 - Coding Style

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYuviView Question on Stackoverflow
Solution 1 - Coding StyleK-balloView Answer on Stackoverflow
Solution 2 - Coding StyleHermann SchwarzView Answer on Stackoverflow