Which one is better, writing another method or adding one more parameter to an existing method?

shelholmes221 :

I have a method: 

public Question createQuestion(String text, Project project, User createdUser, Date createdDate)

this method is being used to create a question by the controller. Now there is no tag here in the parameters. I want to implement a functionality to add tags to a question.

To add tag I need to pass it a tagSet which can be empty as well when the user does not add tag to a question while creating the question. So, should I pass one more parameter to it and then put an if condition before adding that to the question object or should I write a separate method?

public Question createQuestionWithTags(String text, Project project, User createdUser, Date createdDate,Set<Tag> questionTagSet)

 which will call the createQuestion and then will set the questionTagSet in the object returned by the above createQuestion() method. If I write another method then check for the empty tag will be done in the controller and if not then that check condition will be in the utility. Which approach is better?

Also, how about overloading the method in the same context?

Eugene :

Well to be honest, taking into consideration of how complicated overloading is in general (JLS wise), I have a hard time saying yes to it. And by hard I mean look at functional interfaces and overloading (or even search this site for related questions to see how people are pulling their hair sometimes - me included). But this is not related to lambdas only, overloading is by some people seen as too complicated and rarely needed, read this for one example.

What you have done with renaming those methods is the cleanest possible ways of achieving the task you have (unless you think of a builder pattern may be).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=108479&siteId=1