Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should an interface method return a custom object? [closed]

Tags:

c#

standards

A question was raised in a discussion I had around whether an interface method should return a Custom object vs a primitive type.

e.g.

public interface IFoo
{
      bool SomeMethod();
}

vs

public interface IFoo
{
    MyFooObj SomeMethod();
}

Where MyFooObj is:

public class MyFooObj
{
     bool SomeProp{get;set;}
}

The argument being that you can easily add properties to the object in the future without needing to change the interface contract.

I am unsure what the standard guidelines on this are?

like image 608
Tom Jones Avatar asked Mar 22 '12 15:03

Tom Jones


4 Answers

IMHO Changing the MyFooObj is the same as changing/adding methods to the IFoo Interface - so no I don't think it's a good idea add just another abstraction - remember YAGNI

like image 77
Random Dev Avatar answered Nov 08 '22 20:11

Random Dev


My standard response is - YAGNI.

You can always change things if it turns out that way, in particular if you control the full source of the application and how the interface is used.

Wrapping a boolean just in order to forecast the future is only adding complication and additional layers of abstraction when they are not currently needed.

If you are using DDD and specific modelling techniques in your codebase, is can make sense to have such aliases to booleans, if they are meaningful in your domain (but I can't see this being the case for a single boolean value).

like image 7
Oded Avatar answered Nov 08 '22 19:11

Oded


I don't see the point of encapsulating primitive types in a custom object.

If you change the definition of this custom object, then you actually change the contract because the function doesn't return the same thing.

I think it's again an over-engineered "pattern".

like image 3
ken2k Avatar answered Nov 08 '22 20:11

ken2k


There are no general guidelines regarding this.

As you pointed out, if you have semantics around the return type that you think strongly believe may change or may need to be updated in the future it may be better to return the complex type.

But the reality is that in most circumstances it is better to keep things simple and return the primitive type.

like image 3
Mike Dinescu Avatar answered Nov 08 '22 19:11

Mike Dinescu