I was wondering what the best practices are in C++ with regards to declaring variables.
Usually in C# I declare variables as camelCasing but I was wondering what the rule is in C++. What is the best method below
1 2 3 4 5 6 7 8
1. int thisIsAVariable = 0;
2. int this_is_a_variable = 0;
3. int _thisIsAVariable = 0;
4. int _this_is_a_variable = 0;
5. int __thisIsAVariable = 0;
6. int __this_is_a_variable = 0;
I have seen many variations of declaring variables and wondering what the general rule was? Also does it make adifference if I declare them in a class?
Technically, it makes no difference. Practically, it's all about readability. Camel casing is a must, but other than that, just make sure you're consistent and you'll be fine.
Personally, I hate underscores in names, but if you find them handy, by all means do so. However, I do believe using underscores at the start is used by "official" headers, thus runs the risk of reusing an already defined name from an include (which is incredibly difficult to debug unless you have a very good IDE).
One exception: I sometimes use temporary functions/variables as placeholders or to test a different part of the code. These, I prefix with TEST_*, because it's easy to spot due to caps and the underscore, which immediately trigger my spidey-senses.