Naming functions, methods, pointers, variables, arrays etc in C++

萝らか妹 提交于 2020-01-03 02:54:40

问题


Allright, doing some project with few friends, and I need some standard for naming things in c++. Does anyone have any good naming scheme for c++ that is well thought-out and not made in like 10min.

Example, int* house should be named int* house_p, so that when someone reads the code, he doesn't need to scroll all the time wondering if a thing is a pointer, array, matrix, or whatever...

Post your well thought-out naming schemes that you are using !


回答1:


Example, int* house should be named int* house_p, so that when someone reads the code, he doesn't need to scroll all the time wondering if a thing is a pointer, array, matrix, or whatever...

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

ComplicatedDerivativeFinancialInstrument x;

What suffix will you use?

What you are asking about is known as Hungarian notation - its use in C++ is almost universally considered to be A Bad Idea.




回答2:


In C++, when designing/refactoring code, it's quite common to change from a pointer (int*) to a reference (int&). If you had repeated yourself by including a suffix on the variable name indicating that it's a pointer, you would then have to change that to something else, all over the place. That seems like so much pointless make-work, that just makes the code harder to edit and shape into what you want.

You shouldn't need that kind of reminder; it will be quite clear from usage if a variable is a pointer or not, for instance.




回答3:


There are good naming conventions out there, but included type information in the name of the variable(one form is called hungarian notation) is something that is becoming less popular. Most modern IDEs will give you useful information about a variable by just mousing over the variable, such as it's type. However, if it is important to you, hungarian notation is probably what you want.




回答4:


As Neil and aaronis have mentioned, you're talking about a form of Hungarian Notation. Since C++ is a strongly-typed language, it's generally considered a bad idea. It makes it harder to read the code, because there are a bunch of extra characters getting in the way.

The original Hungarian notation was actually used to signal information beyond what types told. For example, let's say I am doing some graphics work and I have points in various coordinate system. They're all of type Point, but some are in object coordinates, some are in camera coordinates, and some are in world coordinates. In that case, it makes good sense to name their respective variables something like oPt, cPt, and wPt.

Historically, this "apps" Hungarian seemed like a good idea (extra character codes to indicate non-type information) to some people and they went way too far turning it into "systems" Hungarian (extra character codes to indicate the C++ type), which is frowned upon these days.




回答5:


If there was a "I Hate Hungarian Notation" group on Facebook, I'd join it.

Here's 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;
}



回答6:


As a general rule, use language of the real-world problem you're trying to solve. Your code will document itself when you use good, descriptive identifiers that make sense to the outside world.

As for syntax, choose a convention that's readable, conveys some information about the identifier, but doesn't lock you into some implementation. Make it simple and easy to remember, so your coders can spend more time coding and less time adhering to complex syntactical requirements. I find the following pretty easy on the eyes and informative enough:

  • Class and Method definitions - First letter capitalized with first letter of concatenated words also capitalized
  • Object instances and function arguments - first letter lower-case with first letter of concatenated words capitalized
  • Class data fields - trailing underscore, first alpha character lower-case with first letter of concatenated words capitalized

I'm afraid it probably doesn't pass the 10-minute test, but if it takes longer than 10 minutes to explain, it's probably getting in the way of real work.

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


来源:https://stackoverflow.com/questions/713797/naming-functions-methods-pointers-variables-arrays-etc-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!