views:

1295

answers:

5

I've been having an argument about the usage of the word "accessor" (the context is Java programming). I tend to think of accessors as implicitly being "property accessors" -- that is, the term implies that it's more or less there to provide direct access to the object's internal state. The other party insists that any method that touches the object's state in any way is an accessor.

I know you guys can't win the argument for me, but I'm curious to know how you would define the term. :)

+1  A: 

I've always gone by the first definition. So, generally that applies just to getters and setters. If we go by the second method, then it's a far less useful distinction, since that covers almost all methods.

Don Branson
Precisely the reason I'm arguing this case. :)
Rytmis
Great minds think alike - and so do we! ;)
Don Branson
+3  A: 

An accessor method does exactly what it says on the tin: accesses some state from the type without side effects (apart from lazy instantiation, perhaps, which is not something that the caller would normally know about).

private int _age;

public int getAge()
{
    return _age;
}

Methods that modify state are more usefully considered (in my opinion) as mutators.

Drew Noakes
I agree with this. Mutators change the state, accessors inspect the state. I would say that the term accessor also applies to derived properties of an object. After all, even a derived property reflects the state of an object.
KarstenF
+5  A: 

By accessors, I tend to think of getters and setters.

By insisting that all methods that touch the object's internal state are accessors, it seems that any instance method that actually uses the state of the object would be an accessor, and that just doesn't seem right. What kind of instance method won't use the state of the object? In other words, an instance method that doesn't use the object's state in some way shouldn't be an instance method to begin with -- it should be a class method.

For example, should the BigDecimal.add method be considered an accessor? It is a method that will read the value of the instance that the add method was called on, then return the result after adding the value of another BigInteger. It seems fairly straight forward that the add instance method is not a getter nor a setter.

coobird
+1  A: 

Besides googling and wikipedia, the Java Language Specification shows this as an example of an accessor method:

private static int N;
public static int getN() { return N; }

So, yes, I'd say it just gets the value of a field. The compiler may inline this, converting it to a simple read, so anything more than that probably isn't an accessor.

John Ellinwood
That's not a conclusive argument. Just because the spec gives that example doesn't mean that accessor methods aren't other things as well.
Rob Kennedy
A: 

It is good to be able to differentiate getters and setters in technical conversation. Accessor methods are partners to modifier methods. Accessors read the state of an object (getA()), while modifiers write the state (setA(Object)).

akf