Liath
Liath

Reputation: 10191

C# class names starting with an I

I am writing a class where the name starts with an I (because that's the name of the product we're integrating with - can't change).

Convention states that class names have a capital letter to start, however in this case it would appear to the consumer as an interface.

Is there any documentation guiding developers on the correct approach here? In addition what happens if I do need to implement and interface - should it be IiProduct/IIProduct?

Edited to Add:

I can't mention the product for obvious reasons but it follows the same capitalisation convention as apple. iPhone, therefore IPhoneClass (follows their branding but appears as an interface) rather than IphoneClass (which follows convention rather than branding).

Upvotes: 14

Views: 9057

Answers (6)

George Duckett
George Duckett

Reputation: 32428

If the product name is for example iPhone, then in that case I would call the classIphone to keep class naming consistent. The interface would be IIphone, which doesn't look great, but is clear in its meaning (as it follows convention).

I'm sure people using the class would be more upset if they thought it was an interface, than if it wasn't capitalized as the product is to follow convention.

Upvotes: 14

zzfima
zzfima

Reputation: 1565

You can wrap your "bad" called names class with "I" capital letter to other Name For example:

    //"Bad" name class
public class IBadClass
{
    public void SomeFuncA(int i)
    {
    }

    public void SomeFunctB(string str)
    {
    }
}

//"Better" name class
public class BetterNameClass
{
    private IBadClass _badClass;

    public BetterNameClass()
    {
        _badClass = new IBadClass();
    }

    public void SomeFuncA(int i)
    {
        _badClass.SomeFuncA(i);
    }

    public void SomeFunctB(string str)
    {
        _badClass.SomeFunctB(str);
    }
}

Upvotes: 0

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

I follow this convention for interface.

  1. Find the most significant method in the interface.
  2. Add able suffix with it.
  3. Capital the first letter.
  4. Add I prefix

See the example

interface ICallable{
    public void call();
}

ICallable = I + CapitlizeFirstLetter(call+able)

I think most developers follow these convention. From that point of view IClass, IDone, ICall , IncomeTax etc are not an interface. But IClassable, ICallable, ITaxable etc respectively are interface.

Upvotes: 4

Digbyswift
Digbyswift

Reputation: 10400

If the class name begins with an I then this is fine, e.g. Index or Idea are both valid names. IIndex would be a valid interface name.

For help with conventions, you could consider picking up a copy of Code Complete which is an amazing source of guidance in many programmatical aspects.

Upvotes: 2

Tom W
Tom W

Reputation: 5413

Can you mangle the name of the class to ensure that it doesn't start with an I?

Typically though, you'd expect that an I that doesn't belong to a word denotes an interface. A class name that starts with I (For example, Interpolator) is fine and not particularly ambiguous.

Upvotes: 0

Alex Dn
Alex Dn

Reputation: 5553

Interface starts with I as a prefix and then Interface name using capital letter. So interface will be IImposibleProduct and class will be ImposibleProduct.

Upvotes: 10

Related Questions