2 Replies Last post: Mar 22, 2013 7:22 PM by Mark Vedder  
Arthur Kramer Newbie 12 posts since
Feb 14, 2013
Currently Being Moderated

Mar 20, 2013 1:40 PM

bean naming conventions -> getter/setter

When I have a field called e.g. xCoordinate it appears to me that the getter shoud be named getXCoordinate().

 

Why? Because "get" is clearly a name standard in Java, then "X" and "Coordinate" are distinguishable by the case shift (small x -> big C) so "X" is is clearly the start and end of a word at the same time and should be capitalized.

 

Now IntelliJ auto-creates a getter called "getxCoordinate". Even though Eclipse does the same, it still appears to me that this is wrong since there is no more distinction between "get" "X" and "Coordinate" rather "getx" forms a non-existing term. Why is this so?

Denis Zhdanov Expert 1,620 posts since
Oct 20, 2007
Currently Being Moderated
Mar 20, 2013 4:05 PM in response to: Arthur Kramer
Re: bean naming conventions -> getter/setter

That is related to javabeans specification. Consider a sample below:

 

package org.denis;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

public class SimpleTest {

    private int xCoordinate;
    private int yCoordinate;

    public static void main(String[] args) throws Exception {
        BeanInfo info = Introspector.getBeanInfo(SimpleTest.class);
        for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) {
            System.out.println(descriptor.getName());
        }
    }

    public int getxCoordinate() {
        return xCoordinate;
    }

    public int getYCoordinate() {
        return yCoordinate;
    }
}

 

It produces the following (notice 'YCoordinate' instead of 'yCoordinate'):

 

YCoordinate

class

xCoordinate

 

Denis

Mark Vedder Apprentice 1,003 posts since
Dec 10, 2003
Currently Being Moderated
Mar 22, 2013 7:22 PM in response to: Denis Zhdanov
Re: bean naming conventions -> getter/setter

If you are currious why it does this, section 8.8 Capitalization of inferred names of the Javabeans Specification explains it. It's basically to prevent getURL from seeing the property name as uRL.

 

Here's the section (empahsis added):

When we use design patterns to infer a property or event name, we need to decide what rules to follow for capitalizing the inferred name. If we extract the name from the middle of a normal mixedCase style Java name then the name will, by default, begin with a capital letter.

 

Java programmers are accustomed to having normal identifiers start with lower case letters. Vigorous reviewer input has convinced us that we should follow this same conventional rule for property and event names.

 

Thus when we extract a property or event name from the middle of an existing Java name, we normally convert the first character to lower case. However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone. So for example,
    “FooBah” becomes “fooBah”
    “Z” becomes “z”
    “URL” becomes “URL”

 

We provide a method Introspector.decapitalize which implements this conversion rule.

More Like This

  • Retrieving data ...