I have to write a code for my boss's presentation. Eventually, technical personnel and software engineers will look at the code. He wants me to perfect it so it looks professional (Do I look unprofessional??) so I have to do what has to be done.
Consider this writing style:
class ClassName // capital letter
{
private:
int var1, var2; // small letter no underscore
double var3;
std::string var4;
protected: // no protected data
public:
ClassName() {}
ClassName(int _var1, int _var2) // definition should be in .h but I will just right it here now
{
this->var1 = _var1; // underscored parameters
this->var2 = _var2;
}
};
I bet this will compile no problem but when it comes to variable naming, underscored prefixes, class structure.. etc, there are plenty of conflicted resources. I made some researches and it got me more confused. I know that at the end of the day it is just an opinion based style or personal perspectives but I only have SO community to get over this issue and I still believe there is a guide or standards for this somewhere:
- Layout of the Recommendations states "Private class variables should have underscore suffix"
- A highly rated answer in Rules about underscore states "never start a variable with an underscore" and the accepted answer states "all underscored variables are reserved"
- The accepted answer in Naming convention states "In C++, an underscore usually indicates a private member variable"
- In Meaning of leading underscores I see that the parameters of the constructor are underscored
- cpp variable naming states that "_ SUM: Underscore at the first position is allowed in C++ language"
Another style would be:
class className // small letter
{
private:
int m_var1, m_var2; // m and underscored
double m_var3;
std::string m_var4;
// no protected section at all
public:
className();
className(int var1, int var2) // should be just prototype in .h and definition in .cpp but I will write here for now
{
this->m_var1 = var1; // no naming style for parameters
this->m_var2 = var2;
}
};
The newest question/answer is maybe 5 years old (it doesn't really matter). But I would like to see rates and answers about this issue. When to use _variable
?
When to use variable_
?
When to use variable
?
When to use m_variable
?
Should I always follow a class structure even if I don't have data (like an "empty" protected
)? Variable and class names are upper case or lower case when and why? How would a professional programmer/developer write the above example code?
"Added C# because I think both languages have the same standards in this issue"