Hi I got the Jumping into C++ book and im confused as to how to name a Variable. I understand you need the type first but the example I see changes throughout the chapter and I don't see the original example again. I don't know if the fallowing naming pattern is necessary or if you can make up names to describe that section of code.
1 2 3 4 5
int whole_number;
char letter;
double number_with_decimals;
int thisisanumber
Variables can be named whatever you like. However, its good practice to use meaningful names otherwise it can be difficult for others to follow and understand your code, or even yourself months down the line when you reopen your code.
Yes, variables only technically have to follow the format of <data type> |variable_name|, but as Softrix pointed out the normal is to make the names informative so one can kind of deduce what you are doing from looking at your code.
Technically you could very well do this:
1 2 3 4 5 6 7 8
int main()
{
int i, j, k;
char a;
// imagine if you had 100+ lines of code manipulating variables like that
// gets unreadable fast and really confusing
return 0;
}
This would be better:
1 2 3 4 5 6 7 8
int main()
{
int firstNumber, secondNumber, sum = 0;
char answer;
// still not the best example, but at least now just from looking at the names
// someone can guess that the ints are going to be the numbers for finding
// the sum and answer will be the 'y' / 'n' / 'q' or such of a question.
}
Thank you all so much! I have been so stuck on this one part. Can you tell me why you use run on words such as" thisisanumber" why no space? What the symbol _ means, when to and not to use it. The book doesn't say. :) thanks again. Oh I purchased one of the suggested books on here today Programming Principles and Practices using C++, maybe it will fill in the gap between books? Im really good at studying information like your comment out signs and such, its the little unsaid or not well covered material that stumps me.
Well it depends on programmer preference. The underscore is help make the variable name easy to read. Some prefer to use the underscore to separate words while my personal preference is to do it this way (main place I use them is consts and header guards and a few other places depending):
1 2 3 4 5
int thisIsANumber; // camel case -- first letter of every word after the initial word is capital
// this next one also is due to personal preference
constint MAX_WIDTH = 30; // some programmers prefer to do camel case for this too (ie. const int maxWidth = 30; )
Can you tell me why you use run on words such as" thisisanumber" why no space?
You cannot have spaces in your variable names. This is so, because the compiler identifies "tokens" with white space characters. A "token" is the smallest element of a program that is meaningful to the compiler.
For instance:
int number;
int is a token, because it's a keyword, and it's also a data type.
number is a token, because it is separated by a white space character. Since it isn't a reserved keyword, the compiler will assume that it's a variable name (identifier) or something else.
In conclusion, if you write:
int variable name with spaces;
You'll get compilation errors. It's for this reason, that some programmers like to substitute spaces with underscore characters ( _ ).
Xismn - what a wonderful explanation! I totally understood it..lol as for the rest that have replied your explanations were well written and completely understandable as well, Thank you all so very much for your help! I wish I had y'all here with me as I study. :) Your all amazing!!