I was asking myself how you could express this difference in Java.
Just don't use the get
prefix 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:
class Blob {
long getLength() { ... }
ByteBuffer getBytes() { ... }
Sha1Checksum getChecksum() { ... }
}
... it would seem that getting the length, contents, and checksum from the Blob
are equally costly. If we did like this, instead:
interface Blob {
long getLength() { ... }
ByteBuffer getBytes() { ... }
Sha1Checksum calculateChecksum() { ... }
}
... 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 Blob
is constructed?), but there are occasions where it makes sense to make a distinction.