成员变量使用什么样的前缀?

发布于 2024-07-06 12:13:54 字数 500 浏览 10 评论 0原文

毫无疑问,对于理解代码来说,给成员变量一个前缀至关重要,以便可以轻松地将它们与“普通”变量区分开来。

但是你使用什么样的前缀呢?

我一直在使用 m_ 作为前缀的项目,在其他项目中我们仅使用下划线(我个人不喜欢,因为仅使用下划线不够说明性)。

在另一个项目中,我们使用了长前缀形式,其中还包括变量类型。 例如,mul_unsigned long 类型的 member 变量的前缀。

现在让我知道您使用哪种前缀(并请给出原因)。

编辑:你们中的大多数人似乎在编码时没有为成员变量添加特殊前缀! 这取决于语言吗? 根据我的经验,C++ 代码倾向于使用下划线或m_ 作为成员变量的前缀。 其他语言呢?

No doubt, it's essential for understanding code to give member variables a prefix so that they can easily be distinguished from "normal" variables.

But what kind of prefix do you use?

I have been working on projects where we used m_ as prefix, on other projects we used an underscore only (which I personally don't like, because an underscore only is not demonstrative enough).

On another project we used a long prefix form, that also included the variable type. mul_ for example is the prefix of a member variable of type unsigned long.

Now let me know what kind of prefix you use (and please give a reason for it).

EDIT: Most of you seem to code without special prefixes for member variables! Does this depend on the language? From my experience, C++ code tends to use an underscore or m_ as a prefix for member variables. What about other languages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

他不在意 2024-07-13 12:13:54

毫无疑问,对于理解代码来说,为成员变量赋予前缀至关重要,以便可以轻松地将它们与“普通”变量区分开来。

我对这种说法提出异议。 如果你的语法高亮还算不错,那么这根本就不是必要的。 一个好的 IDE 可以让您用可读的英语编写代码,并可以通过其他方式向您显示符号的类型和范围。 当插入点位于其中一个符号上时,Eclipse 会突出显示符号的声明和使用,从而做得很好。

编辑,谢谢 slim:像 Eclipse 这样的良好语法荧光笔还可以让您使用粗体或斜体文本,或者完全更改字体。 例如,我喜欢用斜体来表示静态的东西。

另一个编辑:这样想; 变量的类型和范围是次要信息。 它应该是可用的并且很容易找到,但不会对你大喊大叫。 如果您使用像 m_ 这样的前缀或像 LPCSTR 这样的类型,当您只想读取主要信息(代码的意图)时,它们就会变成噪音。

第三次编辑:无论语言如何,这都适用。

No doubt, it's essential for understanding code to give member variables a prefix so that they can easily be distinguished from "normal" variables.

I dispute this claim. It's not the least bit necessary if you have half-decent syntax highlighting. A good IDE can let you write your code in readable English, and can show you the type and scope of a symbol other ways. Eclipse does a good job by highlighting declarations and uses of a symbol when the insertion point is on one of them.

Edit, thanks slim: A good syntax highlighter like Eclipse will also let you use bold or italic text, or change fonts altogether. For instance, I like italics for static things.

Another edit: Think of it this way; the type and scope of a variable are secondary information. It should be available and easy to find out, but not shouted at you. If you use prefixes like m_ or types like LPCSTR, that becomes noise, when you just want to read the primary information – the intent of the code.

Third edit: This applies regardless of language.

半窗疏影 2024-07-13 12:13:54

我根本不使用任何前缀。 如果我遇到将局部变量或方法参数与类成员混淆的危险,那么方法或类就会太长,并且可以从拆分中受益

这(可以说)不仅使代码更具可读性并且有些“流畅”,而且最重要的是鼓励结构良好的类和方法。 最终,它归结为一个与前缀或无前缀困境完全不同的问题。

更新:嗯,品味和偏好会改变,不是吗。我现在使用下划线作为成员变量的前缀,因为从长远来看,它已被证明有利于识别本地变量和成员变量。 尤其是新团队成员有时会因为两人不容易被认出而感到困难。

I do not use any prefix at all. If I run into danger of mixing up local variables or method parameters with class members, then either the method or the class is too long and benefits from splitting up.

This (arguably) not only makes the code more readable and somewhat "fluent", but most importantly encourages well structured classes and methods. In the end, it thus boils down to a completely different issue than the prefix or no-prefix dillema.

UPDATE: well, taste and preferences change, don't they.. I now use underscore as the prefix for member variables as it has proven to be beneficial in recognizing local and member variables in the long run. Especially new team members sometimes have hard time when the two are not easily recognizable.

忆依然 2024-07-13 12:13:54

没有任何。 我曾经使用过下划线,但是在一个其他人不喜欢它的项目中被劝说放弃了,并且没有错过它。 一个好的 IDE 或一个好的记忆力会告诉你什么是成员变量,什么不是。 我们项目的一位开发人员坚持要加上“这个”。 在每个成员变量前面,当我们处理名义上“他的”代码区域时,我们会迁就他。

None. I used to use underscore, but was talked out of it on a project where the others didn't like it, and haven't missed it. A decent IDE or a decent memory will tell you what's a member variable and what isn't. One of the developers on our project insists on putting "this." in front of every member variable, and we humour him when we're working on areas of code that are nominally "his".

柏拉图鍀咏恒 2024-07-13 12:13:54

仅下划线。

就我而言,我使用它是因为这就是我工作场所的编码标准文档的规定。 但是,我看不出在变量开头添加 m_ 或一些可怕的匈牙利语的意义。 极简主义的“仅下划线”使其具有可读性。

Underscore only.

In my case, I use it because that's what the coding standards document says at my workplace. However, I cannot see the point of adding m_ or some horrible Hungarian thing at the beginning of the variable. The minimalist 'underscore only' keeps it readable.

痴者 2024-07-13 12:13:54

保持一致比任何事情都更重要,所以选择你和你的队友可以同意的事情并坚持下去。 如果您编码所用的语言有约定,您应该尝试遵守它。 没有什么比遵循不一致的前缀规则的代码库更令人困惑的了。

对于 C++,除了 _ 有时作为编译器关键字前缀之外,还有另一个原因更喜欢 m_ 而不是 _。 m代表成员变量。 这还使您能够消除局部变量和其他变量类别之间的歧义,s_ 表示静态变量,g_ 表示全局变量(但当然不要使用全局变量)。

至于 IDE 将永远照顾您的评论,IDE 真的是您查看代码的唯一方式吗? 您的 diff 工具是否具有与 IDE 相同的语法高亮质量水平? 您的源代码管理修订历史记录工具怎么样? 您甚至从未将源文件cat到命令行吗? 现代 IDE 是出色的效率工具,但无论您在什么上下文中阅读代码,都应该易于阅读。

It's more important to be consistent than anything, so pick something you and your teammates can agree upon and stick with it. And if the language you're coding in has a convention, you should try to stick to it. Nothing's more confusing than a code base that follows a prefixing rule inconsistently.

For c++, there's another reason to prefer m_ over _ besides the fact that _ sometimes prefixes compiler keywords. The m stands for member variable. This also gives you the ability disambiguate between locals and the other classes of variables, s_ for static and g_ for global (but of course don't use globals).

