For Beginners in C: Variable Naming Conventionals

Game Editor comments and discussion.

For Beginners in C: Variable Naming Conventionals

Postby EvanBlack » Mon Oct 10, 2011 8:42 pm

A good idea when you are making your variables is to use common naming conventions such as if you have a constant denote it with a c or use all capital letters.

Code: Select all
//Example:
const int cMyConstantVariable = 20; // but as you can see this one doesn't look as neat as the next one.

const int MYCONSTANTVARIABLE = 20; // Now you know any time you see a variable in all capitals,
                                                         // it's a constant, it doesn't change.


Also, when creating your variables for different areas of your code, you should try to make them easy to figure out where they are, and also keep them from getting tangled up in the compiler.

Code: Select all
//Example:

int g_ThisVariableIsGlobal; //This is a global variable denoted with a g_
                                        // so anytime you see a variable with a g_ tag, its a global one.

int a_MyActorVariable;      // This is a User created actor variable.
                                       // You know it was created by the user for an actor because of the a_

int FunctionVariable;         // Function variables can be left the same,
                                       // they don't require any tags because if you are using the
                                       // other two naming conventions you won't have clashes.
                                       // These variables are only used inside the function
                                       // they were created for.



So what this looks like all together, and even allows using the same name over and over with no clashes, (which is not a good habit btw..)

Code: Select all

const int g_VAR = 20;  //global constant

int g_Var;

void SetVariables( int Var)
{
    Var = g_VAR;
    myActor.a_Var = Var;




Now you can see, even with all the variables having the same name, there is no confusion on which variable is which. You can read it easily and the compiler won't freak out. But you shouldn't name you variables the same thing anyway. It just bad coding habit.

Note: Don't look at my current code, its a nightmare lol.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: For Beginners in C: Variable Naming Conventionals

Postby Jagmaster » Mon Oct 10, 2011 9:11 pm

Good stuff to know. Thank you.
User avatar
Jagmaster
 
Posts: 875
Joined: Sun May 08, 2011 4:14 pm
Location: Not where you think.
Score: 82 Give a positive score

Re: For Beginners in C: Variable Naming Conventionals

Postby CoFFiN6 » Wed Dec 14, 2011 8:57 pm

Wow thanx a lot!!! This helped me
Don't run faster then the dragon, run faster than your friends :P
User avatar
CoFFiN6
 
Posts: 49
Joined: Sun Sep 11, 2011 8:17 pm
Location: South Africa
Score: 4 Give a positive score

Re: For Beginners in C: Variable Naming Conventionals

Postby Game A Gogo » Wed Dec 14, 2011 9:07 pm

what I've learned in my programming class is to also use things like i,s,c,d,f to identify the type of variable, for exemple
Code: Select all
int iMyNumber;
float fMyFloat;
double dMyDouble;
char cMyChar;


s would be for strings, but GE doesn't have that library.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: For Beginners in C: Variable Naming Conventionals

Postby skydereign » Wed Dec 14, 2011 9:49 pm

Well you could use s for char arrays, sMyChar/sMyCharArray/sMyString.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest