8

Imagine this set up:

public interface IMass{
    double Mass {get;}
}

public static class IMassExtension {
    public static double ToKg(this IMass massObject) {
        return massObject.Mass / 1000.0;
    }

    public static double CalculateInteractiveGravity(this IMass massObject, IMass otherMassObject)          
    {
         return blah;
    }
}

Is it ok to put the extension class in the same file as the interface (i.e. IMass.cs) or should it be in a separate file (IMassExtension.cs)?


A base class is not possible here. Imagine

public class Person : Animal, IMass {}

and

public class House : Building, IMass {}
1
  • @Yusubov Base class won't work here, updated question
    – Moop
    Aug 5, 2014 at 17:19

4 Answers 4

13

Yeah, that's totally fine. Further, I would encourage you to do that, as it makes the functionality available in the interface more discoverable, while reducing the noise in the "what functionality is in this csproj?" view in visual studio.

The only arguments I've heard against this are:

  • "but then you have 2 types per file!" - guidelines are there to help you, not tie your hands. You'd make it only one type if you could.
  • "but that extension uses XYZ!" - yeah, if you wouldn't include the method if it were an abstract class, then you shouldn't hard-wire it to the interface either. That's not a problem with this practice, but with your design.
1
  • New language features pretty much enhance this further Apr 14, 2020 at 6:51
2

In summary: Having the extension methods in a separate file would be the approach to go. It is also very important to name extensions appropriately, as that will definitely safe time during the maintenance.

I would definitely prefer the name MassExtensions over IMassExtensions. The reason being that to most programmers an I prefix on a type implies that it is an interface. This is both a very common pattern and part of the .NET Design Guidelines.

Adding an I prefix for the extension method case breaks both assumptions and established guidelines.

There is also precedence for this in the Base Class Library. The majority of the extension methods available for IEnumerable are contained in the type Enumerable.

Following related posts might be helpful:

2
1

Normally, I wouldn't put extension methods in the same file as the interface. Same library/package? Sure.

In this example, I would definitely wouldn't. What if someone implemented mass in imperial units (slugs)? The toKg() should be in the interface (and maybe well as toSlug(), but that would technically be derivable from toKg()), and the extension methods would be methods like toG(), toMcg(), etc., which could be implemented using the interface methods.

0

I would put it in a separate file, and I would give it a more descriptive name than "IMassExtension.cs". Perhaps something like "MassConversions.cs", since that's what the class actually does.

Don't forget, the extension methods can still be called as if they were simple static methods (e.g. MassConversions.ToKg(massObject) ), so the class should be named with that in mind. Also, your extension class isn't an interface, so it could be misleading to name it with the leading 'I'.

6
  • Well, this is just a simple example, I have a bunch of extension methods that do different things, and there is no good common name for it
    – Moop
    Aug 5, 2014 at 17:16
  • That might be a sign that some further thought should be put into organizing the functionality. "Kitchen sink" classes are commonly sources for lots of agony, and should generally be avoided. Just because the functionality is surfaced in the form of extension methods is no reason to abandon good class structure.
    – Eric King
    Aug 5, 2014 at 17:22
  • I updated the question to show a possible reason why it is useful for this design.
    – Moop
    Aug 5, 2014 at 17:23
  • I see your update, and my comment still applies. Make separate (appropriately named) classes for separate types of functionality. In my opinion, plopping all of them in the same class is convenient, but sloppy.
    – Eric King
    Aug 5, 2014 at 17:24
  • Are you saying that Animial : Mass and Building : Mass if Mass was a base class?
    – Moop
    Aug 5, 2014 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.