views:

649

answers:

9

When you write an app that will have vast number of classes, do you use prefixes/suffixes for your class names? Or should I just rely on the namespaces I already define?

In my case, I have classes like:

Blur
Contrast
Sharpen
Invert
...

I just don't know what I might have tomorrow, and want to be collision free as possible with the frequent used classes in the BCL.

Any ideas?

+11  A: 

I would say don't use prefixes or suffixes for the names -- that's very close to hungarian notation, which is widely regarded as bad, even by the creators of it at MS. The sole exception to this rule is interfaces, which many regard as OK to prefix with I, but I am of the opinion that interfaces shouldn't have a prefix either. You should group classes that are related into namespaces to prevent collisions.

rmeador
+1 I do like prefixing interfaces with I, and I frequently use hungarian notation for local variables, but anything at the instance level or higher gets a "proper" name.
Rex M
An ihmo excellent article on how the inventors meant Hungarian notation to be used by Joel Spolsky: http://www.joelonsoftware.com/articles/Wrong.html
0xA3
the I prefix is another painful naming convetion thing that I grudgingly adopt. Unfortunately, there needs to be a clear way to communicate that something's an interface. Working in Java for many years has convinced me that the "I" prefix is painful but still better than nothing at all.
Michael Meadows
I have configured my IDE to show interfaces and abstracts classes in a different color (green and blue). No need for Hungarians. I also color code mutable variables (purple), so I don't need the 'final' keyword on local variables: http://img376.imageshack.us/img376/1856/mutableimmutablene2.png
Esko Luontola
+17  A: 

Namespaces were invented exactly to solve this problem. Use them.

Itay Moav
A: 

I am curious. Shouldn't those be method names instead of class?

shahkalpesh
Not if it's a strategy pattern and they're swaped in/out at runtime.
SnOrfus
Hi, in my case, these actions are implemented as nodes, so they are live, if that makes sense.
Joan Venge
+4  A: 

Generally, I would name them something descriptive (like you have) but I split them up into appropriately named namespaces.

In your case, I'd heave something like

namespace AppName.ImageOperations
{
    public class Blur{...}
}
SnOrfus
+1  A: 

If you have a common base class or an interface (e.g. class Effect or interface IEffect), then I would probably use Effect as a suffix for these classes.

Otherwise I tend to use namespaces.

M4N
+1  A: 

At work we use prefixes for all classes and some classes use suffixes. This does stop BCL collisions but in my opinion it is unecessary. You are better off using sensible naming and good namespaces.

I would suggest using an I prefix for interfaces, but it is just a personal preference.

Stevo3000
+5  A: 

Avoid prefixing or suffixing classes unless:

  • It is an interface: IFilter
  • You need to communicate that it is participating in a pattern: GaussianBlurStrategy
  • (from marc_s) You create a custom exception: ImageDoesNotSupportFilterTypeException
  • Sometimes to communicate if something is an abstract base class: FilterBase

I'm torn on that last one. I don't like seeing the Base suffix, but it's really hard for a consumer of your API to know that they can't instantiate Filter (without trying it first) based on the name. I have no love for it, but it's a convention that I use.

EDIT

Per Jon B's comment, suffixing custom delegates with Handler is common (FilterExecutedHandler(object sender, FilterEventArgs e);). Although a delegate declaration isn't a class per se, it can cause name collission so should probably be stated here.

Michael Meadows
Thanks Michael, for #3, is it only valid for abstract classes? What about other base classes?
Joan Venge
I edited to pontificate a little more. Naming conventions are really subjective, and you should do what makes you comfortable. Either way works, but I only use it for abstract classes for reasons I state in the last paragraph.
Michael Meadows
@Michael: Is it still common practice to suffix a delegate with "Handler"? If so, you might add that to the list.
Jon B
Hmm, never thought about that... I haven't created a custom delegate since 2.0 (and EventHandler<T>) was released.
Michael Meadows
Also Michale, why suffix, but not prefix? I know it's personal preference but to me BaseFilter is more readable and you get intellisense support, right?
Joan Venge
Again, I think it's just personal preference... It was the way I learned it, so I continue to do it. If you like the prefix, use it instead. I think the suffix is more common, but I don't think there's any good reason to do it one way or the other.
Michael Meadows
@Joan: BaseFilter would not be immediately recognizable to a developer accustomed to the more common FilterBase approach.
Jon B
There's also a practice to add "Exception" to your custom exception types
marc_s
Thanks, Jon. Since I am not familiar with the Base suffix, can you please tell me where it comes from, by popularity?
Joan Venge
@marc_s good catch, I added that
Michael Meadows
+1  A: 

Actually I could care less about prefix/suffix and more about Namespace partitions and Class names. I want Class names to be meaningful, but I also want correct Namespaces as to avoid collisions and to unify my companies code base.

Ex.

namespace Company.Department.Product
{
     public class Class
     {}
}

This makes it very easy to partition code out, and help programmers decide where to put functionality. It takes more time to setup, but I feel in the end it's worth it.

Chris
A: 

The Guidelines for Names (from Microsoft) is an interesting read...

For Namespaces they say:

The general format for a namespace name is as follows:

<Company>.(<Product>|<Technology>)[.<Feature>][.<Subnamespace>]

For example, Microsoft.WindowsMobile.DirectX

For Classes, one thing they say:

Do not give class names a prefix (such as the letter C).

Interfaces, which should begin with the letter I, are the exception to this rule.

Worth at least a scan through to pick up some ideas...

Edward