As for the comments that the IDE will always take care of you, is the IDE really the only way that you're looking at your code? Does your diff tool have the same level of quality for syntax hilighting as your IDE? What about your source control revision history tool? Do you never even cat a source file to the command line? Modern IDE's are fantastic efficiency tools, but code should be easy to read regardless of the context you're reading it in.

撧情箌佬 2024-07-13 12:13:54

我更喜欢使用 this 关键字。
这意味着 this.datathis->data 而不是一些依赖于社区的命名。

因为:

  • 在当今的 IDE 中,输入 this. 弹出 intellinsense
  • 对于每个人来说都是显而易见的,而不知道定义的命名

BTW 用字母前缀变量来表示其类型对于好的 IDE 来说已经过时了,并让我想起了这个 乔尔的文章

I prefer using this keyword.
That means this.data or this->data instead of some community-dependent naming.

Because:

  • with nowadays IDEs typing this. popups intellinsense
  • its obvious to everyone without knowing defined naming

BTW prefixing variables with letters to denote their type is outdated with good IDEs and reminds me of this Joel's article

抹茶夏天i‖ 2024-07-13 12:13:54

我们使用 m_,然后稍微修改一下西蒙尼表示法,就像 Rob 在之前的回复中所说的那样。 因此,前缀似乎很有用,而且 m_ 不太具有侵入性并且很容易搜索。

