I am writing a Java API library for an existing REST API.
The REST API has a few postOperations (getPosts, getUserPosts, getGlobalPosts etc.)
So I have a class for postOperations that exposes that.
Now I need to add to all the operations the option to add a group of parameters (generalParameters).
What do you think is the better solution?
- Add a parameter to the current functions, which means that sometimes the user will have to pass null (it’s actually 80% of the time).
- Add an overloading function to every operation that gets 2 parameters (this means adding 6-7 overloading methods to the class)
I know both ways work.
And it’s not a major issue in most applications, but when writing a library other would use it seems a bit more important to me.
What’s your opinion? what is a better api to expose?
I would do something like this:
Per your edit:
This code here uses option 2; they will never have to pass a null value if you add another overloaded method which doesn’t take in anything, but passes null for them