How should I name this method? [closed]

谁说胖子不能爱 提交于 2019-12-13 13:01:23

问题


I am designing the API for a service that deals with Job entities. I need to retrieve jobs given a status. So, I ended up naming my methods like so:

List<Job> getJobsByStatus(JobStatus status);

A while later I realised that I also need to be able to retrieve jobs which don't belong to a given status. Say, I want to retrieve all but the closed jobs.

I have been unable to think of a suitable and intuitive name for this method.

I thought of the below but don't quite find them right.

List<Job> getJobsAllButStatus(JobStatus status);
List<Job> getJobsNotStatus(JobStatus status);

I can't use a specific status such as closed and christen my method getAllButClosedJobs because my method will be a generic one capable of handling any status.

PS: I hope this question belongs to SO though it is not technically programming. Otherwise, please feel free to migrate it to a suitable site.


回答1:


List<Job> getJobsExcludingStatus(JobStatus status);

or even

List<Job> getJobsExcluding(JobStatus status);

.......

And for good measure here's why you shouldn't use a boolean parameter. Say you had an interface like this:

List<Job> getJobs(JobStatus status, boolean exclude);

then imagine code that reads like this:

List<Job> jobLIst = getJobs(status, false);

How is anyone meant to know how that works? They'd have to dig inside the method to find out that false was a switch for including or excluding or whatever. The if statement that would be inside the method implmentation is hiding two methods in your API - one that does the true case and the other that does the false case. Typing isn't the bottleneck in software development - it's the thinking.




回答2:


List<Job> Jobs.allWith(JobStatus... status);
List<Job> Jobs.allBut(JobStatus... status);

Combination of fluent api and varargs




回答3:


List<Job> getJobsWithoutStatus(JobStatus status)



回答4:


Try to keep it simple -

List<Job> getJobsBelongTo(JobStatus status)

for retreiving all jobs which belong to status.

List<Job> getJobsNotBelongTo(JobStatus status)

for retreiving all jobs which do not belong to status.



来源:https://stackoverflow.com/questions/9095483/how-should-i-name-this-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!