I was reading this MSDN article about the usage of properties and methods in .NET. It points out why and when to use properties or methods.
Properties are meant to be used like
fields, meaning that properties should
not be computationally complex or
produce side effects.
Otherwise one should use methods.
I was asking myself how you could express this difference in Java.
What is your opinion?
Just don’t use the
getprefix on the method, as it normally implies that the method will be cheap (as getters usually only access fields, delegate to other getters, or perform fairly simple calculations based on other getters). For example, if a class has this interface:… it would seem that getting the length, contents, and checksum from the
Blobare equally costly. If we did like this, instead:… it becomes clear(er) that we can expect
calculateChecksum()to be more expensive than the other operations, as its name says it’s going to do more than just get something.To a certain degree, the complexity is an implementation issue that shouldn’t be seen in the interface (maybe I decide to calculate the checksum eagerly when the
Blobis constructed?), but there are occasions where it makes sense to make a distinction.