views:

180

answers:

6

Developer teams usually have some class naming conventions based on class functionality and roles it plays in patterns. For example, we use the following postfixes:

  • Info for data structure classes (only public properties, no methods, such as business entities).
  • Helper for classes with common functionality used all over the project (StringHelper, FormatHelper, ImageHelper)
  • Controller for MVC-controllers
  • Repository for DAL classes that contains operations grouped by entity they dedicated to (PersonRepository, OrderRepository)
  • Manager for business logic classes

Etc.

What are the naming conventions for postfixes/prefixes your team uses?

+1  A: 

There are three prefixes we use: I, T and _. The first is used for interfaces, the second for generic types and the third for property backers. I strongly advice against any other prefixes. This is in line with Microsoft's recommendations by the way. EDIT: What I mean here is that Microsoft recommends not to use prefixes other than I and T. See Guidelines for Names - Names of Classes, Structs, and Interfaces. I'm already violating by using _ but I feel the need to distinguish between private fields and property backers and I like the fact that _ is not alpha numeric. /EDIT

The list of postfixes is virtually endless. They are often based on the name of the base class/interface, such as IDispatcher -> EmailDispatcher.

Personally, I don't like postfixes like Info very much because they are too generic as most classes represent some sort of information anyway. Finally, I like to use Service as a postfix instead of Manager.

EDIT I use the Provider postfix quite often as well, like in the well known ApplicationRoleProvider BCL class.

Sandor Drieënhuizen
I wasn't aware of Microsoft recommending _ as a class prefix. Can you provide a citation?
John Saunders
@John Saunders: I think @Sandor talks not about class name prefix, but field name prefix
abatishchev
@abat: ok, thanks.
John Saunders
Why should be forced to follow ms standards? Just have team standards (unless your team is in Microsoft!)
Pierreten
@Sandor Drieënhuizen how do you call proxy classes to communicate with external services?
Andrew Florko
@John Saunders: I updated my answer to point out the Microsoft Guidelines. I'm aware prefixing with `_` is in violation of them. @Andrew Florko: I postfix service proxy classes with `ServiceClient`.
Sandor Drieënhuizen
@Pierreten: nobody is forcing anybody but I simply like these particular recommendations. Not just because Microsoft is recommending them but because they make a lot of sense.
Sandor Drieënhuizen
@Pierreten: One advantage of following the MS recommendations is that you get tool support for free.
Brian Rasmussen
@Brian Rasmussen: good argument. For those who don't immediately know what tools you're talking about: FxCop and StyleCop, both available for free from MS.
Sandor Drieënhuizen
@Sandor: Surely your team also uses the `T` prefix for generic type parameters?
LukeH
@LukeH: yes, ofcourse! I forgot all about it.
Sandor Drieënhuizen
FXCop recognizes the prefixes s_ and m_ for static/shared and member fields.
Jonathan Allen
Another advantage to following a more universal set of naming conventions is that there's a greater chance new developers will be able to (probably) better adhere to your naming convention as well as understanding/learning your code base faster.
RobS
A: 

You forgot

  • Model for MVC Models
  • View for MVC Views

Also we use Manager, probably too often, for classes which do some control under another classes, e.g. UserManager.

abatishchev
A: 

We use an 'I' prefix for interfaces and that's about it. Generally speaking, we try to comply with the language (we use multiple) for naming conventions to avoid having to mix conventions in our code for places where we're required to comply with the existing conventions. We don't go against the language standards, so to speak, as that would require us to mix conventions.

After working with various projects and coding standards over the past decade, I've come to believe that the more exotic standards become and the more they impede into subjective domains, the more likely they are to turn people off completely which can seriously hinder the team's productivity.

There are good practices that help to make code easier to debug, for instance (don't cram multiple statements on one line, e.g.), and help to produce more efficient, robust code (define variables when they can be sensibly initialized with limited scope), but the ones which don't have very unbiased, objective arguments in favor of them usually demand developers to subscribe to someone's personal preference.

With naming conventions in particular, some practices are just downright harmful and counter-productive. Hungarian notation, e.g., places a predominant focus on types when modern developers should generally be concentrating more on the interface(s) that a type provides and polymorphism than on the specific types themselves. I'm not particularly fond of the 'Manager' convention either, as most high-level classes that make use of composition would often be considered to fit into that category and then the suffix becomes quite superfluous and used far too often.

A: 

FXCop recognizes the prefixes s_ and m_ and will complain if you mix them up. I like them better than the generic _ prefix that C# developers seem to prefer.

I is always used for interfaces and T for generic parameters. No one disputes this.

Everything else is handled by suffixes like Collection. I won't list them because they are either obvious or specific to my company's libraries.

I'm of mixed thoughts about controls. Sometimes I use proper Pascal cased names, other times I use the old VB-style prefixes like txt, cmd, lbl, etc. There are mertis and flaws to both.

Jonathan Allen
I am a fan of the txt, num, grd, lst etc prefixes for controls, as it makes it easier for me in code to see what fields are datafields, like "cutomerName", and which are controls, like "txtCustomerName". This is a personal preference, of course, but one which I found to be shared by many of my team members over the years at various companies, in various languages. Specifically VB6, C#, and Delphi
Joon
@Joon: in my opinion, (naming) conventions are all about simplicity and consistency. Using such abbreviated prefixes (called Hungarian notation) is not something you can capture in a simple rule. If you try, you'll quickly end up with ugly names such as unpvMyValidator and what not. Not to speak of naming clashes because differen types can end up as the same prefix. Yes, you could add a table of common prefix to your convention but that doesn't provide simplicity at all and requires team members to learn tables from the top of their heads.
Sandor Drieënhuizen
@Sandor: I am familiar with Hungarian notation, and I am definitely not a fan for most types of code. However I have found that prefixing control names makes it easy to distinguish UI elements from class fields.What alternative would you propose that allows you to easily identify which code elements relate to UI widgets?
Joon
@Joon: I always postfix the element names with their respective type name. This results in names such as SurnameTextBox or CustomersTreeView. It is a very easy and unambiguous convention and even though it can result in rather long names, I find it very practical because it involves no decision making.
Sandor Drieënhuizen
A: 

For class names I prefer suffixes to prefixes; e.g. for different kinds of Block class, I have BlockBase, BlockText, BlockCollection, BlockSequence, BlockTable, BlockDocument, etc. I prefer suffixes because it helps to group them together alphabetically.

To give another example: if I had several class which implement IDispatcher, I might prefer DispatchEmail instead of EMailDispatcher.

On the other hand, I'm alternatively using internal namespaces (i.e. folders within a project/assembly) to do this.

ChrisW
In my opinion class names should be verbs if they do something and nouns if they represent something. This is pretty common practice. For example: an EmailDispatcher dispatches email so it should not be called DispatchEmail. On the other hand, an Apple is not an Appler ofcourse. If you're talking about methods, its different because then DispatchEmail would be a proper method name as methods are often named in the present simple tense and should explain what they are going to do when called upon.
Sandor Drieënhuizen
A: 

Our team prefixes all classes with 'C', all interfaces with I, and all structs with T. Which is horrible - at least that classes begin with 'C'. Because the source files have the same name as the class. Then when the source files are listed in alphabetical order it is hard to find what you want! Because the project is a number of years old, renaming all the classes and source files is not an option, so the convention will stay.

GarethOwen
There are refactoring tools that can do that for you.
Turing Complete
Across different languages and IDE's? c++, c#, java
GarethOwen