Coding style for Java properties

This article is about coding the "property" pattern in Java. Java's version of this pattern is described in the JavaBeans Tutorial.

A property of a class can take many forms

  Kind of 
 property 
 Accessors  prop   getProp()   setProp() 
   neither X    
normal  read-write X X X
normal  read-only X X  
normal  write-only (NO!) X   X
virtual  read-write   X X
virtual  read-only   X  
virtual  write-only (NO!)     X

C style carried over to Java

Here's an example of two read-write Java properties in age-old C coding style:

protected User user;
protected RuntimeOptions runtimeOptions;

public User getUser() {
    return user;
}

public void setUser(User newValue) {
    user = newValue;
}

public RuntimeOptions getRuntimeOptions() {
    return runtimeOptions;
}

public void setRuntimeOptions(RuntimeOptions newValue) {
    runtimeOptions = newValue;
}

In ancient C tradition you put all variables at the top of a function. You had no choice because the C language forced this (until the advent of the too-rarely-used "block" construct). Later, C++ and Java came along and allowed declarations to be interspersed with code, but the C tradition persists. In my view that style, borne of deprevation, is obsolete within methods in Java and is especially obsolete and inappropriate when applied by analogy to member variables of a class. A class is a different thing from a function, and getters and setters completely break the analogy.

If you see nonpublic member variables at the top of a class you must guess whether there may turn out to be related getters and setters if you look below. You'll have to look to see if they have complex behavior, which is important to know (for example what if the member variable is an Integer but the getter returns an int). Member variables aggregated at the top claim by their aggregate prominence to represent all properties, yet they are silent about the existence of "virtual properties" (properties that have a getter and/or setter not backed by a member variable).

Proposed Java style

The style I've developed for Java keeps everything for each property together (member variable / getter / setter and related constants) and visually sets them all off as a unit. When the getter and setter are trivial, I make them one-liners, visually aligned so you can recognize the standard pattern at a glance, like this:

protected User    user;
public    User getUser() { return       user; }
public    void setUser(User newValue) { user = newValue; }

protected RuntimeOptions    runtimeOptions;
public    RuntimeOptions getRuntimeOptions() { return                 runtimeOptions; }
public    void           setRuntimeOptions(RuntimeOptions newValue) { runtimeOptions = newValue; }

If the getter or setter is not simple, then it's spelled out but still attached to its compatriots (no blank lines separate them), like this:

protected User    user;
public    User getUser() { return       user; }
public    void setUser(User newValue) { user = newValue; }

protected RuntimeOptions    runtimeOptions;
public    RuntimeOptions getRuntimeOptions() {
    sideEffect1();
    return runtimeOptions;
}
public    void           setRuntimeOptions(RuntimeOptions newValue) {
    runtimeOptions = newValue;
    sideEffect2();
}

Actually, I do keep part of the old C tradition: I try as much as possible to put all property units first after all constructors, followed by procedures (i.e. methods that do something other than returning or changing the object's state).

Futures

I would like to see Java provide a first-class syntax for properties.

One of the core principles of good programming style in my book is localize, localize, localize: keep stuff together that belongs together, and if you can encapsulate it so that unwanted outside access is impossible, so much the better. A special syntax for a property unit could be specified to deny access to the property's member variable even from within the same class. The syntax might look something like this:

// Trivial case:
// type name lowercased is property name, public getter and setter, protected member variable
public RuntimeOptions {}

// protected getter and setter, static, nondefault name
protected static RuntimeOptions options {} 

RuntimeOptions options {
    private value;      // member variable is private instead of protected
    public  getValue(); // streamlined syntax for the obvious trivial case
    public  setValue(); // streamlined syntax for the obvious trivial case
}
			
RuntimeOptions options {
    public getValue();
    public setValue(RuntimeOptions newValue) {
        value = newValue;
        sideEffects();
			}
}

http://Yost.com/computers/java/property-style - this page
2000-08-08 Created
2001-04-07 Published here