tags:

views:

10

answers:

1

Say you have type(interface/class) A and A has

A.setX(String x) (and all gets)
A.setY(String y)
.
.

And you have this API:

save(A a);

save method requires A to have X, Y and 10 other values set, but a few other properties are optional. So how can save method tell this kind of requirement to rest of the world?

You can assume that the caller only knows about the signature and documentation of method.

A: 

Depending on what your application is, you might want to consider making A an immutable type that had to have x and y set at all times. Then, the save method will always work.

Failing that you could give the save method the appropriate preconditions (possibly implemented with assertions if your language supports them and they're appropriate for your application).

David
x, y and 10 other properties, the biggest problem is having requirement that many of them must be set except a few. Yes assertions will enforce object to have appropriate preconditions but you can not really know about assertions by looking at method signature.
erdogany