Inserting a space in function calls/definitions
Moderator: Coders of Rage
- Bullet Pulse
- Chaos Rift Cool Newbie
- Posts: 89
- Joined: Sun Feb 21, 2010 6:25 pm
Inserting a space in function calls/definitions
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?
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?
- Falco Girgis
- 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
None whatsoever. Just to make it more readable. To me, it just makes it uglier.
- short
- 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
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
link: https://github.com/bjadamson
Re: Inserting a space in function calls/definitions
I hate the extra space too
- hurstshifter
- 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
Using the proper amount of whitespace is an art.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
http://www.thenerdnight.com
- Bullet Pulse
- Chaos Rift Cool Newbie
- Posts: 89
- Joined: Sun Feb 21, 2010 6:25 pm
Re: Inserting a space in function calls/definitions
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++)
if(x == 0)
/
for(int i = 0; i < size; i++)
or
if (x == 0)
/
for (int i = 0; i < size; i++)
- LeonBlade
- 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
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.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++)
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
}
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
There's no place like ~/
- Falco Girgis
- 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
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.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++)
I personally have spaces between arguments and operators like you posted.
Re: Inserting a space in function calls/definitions
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;
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- hurstshifter
- 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
LOL @avansc
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
http://www.thenerdnight.com
- AerisAndMe
- 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
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:
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:
(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:
Hope this helps and wasn't too long :P I have a tendency to ramble.
Ex: Say you have:
Code: Select all
if (i_var1<i_var2) {
If you had something like this:
Code: Select all
if (i var1 < i var2)
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)
- MrDeathNote
- 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
avansc wrote:this is what i prefer.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; }
http://www.youtube.com/user/MrDeathNote1988
"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
"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
- GroundUpEngine
- 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
-
- Respected Programmer
- Posts: 387
- Joined: Fri Dec 19, 2008 3:33 pm
- Location: Dallas
- Contact:
Re: Inserting a space in function calls/definitions
avansc wrote:this is what i prefer.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; }
The man's not lying ...I've seen his code.
- Bullet Pulse
- Chaos Rift Cool Newbie
- Posts: 89
- Joined: Sun Feb 21, 2010 6:25 pm
Re: Inserting a space in function calls/definitions
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
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