There's a convention for boolean property accessors to name them like isX()
.
Does it apply to all boolean returning methods that are not invoked for their useful side effects, like this one?
Also, should I replace "is" with "are" if the noun is plural?
"Follow you team's guidelines" is not applicable since we have none (but I wish to bring more sense into the codebase).
// inside JTable wrapper
public boolean multipleRowsSelected() {
int[] selectedRows = getSelectedRows();
return selectedRows != null && selectedRows.length > 1;
}
3 Answers 3
The use of a verb (is
, get
) for a method goes hand in hand with the use of noun/adjective for a variable/field.
boolean selected = line.isSelected();
boolean lineSelected = line.isSelected();
Mind that this convention is not very tight in this field as opposed to word capitalization. For booleans one the possible formulations - when a noun is used:
as predicate (noun-is-adjective) or
as question (is-noun-adjective).
boolean lineSelected = line.lineIsSelected(); boolean lineSelected = line.isLineSelected(); // UGLY IMHO boolean multipleRowsSelected = table.multipleRowsAreSelected(); boolean multipleRowsSelected = table.areMultipleRowsSelected(); // UGLY IMHO
Though the question form has a prefix systematic, always is
/are
in front.
But a predicate is more operational correct.
With respect to using are
. That really is acceptable, as many predicates have other verbs like have
or contains
.
Also one often can see the compound structure exposed; something like:
table.getSelectionMode().isMultiple()
Some other perspectives
Getters and setters are a bit tainted or at least called boiler plate code. Other styles exist. Argumently not much better is the latest style of the java record classes:
record Point(int x, int y) {};
Point pt = new Point(3, 4);
int x = pt.x();
And similar:
int x = obj.x(); // Getter.
obj.x(30); // Setter.
Here every categpry is generalized to a noun. A kind of Chinese.
IsX
has become conventional and thus has some usability benefits. We see IsX and know it's a boolean flag.
But I would advise against trying to make your methods follow english grammar too closely.
For the same reason you avoid plurals, for every case where a choice makes you code grammatical, there is a case where it makes it ungrammatical or ugly.
obj.IsX = true; //ungrammatical
if(obj.IsX) //grammatical
people.add(person) //grammatical
foreach(person in people) //hmmmm
people.contains(person) //???!
personList.contains(person) //grammatical in more cases.
For your specific case of table.multipleRowsSelected
It seems like it's already covered by table.getSelectedRows().length > 1
, but if you really need it then yes I would switch to an IsSomething
rather than table.AreMultipleRowsSelected
.
Has
is almost as popular as Is
so..
table.HasMultipleRowsSelected
Or Maybe you could do a combination of
table.IsAnyRowSelected && ! table.IsSingleRowSelected
Or if you need to differentiate these tables in a general sense, you can choose a noun.
table.IsMultiplex
If you use this across the board you can talk about "multiplex tables" and everyone knows what you mean.
-
Thank you. So what is you advice for my specific case?Sergey Zolotarev– Sergey Zolotarev2025年04月08日 10:25:43 +00:00Commented Apr 8 at 10:25
-
1"I would switch to an IsSomething rather than table.AreMultipleRowsSelected" Do you mean I should avoid plural nouns in such cases?Sergey Zolotarev– Sergey Zolotarev2025年04月08日 10:56:21 +00:00Commented Apr 8 at 10:56
-
1But you do agree that
isMultipleRowsSelected()
is nonsense, don't you? We have a lot of methods like that. I understand you want to replace the plural nounSergey Zolotarev– Sergey Zolotarev2025年04月08日 11:26:16 +00:00Commented Apr 8 at 11:26 -
1But it is so blatantly ungrammatical, in any context!!Sergey Zolotarev– Sergey Zolotarev2025年04月08日 13:51:21 +00:00Commented Apr 8 at 13:51
-
2I think this is the point of my answer. "IsX", "HasX" are in common usage and the meaning is well understood. Use this standard because of that. Not because sometimes its grammatical in english, and DEFINITELY don't go through refactoring standard following names to make you code read like english.Ewan– Ewan2025年04月08日 13:56:12 +00:00Commented Apr 8 at 13:56
Obviously "is" is stupid if it’s not true. You may have prefixes like "has" (like a button is not a title, but a button can have a title). Or "can" (you can paste text into a text field but not an audio file), or other suffixes describing correctly what the result of the Boolean function is.
-
'Obviously "is" is stupid if it’s not true." — how so?Greg Burghardt– Greg Burghardt2025年04月08日 12:32:38 +00:00Commented Apr 8 at 12:32
-
You have a button object. Buttons can have titles, or not have titles. They are not titles, So aButton.isTitle is nonsense and stupid. But why do I have to explain that?gnasher729– gnasher7292025年04月08日 12:44:53 +00:00Commented Apr 8 at 12:44
-
2Because you want use isTitles but isTitled. It is not just "is" but the whole name that should be selected sanely. Apple name conventions document talks probably too much about grammars.Giacomo Catenazzi– Giacomo Catenazzi2025年04月08日 14:21:40 +00:00Commented Apr 8 at 14:21
-
Ok, perhaps I misunderstood your statement. Are you saying that using "is" in a name that doesn't imply a yes/no answer is illogical? If that's what you meant, then I agree, but the statement might be overly broad to universally think "is" in a name should be forbidden.Greg Burghardt– Greg Burghardt2025年04月08日 15:26:49 +00:00Commented Apr 8 at 15:26
-
I read your statement as "is" is stupid if it's (false) ... which threw me for a loop.Greg Burghardt– Greg Burghardt2025年04月08日 15:28:04 +00:00Commented Apr 8 at 15:28
int
and converts null to 0.if (areMultipleRowsSelected())
vsif (multipleRowsSelected())
.multipleRowsSelected
is just a shorter variant of the predicate form.getAutoCreateRowSorter
- so even the Java API doesn't adhere to its own naming guidelines.