3

When writing Boolean getter/setter methods it seems standard to write them like so

void setValue(boolean value) { }

boolean getValue() { return true/false; }

However I have seen some usage which states that the getter method should be named appropriately according to its usage.

Lets say for example I am setting/getting a Boolean called "enabled", should my getter be called:

getEnabled()

or

isEnabled()

again lets say for example I am setting/getting a Boolean called "nodes", should my getter be called:

getNodes()

or

hasNodes()

Is this just down to personal preference, or is it good practice to choose a particular naming convention over another?

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • 6
    `nodes` sounds like a really poor name for a boolean. – BoltClock Sep 24 '12 at 14:27
  • 2
    I'd say that it's up to your personal preferences (or the preferences of your team/employer). However, I'd just like to state that I'd be confused if I ever saw a `bool getNode()` method... – Alxandr Sep 24 '12 at 14:28
  • 1
    if "nodes" is boolean so better to use "isNodes", but if "nodes" is collection, then "hasNodes" better – Vytalyi Sep 24 '12 at 14:28
  • 1
    c# has properties as a first-class concept: you wouldn't normally have an explicit getValue / setValue in c# - you'd have a Value with a get/set *accessor* – Marc Gravell Sep 24 '12 at 14:29
  • See [this answer](http://stackoverflow.com/questions/5322648/have-a-boolean-field-whats-the-naming-convetion-for-its-getter-setter) by Jigar Joshi. – Jesper Sep 24 '12 at 14:29
  • also, not meaning to be nit-picky but Boolean != boolean and this is where the get vs is comes into play in most cases. a boolean can't be null where as a Boolean CAN be null – O'Mutt Sep 24 '12 at 14:30

4 Answers4

4

This answer assumes question is related to Java:

It is always better to go with conventions. Some frameworks like Spring are tightly coupled with Java Beans conventions.

Jesper
  • 202,709
  • 46
  • 318
  • 350
kosa
  • 65,990
  • 13
  • 130
  • 167
1

In C# properties are NOT methods. Well, they are methods, but a special type of methods.

You declare a property like this:

public bool Enabled
{
   get
   {
       // return your value
   }
   set
   {
      // assign your value
   }
}
Yván Ecarri
  • 1,661
  • 18
  • 39
  • 1
    Personal preference, but I also don't like my properties to be verby. If this were C# and I were implementing this, I'd just name it `Enabled`. You're right that it's not a method, so I'd shy away from method convention, like verbs or verb phrases as names. – Marc Bollinger Sep 24 '12 at 14:31
  • Never mind that you kinda already have a verb or verb phrase either way. It's just more obvious when you say `IsEnabled` that you mean whether the thing *is currently enabled*, rather than (say) whether it *enabled* at some point in the past. – cHao Sep 24 '12 at 14:53
0

In Java:

All getters and setters must be at least 4 characters long and have an upper case fourth letter.

The only exception is getters of boolean or Boolean which can be 3 letters long and can start with is and must have a upper case letter for the third character.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    Should your first line say "fourth"? – Marc Gravell Sep 24 '12 at 14:31
  • why MUST getters and setters be at least 4 characters long? Will the program break if they are not, or should this say "SHOULD" instead? – Matthew Layton Sep 24 '12 at 14:55
  • I often use getters and setters which are just the name of the field. I do this to make it clear they are not JavaBean. However, if you want to make use of JavaBean based libraries, you have to follow the convention or the methods will be ignored. – Peter Lawrey Sep 24 '12 at 15:04
  • @activwerx: Java bean specs. A getter, by those specs, is named like `getSomething` (or `isSomething` for booleans). If you don't follow that naming convention, IDEs and bean users probably won't recognize your properties. Course, if you don't care about that, you can use "is", "has", "may", or whatever else you want. – cHao Sep 24 '12 at 15:16
  • if (myControl.Enabled) { .... Just looks better – Jawid Hassim May 22 '23 at 07:47
0

That's one (of many) reasons why I prefer C#

public bool Enabled
{
    get { return _enabledValue;}
    set { _enabledValue = value;}
}

and it is so 'natural' to call on the property

myControl.Enabled = true;
...

if(myControl.Enabled == true)
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Ditto, this is the reason I prefer C#...more syntactic sugar! – Matthew Layton Sep 24 '12 at 14:32
  • again, the question remains, should this property be called Enabled or IsEnabled? – Matthew Layton Sep 24 '12 at 14:33
  • I still prefer `Enabled`. The inclusion of the verb is.... or has... remind me of an action while I think of properties like attributes of a class – Steve Sep 24 '12 at 14:35
  • @Steve: You're aware that "Enabled" is a verb as well, right? – cHao Sep 24 '12 at 15:10
  • I like to evaluate Booleans like this (using your example): if(myControl.Enabled)...so to me saying if(mycontrol.IsEnabled) seems more natural. Don't get me wrong I respect your answer, but I'm trying to get the bigger picture as to what people prefer...Lets say I was writing a class as part of a popular library and in my class I had a properly called IsEnabled, do you think people would frown upon the use of the "Is" prefix, or just accept it? – Matthew Layton Sep 24 '12 at 15:16
  • No absolutely no. There is no real reason to worry about this. As I have said, it's just a personal preference. I could find also a logical reason to find your choiche better. If you have all your boolean properties prefixed with `Is..` then Intellisense will show these properties ordered and it will be easier to find them – Steve Sep 24 '12 at 15:21
  • @activwerx: I personally prefer it. That, or "has", etc. I like my booleans to be verby, because that flag by its very nature represents whether something *is* true...or whether something *has* a certain property...or whether something *does* something else. It's about 80% personal preference, though. (The other 20% comes into play when you have other code you have to be compatible with.) – cHao Sep 24 '12 at 15:22
  • Based on this answer, including all comments from everyone...I accept! :-) – Matthew Layton Sep 24 '12 at 15:25
  • @cHao I implore you, as a mitigating factor, the fact of not being a native English :-) – Steve Sep 24 '12 at 15:26