Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I name this method? [closed]

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.

like image 475
adarshr Avatar asked Feb 01 '12 12:02

adarshr


People also ask

How should method be named?

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters.

How do you name a controller method?

Controller class names use CamelCase and have Controller as a suffix. The Controller suffix is always singular. The name of the resource is usually plural. Controller actions use snake_case and usually match the standard route names Rails defines ( index , show , new , create , edit , update , delete ).

How do you name a method that returns a boolean?

The usual convention to name methods that return boolean is to prefix verbs such as 'is' or 'has' to the predicate as a question, or use the predicate as an assertion. For example, to check if a user is active, you would say user. isActive() or to check if the user exists, you would say user.


2 Answers

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.

like image 107
blank Avatar answered Nov 10 '22 01:11

blank


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

Combination of fluent api and varargs

like image 29
Aravind Yarram Avatar answered Nov 09 '22 23:11

Aravind Yarram