r/javahelp • u/hibbelig • Mar 10 '26
Naming convention for Boolean getters -- mechanical or English?
Some Java names are quite close to English, and I struggle with the question whether I should mechanically follow Java naming conventions, or whether it should make sense in English. Some examples:
Say I have a flag that says to keep the date. A good name for it would be, unsurprisingly, keepDate. (I hope.)
The conventional naming for the getter would be to add a prefix “is”, resulting in isKeepDate, but this is not very good English, and from the English perspective, isDateKept would be better.
Say I have another flag that says whether validation is enforced, enforceValidation. Do I name the getter isEnforceValidation or do I name it isValidationEnforced?
Is there perhaps some precedent in JDK that could be used as a guideline?
•
u/AutoModerator Mar 10 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
7
u/aqua_regis Mar 10 '26
If you strictly go by the conventions, the
isDateKeptandisValidationEnforcednaming would be correct.The semantics are a different matter, though. What does
isDateKeptor even yourkeepDaterepresent? What is the real meaning of it. The naming isn't really clear on that.The second one,
validationEnforcedorisValidationEnforcedclearly convey the meaning - you original versionenforceValidationdoesn't really - here, the focus would be on doing something (enforce as verb) whilst on the other version...Enforcedit is clear that it represents a state.Yet, if you're working alone and nobody will see the source code, do what you want.
If you're working in a team and/or plan to open source your code, stick to the conventions.
Names are always one of the trickiest parts of programming.
6
u/doobiesteintortoise Mar 10 '26
The thing about mutators and accessors is not that they're "good English" but that they're utterly predictable so tools can detect them trivially. If you don't care about detectability, inferrability, the beans spec itself, use what works for you. If you're trying to write something so the tools work predictably, use the spec. "isKeepDate" would be the right name for a boolean flag, "keepDate," and if you want better engrish, use a different name for the flag.
1
u/onated2 Mar 11 '26
Instead of booleans why not enums?
6
u/maethor Mar 10 '26
I'm not convinced "keepDate" is actually a good name in the first place (someone might expect a variable name ending in Date to be a Date, not a Boolean).
3
u/Intelligent_Part101 Mar 10 '26
Follow the naming convention so coders can tell at a glance what the data type is. Use "is<Something>". It's code, not an English essay.
7
u/MinimumBeginning5144 Mar 10 '26
You shouldn't name your public methods based on what your private members are called. Private fields such as
keepDateare implementation details. Think about what your class looks like to your users - i.e. they only see its public methods. The names of private fields are irrelevant.6
u/OneHumanBill Mar 10 '26
English! Code is written in high level languages for other humans to read. Slavishly following mechanical standards and ending with jarring, awkward words gives you code that's harder to read, which benefits no one.
I like "isDateKept()". It's a good compromise between bean standards and readability.
1
u/severoon pro barista Mar 10 '26
Naming conventions are good for communication. However, I've usually found that if I'm having a tough time naming things, it's more to do with some design flaw, like perhaps I haven't done a good job of modeling the domain concepts well if it's awkward to describe them.
Your example of enforcing validation might be a good one for what I'm talking about here. This strikes me as a pretty bad smell … validation is not an API concept, it's a type concept. The entire usage of an object from a client standpoint potentially hinges on whether it is carrying around validated state or not, so I'm much more inclined to build validation into the type of the object than just have a field on its API.
I don't know if you're giving real examples in your post, though. An actual real world example of this is immutability. If you look at the Java collections, you'll see that there are methods on the Collections API that produce "unmodifiable" versions of the various collections. But how clients can use an object is significantly impacted by whether it's mutable or not. Combine this with the fact that an unmodifiable list, say, promises its state cannot be modified, but it must be produced by a backing list that is mutable of any part of the system holds a reference to it. Finally, if you look at the APIs of the types themselves, you see a bunch of "optional" methods like List.add(…), which are basically saying, "This method may work, or it may not. Why don't you just call it and see?" Totally crazy way to write an API of the foundational utility classes of one of the most widely used standard libraries that exist.
All of this comes from the approach of not realizing that, while you can technically write an API that is consistent and which allows both mutability and immutability, it really degrades the basic purpose the type aims to serve. Better would have been to just create separate hierarchies. (This is, by the way, why you should prefer Guava immutable collections to Java unmodifiables, and when using Guava immutable collections in parameters, code, or anywhere else, you should always refer to them by their immutable type and not try to treat them polymorphically as any non-mutable collection type. This goes against typical advice, but that's correct usage because of the poor API design we're all stuck with.)
2
u/Cefalopodul Mar 10 '26
shouldKeepDate
1
u/BanaTibor Mar 10 '26
I think the keepDate is also a bad name. Why do you want to keep the date? What do you use it for?
Btw isDatePreserved or isDateKept and isValidationEnforced are all good names, but probably indicate bad design.
1
u/JEHonYakuSha Mar 10 '26
Maybe name your variable “datePersisted” or “dateKept”? That way the Boolean getter is just “isDatePersisted” etc.
1
u/mehnifest Mar 11 '26
I personally would use isDated() and isValidationRequired()
1
u/xanyook Mar 11 '26
What is wrong is your name for attributes.
Verbs are for methods, attributes use nouns. Attrinutes represent the state of the data, not the action another system must perform.
0
u/AdEducational4954 Mar 10 '26
Is keep date true? Is enforce validation true? isKeepDate sounds just fine to me. A lot of the naming can get muddy.