Naming functions, methods, pointers, variables, arrays, etc. In C ++

Okay, doing a few projects with a few friends and I need some kind of standard for naming things in C ++. Does anyone have a good naming scheme for C ++ that is well thought out and not done in 10min.

For example, int * house should be called int * house_p so that when someone reads the code they don't have to scroll all the time wondering if the thing is a pointer, an array, a matrix, or whatever ...

Post your thoughtful naming schemes you use!

+1


source to share


8 answers


For example, int * house should be named int * house_p so that when someone reads the code, they don't need to loop all the time wondering if the thing is a pointer, array, matrix or whatever ...

But what if its type changes - you are going to go through all your code and change the names of all variables. And what if the variable is an instance of a complex type:

ComplicatedDerivativeFinancialInstrument x;

      



What suffix are you using?

What you are asking is called Hungarian notation - using it in C ++ is almost universally considered a bad idea.

+20


source


In C ++, when developing / refactoring code, it is quite common to change a pointer ( int*

) to a reference ( int&

). If you were to repeat yourself by including a suffix in the variable name indicating that it is a pointer, then you would have to change that to something else, everything above the place. This seems so pointless that it just makes the code harder to edit and shape what you want.



You don't need such a reminder; it will be quite clear from usage if the variable is a pointer or not, for example.

+5


source


+2


source


There are good naming conventions, but they include type information in the variable name (one form is called Hungarian notation ) which is becoming less popular. Most modern IDEs will give you useful information about a variable by simply binding the variable, such as its type. However, if this is important to you, Hungarian notation is probably what you want.

+1


source


As Neil and Aaronis mentioned, you are talking about the form of Hungarian notation . Since C ++ is a strongly typed language, it is generally considered bad. This makes the code difficult to read because there are a few extra characters that get in the way.

The original Hungarian notation was actually used to signal information other than what was said. For example, let's say that I am a graphic artist and I have points in different coordinate systems. They are all Point, but some are in object coordinates, some are in camera coordinates, and some are in world coordinates. In this case, it makes sense to name their respective variables such as oPt, cPt and wPt.

Historically, this Hungarian "application" seemed like a good idea (extra character codes indicating non-type information) to some people, and they turned it too far into Hungarian "systems" (extra character codes to denote a C ++ type), which frowned these days.

+1


source


+1


source


Generally, use the language of the real world problem you are trying to solve. Your code will document itself when you use good, descriptive identifiers that make sense to the outside world.

As far as syntax is concerned, choose a convention that reads, conveys some information about the identifier, but doesn't block you in some implementation. Keep it simple and memorable, so your coders can spend more time coding and less time adhering to complex syntax requirements. I find the following fairly easy on the eyes and quite informative:

  • Class and method definition - letter first, capitalized with first letter of concatenated words also capitalized
  • Object instances and function arguments - lowercase alphabetic first with first letter of concatenated words, uppercase
  • Class data fields - trailing underscore, first alpha character lower case with first letter concatenated words with capital letter

I'm afraid it probably won't pass the 10 minute test, but if it takes more than 10 minutes to explain it, it probably interferes with real work.

class FooProblem {
  int fooData_;
  public:
  FooProblem(int fooData): fooData_(fooData){}
};

      

0


source


If there was a group on Facebook "I hate Hungarian notation", I would join it.

Here is some psudocode that illustrates my standard.

namespace utilities
{

class MyGizmo
{
public :
   void doTheThing();
   std::string name_;
private:
   int thingCount_;
};

static const char* ApplicationName = "Gizmotronic Ultra 1.0";

void setGizmoName(MyGizmo& gizmo, const std::string & name)
{
   gizmo.name_ = name;
}

int main()
{
   cout << ApplicationName;
   MyGizmo gizmo;
   gizmo.doTheThing();

   std::string gizmoName = gizmo.name_;

   return 0;
}

      

0


source







All Articles