Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good naming convention for large-scope function variables?

You can have different naming convention for class members, static objects, global objects, and structs. Some of the examples of them are as below.

_member
m_member

or in Java case, the usage of this.member.

But is there any good technique or naming convention for function variables scope that conveys when a single variable has complete function scope or a short lifespan scope?

void MyFunction()
{
  int functionScopeVariable;

  if(true)
  {
    //no need for function variable scope naming convention
  }
}
like image 230
Chad Avatar asked Dec 03 '22 09:12

Chad


2 Answers

I actually encourage delegating this task to the IDE/editor you use.

No, I'm not actually talking about naming variables, that is still best done by a human. But the underlying task of such naming strategies is to show you which type of variable any one name represents.

Pretty much every IDE worth its salt can define different styles (colors, fonts, font types, ...) to different variable types (instance member, static member, argument, local variable, ...) so letting the IDE tell you what type of variable it is actually frees you from having to type those (otherwise useless) pre- or suffixes every time.

So my suggestion: use meaningful names without any prefix or suffix.

like image 59
Joachim Sauer Avatar answered Jan 14 '23 15:01

Joachim Sauer


One method is to follow the guideline that the larger the scope of the variable, the longer the name. In this way, global variables get long descriptive names while scope-limited things like loop index variable can be as small as single letters.

like image 30
Greg Hewgill Avatar answered Jan 14 '23 13:01

Greg Hewgill