[英]Naming convention for Win32/MFC with C++
I am working on an application which uses Win32/MFC framework and C++ programming language using Visual Studio 2010. 我正在使用Visual Studio 2010使用Win32 / MFC框架和C ++编程语言的应用程序。
My question is which naming convention needs to use for variables, function name, class name and etc. I heard Microsoft suggest to use “Hungarian notation” naming convention. 我的问题是变量,函数名,类名等应使用哪种命名约定。我听说微软建议使用“匈牙利表示法”命名约定。
Can you please let me know which standard to use? 您能告诉我使用哪种标准吗?
You might have a truly good reason to want your code to look familiar to other programmers who have learned MFC. 您可能确实有充分的理由希望您的代码对学习过MFC的其他程序员看起来很熟悉。 If so, then you could emulate the conventions of the MFC Samples and Tutorials , and do what they do... 如果是这样,那么您可以效仿MFC Samples and Tutorials的约定,并执行它们的工作...
But MFC is a legacy library which originated very early, and times have changed. 但是MFC是最早起源的旧式库,并且时代已经改变。 Microsoft's style guidelines now specifically say NOT to use Hungarian notation: 现在,Microsoft的样式准则明确规定不要使用匈牙利表示法:
http://msdn.microsoft.com/en-us/library/ms229042.aspx http://msdn.microsoft.com/zh-CN/library/ms229042.aspx
That aside, rather than blindly following any particular document, it is probably better to evaluate each individual convention. 此外,与其盲目地遵循任何特定文件,不如评估每个单独的公约可能更好。 For instance, when MFC samples make classes and have member variables, they start the name with m_
. 例如,当MFC示例创建类并具有成员变量时,它们以m_
开头。 StackOverflow has questions about this particular idea and you can read about alternatives: StackOverflow对此特定想法有疑问,您可以阅读以下替代方法:
Why use prefixes on member variables in C++ classes 为什么在C ++类的成员变量上使用前缀
I'm more aligned with Qt's API Style Guidelines, and most of their conventions work for me: 我更了解Qt的API样式指南,并且大多数约定对我有用:
http://doc.qt.nokia.com/qq/qq13-apis.html http://doc.qt.nokia.com/qq/qq13-apis.html
Speaking of which, Qt is a nice C++ offering that has continued to evolve and mature. 说到这,Qt是一个不错的C ++产品,并且一直在发展和成熟。 Blows MFC out of the water (completely) AND is cross-platform... 将MFC吹出水面(完全),并且是跨平台的...
http://qt.nokia.com/products/developer-tools/ http://qt.nokia.com/products/developer-tools/
If you want to be locked in the Microsoft universe go for .NET and C#. 如果要锁定在Microsoft Universe中,请使用.NET和C#。 That would be a step up, and you wouldn't be stuck with the MFC abandonware. 这将是一步,而且您不会被MFC放弃软件所困扰。
Oh boy. 好家伙。 That's almost like asking people what religion you should follow. 这几乎就像问别人应该遵循哪种宗教。 At least you didn't ask what text editor you should use... 至少您没有问过应该使用哪种文本编辑器...
My 2c: do what works for you. 我的2c:做对自己有用的事情。 Also consider whether you are just coding for yourself, or whether you expect other people to read or work on the code you write, and whether you are starting a project from scratch, or building on existing code. 还应考虑是否只是为自己编写代码,还是希望其他人阅读或使用编写的代码,是否从头开始一个项目,还是建立在现有代码上。
Naming convention really doesn't matter much, so long as: 只要以下条件,命名约定实际上并没有多大关系:
You pick names that are meaningful and describe the purpose of the variable/function/other. 您选择有意义的名称并描述变量/函数/其他的目的。 (Key issue here is purpose; let the compiler deal with the type.) (这里的关键问题是目的;让编译器处理类型。)
Be consistent in how you apply any other aspects of the convention (indentation, casing, prefixes, use of underscores, etc). 在应用约定的任何其他方面(缩进,大小写,前缀,下划线的使用等)时,请保持一致。
Generally speaking, if code is well-written, the details of the convention don't matter much. 一般来说,如果代码编写得当,则约定的细节并不重要。
As for Hungarian and prefixes: Win32 and C++ still uses them somewhat, while .Net and C# don't. 至于匈牙利语和前缀:Win32和C ++仍会使用它们,而.Net和C#则不使用。
I'd highly recommend reading this long but very insightful article by Joel Spolsky that goes to great length to outline how and why a prefix convention can be actually very useful, if done correctly, and also where it's just a pointless chore. 我强烈建议您阅读Joel Spolsky撰写的这篇漫长而非常有见地的文章,其中详尽地概述了前缀约定如何以及为什么实际上可以非常有用(如果正确完成的话)以及在哪里毫无意义的琐事。
These days, I don't bother with most Hungarian prefixes, except for a few (note that these are just my personal preferences, they're ones that I've found to be useful over the years): 如今,除少数匈牙利前缀外,我不再打扰大多数匈牙利前缀(请注意,这些只是我的个人喜好,这些年来我发现它们是有用的):
For me, the first two of these help make the code more readable; 对我来说,前两个帮助使代码更易读。 the third example here helps both with readability, but can also be a useful technique for ensuring correctness - a bit like a mnemonic, if you will. 这里的第三个示例不仅有助于提高可读性,而且还是确保正确性的有用技术-如果可以的话,它有点像助记符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.