remi bourgarel
remi bourgarel

Reputation: 9389

How to respect the open/close principle with primitive parameters?

is it a good pattern to add a parameter object instead of primitive for respecting the Open Close Principle.

Lets say I have this interface

public interface IBar{
  void DoSomething(int id);
}

If one day I need to add a parameter, I'll have to change my interface. With this solution

public interface IBar{
  void DoSomething(DoSomethingParameter parameters);
}

I can add as many parameters as I need without touching the interface, and I'll be able to create new implementaiton of IBar using the new parameters.

Is it an anti pattern ?

Upvotes: 1

Views: 215

Answers (1)

Ibrahim Najjar
Ibrahim Najjar

Reputation: 19423

It is a good idea to use objects as parameters when you want to combine several related parameters into a meaningful representation.

In my opinion doing so for primitive types such as integers, strings ... etc i redundant and won't give you any benefits, if it didn't actually cause overhead for wrapping/unwrapping on each call.

Also you can see many interfaces representing the a Repository where there are always methods like GetAuthor(int id) that are used to get elements from the database by their respective ids.

Upvotes: 1

Related Questions