为什么Android教程中的大多数字段(类成员)都以`m`开头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2092098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do most fields (class members) in Android tutorial start with `m`?
提问by pambuk
I know about camel case rules, but I'm confused with this m rule. What does it stand for? I'm a PHP developer. "We" use first letters of variables as indication of type, like 'b' for boolean, 'i' for integer and so on.
我知道驼峰规则,但我对这个 m 规则感到困惑。它代表什么?我是一名 PHP 开发人员。“我们”使用变量的第一个字母作为类型的指示,例如“b”代表布尔值,“i”代表整数等等。
Is 'm' a Java thing? Does it stand for mobile? mixed?
'm' 是 Java 的东西吗?它代表手机吗?混合?
回答by xiaobing.zhao
This notation comes from AOSP (Android Open Source Project) Code Style Guidelines for Contributors:
此符号来自 AOSP(Android 开源项目)贡献者代码样式指南:
Follow Field Naming Conventions
- Non-public, non-static field names start with m.
- Static field names start with s.
- Other fields start with a lower case letter.
- Public static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES.
遵循字段命名约定
- 非公共、非静态字段名称以 m 开头。
- 静态字段名称以 s 开头。
- 其他字段以小写字母开头。
- 公共静态最终字段(常量)是 ALL_CAPS_WITH_UNDERSCORES。
Note that the linked style guide is for code to be contributed to the Android Open Source Project.
请注意,链接的样式指南适用于要为 Android 开源项目贡献的代码。
It is not a style guide for the code of individual Android apps.
它不是单个 Android 应用程序代码的风格指南。
回答by Kolky
A lot of coding guide lines use m for 'members' of a class. So when you're programming you can see the difference between local and member variables.
许多编码指南使用 m 表示类的“成员”。因此,在编程时,您可以看到局部变量和成员变量之间的区别。
回答by Madan Sapkota
What is mprefix?
什么是m前缀?
mstands for member variable or data member. Use mprefix for non-public and non-static fields.
m代表成员变量或数据成员。m对非公共和非静态字段使用前缀。
When to Use?
何时使用?
private String mCityName;
private float mTemperature;
When not to Use?
什么时候不使用?
public static int mFirstNumber;
public static final String mDATABASE_NAME;
What I do?
我所做的?
Personally, I don't use it. It makes the code more complicated and chaos the readability. If you are still using Notepad for coding I have no words, but modern IDEs are capable of highlighting and coloring member and local variables or anything else.
就个人而言,我不使用它。它使代码更复杂,可读性混乱。如果您仍在使用记事本进行编码,我无话可说,但现代 IDE 能够突出显示和着色成员和局部变量或其他任何东西。
Conclusion
结论
Use? "Yes" or "No" is your personal choice.
用?“是”或“否”是您个人的选择。
回答by ahans
If it's member variables in classes, the 'm' means 'member'. Many Java programmers do that, although with modern IDEs it's not needed since you have highlighting, mouse over tooltips, etc.
如果是类中的成员变量,则“m”表示“成员”。许多 Java 程序员都这样做,尽管在现代 IDE 中不需要这样做,因为您可以突出显示、将鼠标悬停在工具提示上等。
回答by Hamedz
According to Clean Code book, it's not a clean code.
根据 Clean Code book,这不是一个干净的代码。
You don't need to prefix member variables with m. Besides, people quickly learn to ignore the prefix or suffix to see the meaningful part of the name.
您不需要为成员变量加上m前缀。此外,人们很快就会学会忽略前缀或后缀来查看名称中有意义的部分。
回答by user924
If you have problems like
如果你有类似的问题
your IDE to generate setters/getters and you end up with getmName() and setmName()
你的 IDE 来生成 setter/getter,你最终得到 getmName() 和 setmName()
Don't forget to do next (Settings/Editor/Code Style/Java/Code Generation):
不要忘记做下一步(设置/编辑器/代码样式/Java/代码生成):
Update:we don't use something like this in Kotlin (so it's better to switch to it and don't use prefixes anymore)
更新:我们不在 Kotlin 中使用这样的东西(所以最好切换到它并且不再使用前缀)
回答by Steffen J?rgensen
I think it is very individual which code conventions is used. I prefer to name my variables with the following prefixes:
我认为使用哪种代码约定是非常个性化的。我更喜欢使用以下前缀命名我的变量:
- m - Method variables
- c - Class variables
- p - Parameter variables
- m - 方法变量
- c - 类变量
- p - 参数变量
But I guess that each programmer has their own style.
但我想每个程序员都有自己的风格。
回答by Zon
To prove that you definetely shouldn't treat this convention for naming variables in your code, I pass a screenshot from a parent Android Studiohereunder.
为了证明您绝对不应该在代码中使用这种命名变量的约定,我在下面传递了来自父Android Studio的屏幕截图。
Find that variables inside an object a specially sorted to put m-variables lower than your native variables. So by naming them in your code with "m" prefix you hide them in a heap from yourself.
找到对象内的变量,经过特殊排序,使m 变量低于您的本机变量。因此,通过在您的代码中使用“m”前缀命名它们,您可以将它们隐藏在一个堆中。
回答by Janac Meena
The one benefit I found of this code style, is when during an auto-complete of some reference to a variable, I know that I can type "m" to see just the member variables.
我发现这种代码风格的一个好处是,在自动完成对某个变量的某些引用时,我知道我可以键入“m”来仅查看成员变量。
回答by likejudo
It seems to have been a personal preference of some early Android/Google engineers to start member variables with 'm' and so they recommended it.
以“m”开头的成员变量似乎是一些早期 Android/Google 工程师的个人偏好,因此他们推荐了它。
Now this rule is being forced down the throats of developers in companies who are neither AOSP contributors, simply because that page is considered Android Code Style rules. There is little if any benefit in that rule. Google should consider removing it. Otherwise please specify that for Android Apps which of the Code Style Rules are optional.
现在,这条规则被非 AOSP 贡献者的公司的开发人员强加于喉咙,仅仅因为该页面被认为是 Android 代码样式规则。该规则几乎没有任何好处。谷歌应该考虑删除它。否则,请为 Android 应用指定哪些代码样式规则是可选的。
Please add your comment of support to this petition to remove the rule https://code.google.com/p/android/issues/detail?id=226814
请将您的支持评论添加到此请愿书以删除规则https://code.google.com/p/android/issues/detail?id=226814