Page 1 of 2

Inserting a space in function calls/definitions

Posted: Tue Mar 23, 2010 6:55 pm
by Bullet Pulse
Such as:

int a = 5;
Foo( a );

vs.

int a = 5;
Foo(a);

I'm guessing people do this just to make the code look clearer, and I have picked it up, but it seems to be creating a hassle for me.
Is there a reason for doing this?

Re: Inserting a space in function calls/definitions

Posted: Tue Mar 23, 2010 6:59 pm
by Falco Girgis
None whatsoever. Just to make it more readable. To me, it just makes it uglier.

Re: Inserting a space in function calls/definitions

Posted: Tue Mar 23, 2010 7:10 pm
by short
hopefully you don't pick up on the habit of adding the extra space.

Re: Inserting a space in function calls/definitions

Posted: Tue Mar 23, 2010 7:50 pm
by eatcomics
I hate the extra space too

Re: Inserting a space in function calls/definitions

Posted: Tue Mar 23, 2010 7:52 pm
by hurstshifter
Using the proper amount of whitespace is an art.

Re: Inserting a space in function calls/definitions

Posted: Tue Mar 23, 2010 8:03 pm
by Bullet Pulse
For loops or if statements, do you guys typically use:

if(x == 0)
/
for(int i = 0; i < size; i++)

or

if (x == 0)
/
for (int i = 0; i < size; i++)

Re: Inserting a space in function calls/definitions

Posted: Tue Mar 23, 2010 8:32 pm
by LeonBlade
Bullet Pulse wrote:For loops or if statements, do you guys typically use:

if(x == 0)
/
for(int i = 0; i < size; i++)

or

if (x == 0)
/
for (int i = 0; i < size; i++)
Before I answer, let me just point out that there is no real way of doing it in a language that doesn't care about white-space.
If you wanted to, you could write all of your code on just one single line.

It's best you just mess around with how you write code, and come up with your own style.
For example, here are a few different ways someone might write an if statement.

Code: Select all

if(this==that){
    //code here
}

Code: Select all

if(this == that)
{
    //code here
}

Code: Select all

if( this == that )
{//code here
}
...and it just goes on and on, there isn't a right way of doing any of this.
Like I said, just play around and find what you think looks best to your eye.

Here is an example of a file of mine, note that my style ranges ;)
Image

Re: Inserting a space in function calls/definitions

Posted: Wed Mar 24, 2010 9:04 am
by Falco Girgis
Bullet Pulse wrote:For loops or if statements, do you guys typically use:

if(x == 0)
/
for(int i = 0; i < size; i++)

or

if (x == 0)
/
for (int i = 0; i < size; i++)
You are asking things that are completely subjective. There is no right answer to these questions. You need to figure out what you prefer through your own experiences.

I personally have spaces between arguments and operators like you posted.

Re: Inserting a space in function calls/definitions

Posted: Wed Mar 24, 2010 9:20 am
by avansc

Code: Select all

#include <stdio.h>

int main(void)
{
	for(int /* declaring integer var */ a /* naming said var "a" */ = 0 /* setting "a" to = 0 */ ; 
		/* use var "a" as LVALUE */ a /* if LVALUE */ < /* is less than */ 10 /* the integer value 10 */ ;
		a++ /* add 1 to var "a" */)
	{
		printf(          "pooshoe\n"          );
	}
	return 0;
}
this is what i prefer.

Re: Inserting a space in function calls/definitions

Posted: Wed Mar 24, 2010 10:26 am
by hurstshifter
LOL @avansc

Re: Inserting a space in function calls/definitions

Posted: Wed Mar 24, 2010 11:38 am
by AerisAndMe
Right, C++ leaves whitespaces as optional. Although, we all know that a whitespace in C++ isn't always so innocent -- try sticking one in the middle of a variable name or reserved word or data type :P. A C++ lexer will stop reading in a token if it reaches a whitespace just as soon as it will stop reading in a token if it reaches a character that cannot legally be part of the token it's reading in.

