-2
\$\begingroup\$

Lets say I have the following interfaces

interface IRealm
{
    Tile GetTile(int x, int y);
    bool SetTile(int x, int y, Tile tile);
}

interface IRealmSize
{
    int TilesWide { get; }
    int TilesHigh { get; }
}

The intefaces are separate as I have both infinite realms with no size and bounded realms with a fixed size. One class uses these realms, but requires that the realm has a fixed size.

class RealmRenderer<T> where T : IRealm, IRealmSize
{
    public RealmRenderer(T realm) { ... }
}

Is there any drawbacks to doing it like this?

I could see an alternative like the following

interface IBoundedRealm : IRealm, IRealmSize { /* intentionally empty */ }

class RealmRenderer
{
    public RealmRenderer(IBoundedRealm realm) { ... }
}

But this creates a stricter requirement for the various realm types to implement this specific interface just to satisfy the renderer, where using generics leaves this specific requirement out of the realm implementation.

\$\endgroup\$

closed as off-topic by pacmaninbw, πάντα ῥεῖ, dfhwze, t3chb0t, Mathieu Guindon Aug 31 at 13:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – pacmaninbw, πάντα ῥεῖ, dfhwze, t3chb0t, Mathieu Guindon
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ While your question is quite interesting, unfortunately it is off-topic for code review. We can only review working code here and this is a design question that is hypothetical. There is a stack exchange site called Software Engineering that does look at software design but I don't know if it would be on topic there either (softwareengineering.stackexchange.com). We would love to review any working code that you care to show us. codereview.stackexchange.com/help/dont-ask codereview.stackexchange.com/help/how-to-ask \$\endgroup\$ – pacmaninbw Aug 31 at 0:15
  • \$\begingroup\$ Ah, I am very sorry. The working code would be quite a lot to put up, which is why I wanted to focus on the question the design itself. I'll look at the other site. Should I do anything to close this or just wait for enough close votes? \$\endgroup\$ – William Mariager Aug 31 at 0:29
  • 1
    \$\begingroup\$ The code is the design - CR posts accept double the maximum of any other SE site (60K vs 30K chars), so if your project is modularized (seems to be), then it should be possible to post a set of related classes & interfaces, and then reviewers can (and will) provide feedback on every aspect of it, including any smelly interface or generics usage. e.g. you could have IRealm, IRealmSize, one or two implementations, a base class, and the renderer, and then reviewers would have something to chew on ;-) \$\endgroup\$ – Mathieu Guindon Aug 31 at 13:17
  • \$\begingroup\$ FWIW the alternative empty interface at the bottom is a marker interface, and that is generally considered a code smell. \$\endgroup\$ – Mathieu Guindon Aug 31 at 13:18
1
\$\begingroup\$

Having multiple interface constraints on a single generic parameter isn't a code smell; it can make sense in a number of cases.

However, for the two interfaces you mention, does it make sense to have an IRealmSize that isn't part of an IRealm? It may make be more appropriate to use:

interface IBoundedRealm : IRealm
{
    int TilesWide { get; }
    int TilesHigh { get; }
}
\$\endgroup\$
  • \$\begingroup\$ That was a small mistake on my part, I do actually have some more interfaces. I was trying to make a small example, but now I see that I forgot the part that actually prompted me to do the setup with generics. Sadly, it appears that this question is off topic for the site, so I'll hold off updating until I see if it gets closed. \$\endgroup\$ – William Mariager Aug 31 at 0:29
  • \$\begingroup\$ I posted the question on the sister site, as it appears to be on topic there: softwareengineering.stackexchange.com/questions/396767/… \$\endgroup\$ – William Mariager Aug 31 at 0:42
  • 3
    \$\begingroup\$ @WilliamMariager unless you show people the real API all reviews on every site will end up in you saying I do actually have some more interfaces. - do everyone a favor and do not change your code, both here and there if you want to get realistic and useful feedback. \$\endgroup\$ – t3chb0t Aug 31 at 6:19
  • \$\begingroup\$ @t3chb0t understood. \$\endgroup\$ – William Mariager Aug 31 at 11:07

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