Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coding style with generics and inheritance [duplicate]

Possible Duplicate:
How to name C# source files for generic classes

We are currently re-evaluating how we do generic classes when we inherit from a general class. Currently we will put the following two class definitions in the same file

class Foo
{
    // code for class
}

class Foo<T> : foo
{
   // code for class
}

My question is a simple one, should we keep them in the same file, or split them into separate files?

So far the pros to keeping them in the same file is that you have all the code there right infront of you. The con is that when both classes get sufficiently large, it could become un-readable.

What I would like is good reasons as to why we should do one or the other. If you recommend separate file, I would also like you to include possible naming conventions, or a strategy to get around the fact that we can have only one file named Foo

like image 615
Zoidberg Avatar asked Feb 01 '26 22:02

Zoidberg


2 Answers

This is a matter of opinion, but I'd keep them in the same file rather than try to maintain some naming convention for one or the other.

While I subscribe to one class, one file, I think there is value in having these together. We really treat these as one class, right? Typically, Foo will be abstract, and is just a way of using our generic types… well, more generically -- in places where the type parameters don't matter and can't be known at compile time.

If the classes become too large, it should be a red flag anyway that some responsibilities should be broken out.

like image 50
Jay Avatar answered Feb 04 '26 12:02

Jay


Unless classes are utterly trivial, I never put more than one in a single file. It's much easier, IMO, to find exactly the class you seek when you have a predictable, unique file name, with namespaces based on folders, generally.

For naming your files, maybe this:

foo.cs
foo_t.cs
foo_tuv.cs // for a foo class with three generics
like image 39
Andrew Barber Avatar answered Feb 04 '26 10:02

Andrew Barber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!