我该如何命名? [英] How should I name this method?

查看:86
本文介绍了我该如何命名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为处理Job实体的服务设计API.我需要检索给定状态的作业.因此,我最终将自己的方法命名为:

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);

我无法使用特定状态(例如关闭和命名我的方法getAllButClosedJobs),因为我的方法将是能够处理任何状态的通用状态.

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:我希望这个问题属于SO,尽管从技术上讲不是 programming .否则,请随时将其迁移到合适的站点.

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.

推荐答案

List<Job> getJobsExcludingStatus(JobStatus status);

甚至

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);

然后想象这样的代码:

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

任何人都打算知道它是如何工作的?他们必须深入研究方法,才能发现false是包含或排除或其他任何东西的开关.方法实现内的if语句在您的API中隐藏两个方法-一个执行正确的情况,另一个执行错误的情况.键入不是软件开发的瓶颈,而是思想.

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.

这篇关于我该如何命名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