为什么要使用符号呢? 为什么不遵循(对于 .NET)依赖于名称大小写的 Microsoft 表示法建议呢?

首先是后一个问题:正如所指出的,VB.NET 对大小写无关。 数据库和(尤其是)DBA 也是如此。 当我必须直接保存 customerID 和 CustomerID(例如,C# 中)时,我的大脑很受伤。 所以大小写是一种表示法,但不是一种非常有效的表示法。

前缀表示法具有以下几个方面的价值:

  1. 无需使用 IDE 即可提高人们对代码的理解。 就像代码审查一样——我仍然发现最初在纸上进行是最容易的。
  2. 曾经编写过 T-SQL 或其他 RDBMS 存储过程吗? 在数据库列名上使用前缀表示法确实很有帮助,特别是对于我们这些喜欢使用文本编辑器进行此类操作的人来说。

也许简而言之,前缀作为一种表示法很有用,因为仍然存在无法使用智能 IDE 的开发环境。 将 IDE(一种软件工具)视为允许我们使用一些快捷方式(例如智能感知输入),但不包含整个开发环境。

IDE 是一种集成开发环境,就像汽车是交通网络一样:只是更大系统的一部分。 我不想遵循“汽车”惯例,比如留在有标记的道路上,有时,步行穿过空地会更快。 依靠 IDE 来跟踪变量类型就像需要汽车的 GPS 来穿过空地一样。 当然,以可移植的形式掌握这些知识(尽管可能会很尴尬,因为“m_intCustomerID”可能很尴尬)比每次微小的改变都跑回车上要好。

也就是说,m_ 约定或“this”约定都是可读的。 我们喜欢 m_,因为它很容易搜索,并且仍然允许变量类型跟随它。 同意太多其他框架代码活动使用了简单的下划线。

We use m_ and then a slightly modified Simonyi notation, just like Rob says in a previous response. So, prefixing seems useful and m_ is not too intrusive and easily searched upon.

Why notation at all? And why not just follow (for .NET) the Microsoft notation recommendations which rely upon casing of names?

Latter question first: as pointed out, VB.NET is indifferent to casing. So are databases and (especially) DBAs. When I have to keep straight customerID and CustomerID (in, say, C#), it makes my brain hurt. So casing is a form of notation, but not a very effective one.

Prefix notation has value in several ways:

  1. Increases the human comprehension of code without using the IDE. As in code review -- which I still find easiest to do on paper initially.
  2. Ever write T-SQL or other RDBMS stored procs? Using prefix notation on database column names is REALLY helpful, especially for those of us who like using text editors for this sort of stuff.

Maybe in short, prefixing as a form of notation is useful because there are still development environments where smart IDEs are not available. Think about the IDE (a software tool) as allowing us some shortcuts (like intellisense typing), but not comprising the whole development environment.

An IDE is an Integrated Development Environment in the same way that a car is a Transportation Network: just one part of a larger system. I don't want to follow a "car" convention like staying on marked roads, when sometimes, its faster just to walk through a vacant lot. Relying on the IDE to track variable typing would be like needing the car's GPS to walk through the vacant lot. Better to have the knowledge (awkward though it may be to have "m_intCustomerID") in a portable form than to run back to the car for every small change of course.

That said, the m_ convention or the "this" convention are both readable. We like m_ because it is easily searched and still allows the variable typing to follow it. Agreed that a plain underscore is used by too many other framework code activities.

漫漫岁月 2024-07-13 12:13:54

使用 C#,我已将“m_”前缀改为仅一个下划线,因为“m_”是继承自 C++

官方的 Microsoft 指南告诉您不要使用任何前缀,并在私有成员上使用驼峰命名法在公共成员上使用帕斯卡命名法。 问题在于,这与同一来源的另一条准则相冲突,该准则规定您应该使所有代码与 .NET 中使用的所有语言兼容。 例如,VB.NET 不区分大小写。

所以对我来说只是一个下划线。 这也使得通过 IntelliSense 访问变得容易,并且仅调用公共成员的外部代码不必看到视觉上混乱的下划线。

更新:我不认为C#“this.”前缀对“Me”有帮助。 在 VB 中,“Me.age”仍然与“Me.Age”相同。

Using C#, I've moved from the 'm_'-prefix to just an underscore, since 'm_' is an heritage from C++.

The official Microsoft Guidelines tells you not to use any prefixes, and to use camel-case on private members and pascal-case on public members. The problem is that this collides with another guideline from the same source, which states that you should make all code compatible with all languages used in .NET. For instance, VB.NET doesn't make a difference between casings.

So just an underscore for me. This also makes it easy to access through IntelliSense, and external code only calling public members don't have to see the visually messy underscores.

Update: I don't think the C# "this."-prefix helps out the "Me." in VB, which will still see "Me.age" the same as "Me.Age".

給妳壹絲溫柔 2024-07-13 12:13:54

这取决于我使用哪个框架! 如果我正在编写 MFC 代码,那么我会使用 m_ 和匈牙利表示法。 对于其他东西(通常是 STL/Boost),我会为所有成员变量添加下划线后缀,并且我不关心匈牙利表示法。

MFC 类

class CFoo  
{  
private:  
    int m_nAge;  
    CString m_strAddress;  
public:  
    int GetAge() const { return m_nAge; }  
    void SetAge(int n) { m_nAge = n; }  
    CString GetAddress() const { return m_strAddress;  
    void SetAddress(LPCTSTR lpsz) { m_strAddress = lpsz; }  
};

STL 类

class foo  
{  
private:  
    int age_;  
    std::string address_;  
public:  
    int age() const { return age_; }  
    void age(int a) { age_ = a; }  
    std::string address() const { return address_; }  
    void address(const std::string& str) { address_ = str; }  
};

现在这可能看起来有点奇怪 - 两种不同的样式 - 但它对我有用,并且编写了很多不使用相同样式的 MFC 代码MFC本身的风格只是看起来很丑。

It depends on which framework I'm using! If I'm writing MFC code then I use m_ and Hungarian notation. For other stuff (which tends to be STL/Boost) then I add an underscore suffix to all member variables and I don't bother with Hungarian notation.

MFC Class

class CFoo  
{  
private:  
    int m_nAge;  
    CString m_strAddress;  
public:  
    int GetAge() const { return m_nAge; }  
    void SetAge(int n) { m_nAge = n; }  
    CString GetAddress() const { return m_strAddress;  
    void SetAddress(LPCTSTR lpsz) { m_strAddress = lpsz; }  
};

STL Class

class foo  
{  
private:  
    int age_;  
    std::string address_;  
public:  
    int age() const { return age_; }  
    void age(int a) { age_ = a; }  
    std::string address() const { return address_; }  
    void address(const std::string& str) { address_ = str; }  
};

Now this may seem a bit odd - two different styles - but it works for me, and writing a lot of MFC code that doesn't use the same style as MFC itself just looks ugly.

时光病人 2024-07-13 12:13:54

这实际上取决于语言。
我是一名 C++ 人员,在所有内容前添加下划线前缀有点棘手。 该语言保留以下划线开头的内容,以便在某些情况下实现(取决于范围)。 对于双下划线或下划线后跟大写字母也有特殊处理。 所以我说,只要避免这种混乱,只需选择其他一些前缀即可。 在我看来,“m”是可以的。 “m_”有点多,但也不可怕。 确实是品味问题。

但要注意那些 _leadingUnderscores。 您会惊讶地发现有多少编译器和库内部是这样命名的,如果您不非常小心,肯定有可能发生意外和混淆。 拒绝吧。

It really depends on the language.
I'm a C++ guy, and prefixing everything with underscore is a bit tricky. The language reserves stuff that begins with underscore for the implementation in some instances (depending on scope). There's also special treatment for double underscore, or underscore following by a capital letter. So I say just avoid that mess and simply choose some other prefix. 'm' is ok IMO. 'm_' is a bit much, but not terrible either. A matter of taste really.

But watch out for those _leadingUnderscores. You'll be surprised how many compiler and library internals are so named, and there's definitely room for accidents and mixup if you're not extremely careful. Just say no.

谁对谁错谁最难过 2024-07-13 12:13:54

我在成员变量前加上“m”前缀,在参数(函数中)前加上“p”前缀。 所以代码看起来像:

class SomeClass {
    private int mCount;
    ...
    private void SomeFunction(string pVarName) {...}
}

我发现这可以快速告诉您任何变量的基本范围 - 如果没有前缀,那么它是本地变量。 此外,在读取函数时,您不需要考虑传入的内容以及局部变量。

I prefix member variables with 'm' and parameters (in the function) with 'p'. So code will look like:

class SomeClass {
    private int mCount;
    ...
    private void SomeFunction(string pVarName) {...}
}

I find that this quickly tells you the basic scope of any variable - if no prefix, then it's a local. Also, when reading a function you don't need to think about what's being passed in and what's just a local variable.

岛歌少女 2024-07-13 12:13:54

大多数时候,我使用Python。 Python 要求您使用 self.foo 才能访问当前类实例的属性 foo。 这样,您所处理的实例的局部变量、参数和属性混淆的问题就解决了。
一般来说,我喜欢这种方法,尽管我不喜欢被迫这样做。 因此,我的理想方法是不这样做,而是在 this 或 self 上使用某种形式的属性访问来获取成员变量。 这样,我就不必用元数据来混淆名称。

Most of the time, I use python. Python requires you to use self.foo in order to access the attribute foo of the instance of the current class. That way, the problem of confusing local variables, parameters and attributes of the instance you work on is solved.
Generally, I like this approach, even though I dislike being forced to do it. Thus, my ideal way to do thos is to not do it and use some form of attribute access on this or self in order to fetch the member variables. That way, I don't have to clutter the names with meta-data.

纵情客 2024-07-13 12:13:54

我很奇怪,我在成员变量前加上类名(驼峰式)的首字母缩写。

TGpHttpRequest = class(TOmniWorker)
strict private
  hrHttpClient  : THttpCli;
  hrPageContents: string;
  hrPassword    : string;
  hrPostData    : string;

大多数Delphi人只使用F。

TGpHttpRequest = class(TOmniWorker)
strict private
  FHttpClient  : THttpCli;
  FPageContents: string;
  FPassword    : string;
  FPostData    : string;

I'm weirdo and I prefix member variables with initials from the class name (which is camel-cased).

TGpHttpRequest = class(TOmniWorker)
strict private
  hrHttpClient  : THttpCli;
  hrPageContents: string;
  hrPassword    : string;
  hrPostData    : string;

Most of the Delphi people just use F.

TGpHttpRequest = class(TOmniWorker)
strict private
  FHttpClient  : THttpCli;
  FPageContents: string;
  FPassword    : string;
  FPostData    : string;
三寸金莲 2024-07-13 12:13:54

如果语言支持 thisMe 关键字,则不使用前缀,而是使用所述关键字。

If the language supports the this or Me keyword, then use no prefix and instead use said keyword.

月亮坠入山谷 2024-07-13 12:13:54

另一个技巧是命名约定

所有成员变量都像往常一样命名,没有任何前缀(或“this.”,在项目中通常这样做),

但它们很容易与局部变量区分开来因为在我的项目中,这些局部变量总是命名为:

  • aSomething:代表一个对象。
  • someManyThings:对象列表。
  • isAState 或 hasSomeThing:用于布尔状态。

任何不以“a”、“some”或“is/has”开头的变量都是成员变量。

another trick is naming convention:

All member variables are named as usual, without any prefix (or 'this.' is it is usual to do so in the project)

But they will be easily differentiated from local variable because in my project, those local variables are always named:

  • aSomething: represents one object.
  • someManyThings: list of objects.
  • isAState or hasSomeThing: for boolean state.

Any variable which does not begin by 'a', 'some' or 'is/has' is a member variable.

归属感 2024-07-13 12:13:54

由于 VB.NET 不区分大小写,因此我在成员变量前添加下划线前缀,并在名称的其余部分中使用驼峰式大小写。 我将属性名称大写。

Dim _valueName As Integer

Public Property ValueName() As Integer

Since VB.NET is not case-sensitive, I prefix my member variables with an underscore and camel case the rest of the name. I capitalize property names.

Dim _valueName As Integer

Public Property ValueName() As Integer
老旧海报 2024-07-13 12:13:54

我和那些不使用前缀的人在一起。

现在的 IDE 非常好,从语法着色、鼠标悬停工具提示和轻松导航到其定义,可以轻松一目了然地找到有关变量的信息。

这是您可以从变量和命名约定的上下文中获得的内容(例如局部变量和私有字段的小驼峰命名法,属性和方法的大驼峰命名法等)以及布尔值的“hasXXXX”和“isXX”等内容。

我已经很多年没有使用前缀了,但我确实曾经是“this”。 前缀怪物,但除非绝对必要,否则我已经放弃了(谢谢,Resharper)。

I'm with the people that don't use prefixes.

IDEs are so good nowadays, it's easy to find the information about a variable at a glance from syntax colouring, mouse-over tooltips and easy navigation to its definition.

This is on top of what you can get from the context of the variable and naming conventions (such as lowerCamelCase for local variables and private fields, UpperCamelCase for properties and methods etc) and things like "hasXXXX" and "isXX" for booleans.

I haven't used prefixes for years, but I did used to be a "this." prefix monster but I've gone off that unless absolutely necessary (thanks, Resharper).

枯寂 2024-07-13 12:13:54

单个 _ 仅用作视觉指示器。 (C#)

  • 有助于通过智能感知对成员进行分组。
  • 阅读代码时更容易发现成员变量。
  • 更难隐藏具有局部定义的成员变量。

A single _ used only as a visual indicator. (C#)

  • helps to group members with intellisense.
  • easier to spot the member variables when reading the code.
  • harder to hide a member variable with a local definition.
椒妓 2024-07-13 12:13:54

_ 而不是 this.

我也使用 _ 而不是 this. 因为只是更短 strong>(少 4 个字符),它是成员变量的一个很好的指示符。 此外,使用这个前缀可以避免命名冲突。 示例:

public class Person {
  private String _name;

  public Person(String name) {
    _name = name;
  }
}

与此相比:

public class Person {
  private String name;

  public Person(String name) {
    this.name = name;
  }
}

我发现第一个示例更短、更清晰。

_ instead of this.

I use _ too instead of this. because is just shorter (4 characters less) and it's a good indicator of member variables. Besides, using this prefix you can avoid naming conflicts. Example:

public class Person {
  private String _name;

  public Person(String name) {
    _name = name;
  }
}

Compare it with this:

public class Person {
  private String name;

  public Person(String name) {
    this.name = name;
  }
}

I find the first example shorter and more clear.

岁月静好 2024-07-13 12:13:54

这有点取决于您使用的语言。

在 C# 中,您可以使用“this”前缀引用任何成员,例如“this.val”,这意味着不需要前缀。 VB 具有与“Me”类似的功能。

在有用于指示成员访问的内置符号的语言中,我不认为使用前缀有什么意义。 在其他语言中,我认为使用该语言普遍接受的约定是有意义的。

请注意,使用内置表示法的好处之一是,您还可以在访问类上的属性和方法时使用它,而不会影响这些属性和方法的命名约定(这在访问非私有成员时尤其重要)。 使用任何类型的指示器的主要原因是作为一个标志,表明您可能在类中造成副作用,因此在使用其他成员时最好使用它,无论它们是否是字段/属性/方法/等。

It kinda depends what language you're working in.

In C# you can reference any member using the 'this' prefix, e.g. 'this.val', which means no prefixes are needed. VB has a similar capability with 'Me'.

In languages where there is a built-in notation for indicating member access I don't see the point in using a prefix. In other languages, I guess it makes sense to use whatever the commonly accepted convention is for that language.

Note that one of the benefits of using a built-in notation is that you can also use it when accessing properties and methods on the class without compromising your naming conventions for those (which is particularly important when accessing non-private members). The main reason for using any kind of indicator is as a flag that you are causing possible side effects in the class, so it's a good idea to have it when using other members, irrespective of whether they are a field/property/method/etc.

七分※倦醒 2024-07-13 12:13:54

我像这里的许多人一样使用驼峰式大小写和下划线。 我使用下划线是因为我使用 C# 并且已经习惯在构造函数中避免使用“this”关键字。 我使用驼峰式大小写方法范围的变体,因此下划线提醒我当时正在使用的范围。 否则,只要您不尝试添加代码中已经明显的不必要的信息,我认为这并不重要。

I use camel case and underscore like many here. I use the underscore because I work with C# and I've gotten used to avoiding the 'this' keyword in my constructors. I camel case method-scoped variants so the underscore reminds me what scope I'm working with at the time. Otherwise I don't think it matters as long as you're not trying to add unnecessary information that is already evident in code.

找个人就嫁了吧 2024-07-13 12:13:54

我曾经在 C++ 中使用 m_ perfix,但在 C# 中我更喜欢仅对字段使用驼峰式大小写,对其属性使用帕斯卡大小写。

private int fooBar;
public int FooBar
{
  get { return fooBar; }
  set { fooBar = value; }
}

I've used to use m_ perfix in C++ but in C# I prefer just using camel case for the field and pascal case for its property.

private int fooBar;
public int FooBar
{
  get { return fooBar; }
  set { fooBar = value; }
}
街道布景 2024-07-13 12:13:54

我喜欢 m_ 但只要在代码库中使用约定,我就很乐意。

I like m_ but as long as convention is used in the code base is used I'm cool with it.

亣腦蒛氧 2024-07-13 12:13:54

您的 mul_ 示例正朝着 Charles Simonyi 的 Apps Hungarian notation 方向发展。

我更喜欢保持简单,这就是为什么我喜欢使用 m_ 作为前缀。

这样做可以更容易地了解您必须去哪里查看原始声明。

Your mul_ example is heading towards Charles Simonyi's Apps Hungarian notation.

I prefer keeping things simple and that's why I like using m_ as the prefix.

Doing this makes it much easier to see where you have to go to see the original declaration.

待天淡蓝洁白时 2024-07-13 12:13:54

我倾向于在 C++ 中使用 m_,但不介意在 Java 或 C# 中将其保留。 这取决于编码标准。 对于混合有下划线和 m_ 的遗留代码,我会将代码重构为一个标准(给定合理的代码大小)

I tend to use m_ in C++, but wouldn't mind to leave it away in Java or C#. And it depends on the coding standard. For legacy code that has a mixture of underscore and m_ I would refactor the code to one standard (given a reasonable code size)

染年凉城似染瑾 2024-07-13 12:13:54

我用 @。

:D j/k ——但是这确实取决于语言。 如果它有 getter/setter,我通常会在私有成员变量前面放一个 _,并且 getter/setter 将具有相同的名称,但不带 _。 否则,我通常不会使用任何东西。

I use @.

:D j/k -- but if does kind of depend on the language. If it has getters/setters, I'll usually put a _ in front of the private member variable and the getter/setter will have the same name without the _. Otherwise, I usually don't use any.

在巴黎塔顶看东京樱花 2024-07-13 12:13:54

对于我自己的项目,我使用 _ 作为后缀(正如 Martin York 上面指出的那样,_ 作为前缀是编译器实现的 C/C++ 标准的保留符),而在处理 Symbian 项目

For my own projects I use _ as a postfix (as Martin York noted above, _ as a prefix is reserver by the C/C++ standard for compiler implementations) and i when working on Symbian projects.

香橙ぽ 2024-07-13 12:13:54

在 Java 中,一种常见的约定是在成员变量前加上“my”并使用 CamelCaseForTheRestOfTheVariableName。

In Java, one common convention is to preface member variables with "my" andUseCamelCaseForTheRestOfTheVariableName.

贱贱哒 2024-07-13 12:13:54

如果没有必要就没有,否则单下划线。 适用于Python。

None if it's not necessary, single underscore otherwise. Applies for python.

巴黎夜雨 2024-07-13 12:13:54

如果确实需要为成员变量添加前缀,我肯定会更喜欢 m_ 而不是仅使用下划线。 我发现下划线本身会降低可读性,并且可能与 C++ 保留字混淆。

但是,我确实怀疑成员变量是否需要任何特殊符号。 即使忽略 IDE 帮助,也不清楚为什么本地变量和成员变量之间会出现混淆。

If it is really necessary to prefix member variables, I would definitely prefer m_ to just an underscore. I find an underscore on its own reduces readability, and can be confused with C++ reserved words.

However, I do doubt that member variables need any special notation. Even ignoring IDE help, it isn't obvious why there would be confusion between what is a local and what is a member variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文