18

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

Chris S
  • 64,770
  • 52
  • 221
  • 239

8 Answers8

22

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.

Rumen Georgiev
  • 702
  • 4
  • 23
  • 1
    So why not IGroupedItem? That matches the criteria of noun and adjective, or noun phrase – Chris S Jan 05 '09 at 15:30
  • 8
    Probably because you can say that something is 'Groupable' because it's implementing the interface, but you can't guarantee that it is 'Grouped'. – Ant Jan 05 '09 at 15:44
  • This + Spodi's answer are the closest I'll find to an answer without going into a huge language debate, but I can't give two answers – Chris S Jan 06 '09 at 10:10
  • 1
    To distinguish interfaces from classes with "I" (or "Impl" on the class side) is a bad practise. "Interface" is a technical konzept which should not be part of the name. That would be not much better than `ageInt`. – deamon Jan 09 '10 at 13:59
  • @deamon [Microsoft recommends prefixing interface names with "I"](https://msdn.microsoft.com/en-us/library/8bc1fexb(v=vs.71).aspx). -- It's the only form of Hungarian notation currently recommended that I am aware of, and in this one case it seems to be a helpful convention, especially when you're using intellisense, etc. – BrainSlugs83 Jul 12 '16 at 01:16
  • Henry David Thoreau said "Simplify, simplify, simplify." I just say "simplify." Or sometimes, IGroupable. – Steve Lautenschlager Apr 05 '23 at 19:16
8

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.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
4

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.

Spodi
  • 1,151
  • 1
  • 11
  • 19
1

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

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

Malx
  • 990
  • 1
  • 9
  • 16
  • you're talking about hungarian? not sure that applies here really – annakata Jan 05 '09 at 11:57
  • just read the article. "hungarian" have several meanings. The word "type" means not the "integer" but "coordinate" or "weight". So coordX=weightY is wrong in that hungarian. And the method weightConvert(coordX) is also wrong. And you see this error just from looking at code. – Malx Jan 09 '09 at 16:29
1

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

Nick
  • 5,616
  • 10
  • 52
  • 72
0

For verbs in function (and etc.) names, it's common to use the original form of the verb, like Handle, ExecuteQuery, or IsAny, ...

Some less common usage of verbs makes it singular with s/es, like ProducesResponseType (MS Docs).

themefield
  • 3,847
  • 30
  • 32
-1

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

Chris
  • 6,702
  • 8
  • 44
  • 60