Inserting a space in function calls/definitions

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

Inserting a space in function calls/definitions

Post 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?
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Inserting a space in function calls/definitions

Post by Falco Girgis »

None whatsoever. Just to make it more readable. To me, it just makes it uglier.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Inserting a space in function calls/definitions

Post by short »

hopefully you don't pick up on the habit of adding the extra space.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Inserting a space in function calls/definitions

Post by eatcomics »

I hate the extra space too
Image
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Inserting a space in function calls/definitions

Post by hurstshifter »

Using the proper amount of whitespace is an art.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

Re: Inserting a space in function calls/definitions

Post 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++)
Image
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Inserting a space in function calls/definitions

Post 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
There's no place like ~/
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Inserting a space in function calls/definitions

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Inserting a space in function calls/definitions

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Inserting a space in function calls/definitions

Post by hurstshifter »

LOL @avansc
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
AerisAndMe
ES Software Engineer
ES Software Engineer
Posts: 381
Joined: Tue Apr 07, 2009 9:29 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: PC, SNES, PS3
Programming Language of Choice: C/++
Location: Madison AL
Contact:

Re: Inserting a space in function calls/definitions

Post 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.
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Inserting a space in function calls/definitions

Post 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:
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Inserting a space in function calls/definitions

Post by GroundUpEngine »

rofl avansc :lol:
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Inserting a space in function calls/definitions

Post 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.
User avatar
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

Re: Inserting a space in function calls/definitions

Post 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:
Image
Post Reply