Naming conventions: Guidelines for verbs/nouns and english grammar usage

亡梦爱人 提交于 2019-12-04 08:05:31

问题


Can anyone point me to a site, or give me some wisdom on how you go about choosing names for interfaces, classes and perhaps even methods and properties relating to what that object or method does?

This is specifically for Microsoft development, so Java-esque "doGet" and so on isn't really used, however some general rules that cross language barriers must (I would've thought) exist.

An example might help: I have 4 choices of names for an interface:

IGroupedItem
IGroupableItem
IDataEntity
IGroupedEntity

They all contain an adjective and the noun, or just a noun. Looking at the .NET framework it seems like there must be some kind of ruleset for this, for consistency? Aside from the obvious verbs for methods.

Edit: Though the example is an interface, I'm not limiting this to just interfaces. The general guideline is classes are nouns, methods verbs, properties nouns. I suppose what I mean is choice of the synonym. Is throwing "Entity" everywhere wrong


回答1:


Look at the MSDN articles for naming guidelines. In short:

  • Use nouns for class names and property names (it's obvious)
  • For interface names, start with I and use nouns and/or adjectives to describe behavior
  • Use verbs for method names to describe action

For your example - IGroupableItem.




回答2:


Interfaces are things a class is capable of doing. Not what it is, but what it can do.

IGroupableItem

Other names describe what things are or are too vague to be useful.

Specifically, "IDataEntity" is largely meaningless. After all, everything's a data entity.




回答3:


MSDN has an article just on Interface Naming Guidelines that may help you out. If you want the naming conventions of stuff other than interfaces, along with many other naming and design guidelines, you can find that all on MSDN, too.




回答4:


This is the same material as Spodi's answer, but MSDN's Design Guidelines for Class Library Developers are mostly excellent, covering naming and much, much more.




回答5:


There is nice article Making Wrong Code Look Wrong by Joel Spolsky. It tells about not so popular, but very handy naming convention.




回答6:


As well as the MSDN Guidelines, there is a C# Coding Standards document from IDesign by Juval Lowy that is quite helpful (don't know if/how much this differs from MSDN).

C# Coding Standards




回答7:


Try this....

Main Site: http://www.ssw.com.au/ssw/standards/Default.aspx
Code Rules: http://www.ssw.com.au/ssw/Standards/Rules/RulestoBetterCode.aspx



来源:https://stackoverflow.com/questions/412839/naming-conventions-guidelines-for-verbs-nouns-and-english-grammar-usage

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