Ex: Say you have:

Code: Select all

if (i_var1<i_var2) {
After the "if" and the "(" are processed as tokens, the first "i" in "i_var1" will tell the lexer to jump to a state that reads in tokens that are okay to begin with a letter (might be reserved words, variables, data types, etc.), and then the underscore, the v, a, r, and 1 are all legal characters for such a token, so the lexer reads them in without a problem. When the lexer reaches the "<" operator, it stops, knowing that this character cannot be part of a token like "i_var1". Because "<" can't legally be part of the "i_var1" string, "i_var1" is registered as a token, and the lexer refreshes itself to prepare reading in the next token that now begins with "<". If there were a space (or a thousand spaces) between "i_var1" and the "<" operator, the lexer would jump to a state that reads whitespace characters, and stay there until another non-whitespace character was read in, at which point it would begin reading in the next token, starting with that character.

If you had something like this:

Code: Select all

if (i var1 < i var2)
(and we all know whitespaces are just as illegal in a token like "i_var1" as the "<" operator is), the lexer would read the "i", register it as a legal token, read whitespaces until the next non-whitespace character (in this case there is only the one whitespace separating "i" and "var1"), read "var1", register it as a legal token, and so on, leaving "i var1" as two independent tokens, "i" and "var1", rather than one. The lexer of course, thinks it's done its job, and would pass this right on to the parser as completely legit, not knowing there's even the slightest hint of a problem. The parser would tear this apart though, because we all know that statement is nowhere near syntactically correct.

In response to the question at hand, lexers for C++ know that a punctuation token is just one character long, so it doesn't matter to the compiler whether there's a whitespace or the beginning of a variable or data type that follows it, because that punctuation token's already been registered and set aside (if there is whitespace following it, the compiler just keeps reading in characters until it finds one that isn't whitespace -- otherwise it starts reading in characters for the next token).

Some languages do "care about" whitespace, though. Take Python, for example. Rather than using curly braces { } to indicate a code block, Python uses indentations:

Code: Select all

def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n-1) + fib(n-2)
Hope this helps and wasn't too long :P I have a tendency to ramble.

Re: Inserting a space in function calls/definitions

Posted: Wed Mar 24, 2010 11:41 am
by MrDeathNote
avansc wrote:

Code: Select all

#include <stdio.h>

int main(void)
{
	for(int /* declaring integer var */ a /* naming said var "a" */ = 0 /* setting "a" to = 0 */ ; 
		/* use var "a" as LVALUE */ a /* if LVALUE */ < /* is less than */ 10 /* the integer value 10 */ ;
		a++ /* add 1 to var "a" */)
	{
		printf(          "pooshoe\n"          );
	}
	return 0;
}
this is what i prefer.
:lol:

Re: Inserting a space in function calls/definitions

Posted: Wed Mar 24, 2010 11:47 am
by GroundUpEngine
rofl avansc :lol:

Re: Inserting a space in function calls/definitions

Posted: Wed Mar 24, 2010 2:30 pm
by qpHalcy0n
avansc wrote:

Code: Select all

#include <stdio.h>

int main(void)
{
	for(int /* declaring integer var */ a /* naming said var "a" */ = 0 /* setting "a" to = 0 */ ; 
		/* use var "a" as LVALUE */ a /* if LVALUE */ < /* is less than */ 10 /* the integer value 10 */ ;
		a++ /* add 1 to var "a" */)
	{
		printf(          "pooshoe\n"          );
	}
	return 0;
}
this is what i prefer.

The man's not lying ...I've seen his code.

Re: Inserting a space in function calls/definitions

Posted: Wed Mar 24, 2010 4:15 pm
by Bullet Pulse
Alright, so I've come up with a definitive way to use whitespace while I'm coding.

I haven't been paying attention to some little things like whitespace,
but now I think it's important to establish a set of these rules so that you keep your code consistent.

That made my day avansc :bow: