2

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:

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"

Community
  • 1
  • 1
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • 1
    Your organization should have a style guide. Follow it. If they don't have a style guide then either (1) they don't care, or (2) they do care and no one has bothered to write one. A style guide doesn't have to be long; it can be shorter than this question. If your org is lacking a style guide, **write one**, and if people complain about it, hey, that's volunteerism right there. Make them own it. – Eric Lippert Apr 28 '16 at 15:17
  • 1
    Professionalism is consistency. If your style works for you, it'll work for others. Just apply it consistently across your presentation. Don't try to be something you're not. – Richard Hodges Apr 28 '16 at 15:17
  • 1
    Richard is right; I would add: if you are modifying existing code, follow the style in the code. I would also add: obtain a good code formatter, run it frequently, and trust it to do a good job. – Eric Lippert Apr 28 '16 at 15:18

0 Answers0