All this has me thinking about pre & post conditions: who has the responsibility to check pre conditions? The client code so they don't cause problems; also the objects' code again so an exception can be thrown in the case that the input is bad?
The Post conditions seem an easy enough concept - leave the object in a valid state no matter what, and provide return values in the appropriate range.
Then I started to think about
Scott Meyers' point about
making code hard to use wrongly. He had an example of providing a class for each month of the year, so that a Date class would be used without ambiguity.
For me, I have a function which takes Latitudes & Longitudes as parameters. Rather than having these parameters as doubles, then checking their validity and making sure they are in radians, instead I make the parameters
CLatLong
objects.
The
CLatLong
object has the responsibility to validate the values it holds and it also has a member which is in radian units. This way the
CLatLong
would throw an exception - why should we do anything much with a Latitude of
-900.0
?
Hopefully this means my function could provide a strong(?) guarantee that it won't throw - well at least in terms of the input anyway, I would need to investigate anything else that might potentially go wrong.
@
dhayden
I wonder: did you see this bit?
theIdeasMan wrote: |
---|
The article above talks about fixing bugs as a must do over using exceptions, which is I guess what you are saying. But if one can't fix the bug in the client code, then shouldn't an exception be provided anyway? |
I agree with you - one must fix bugs. I can't help thinking on the other hand that one should provide exceptions for other things that go wrong.
Hope all is well with everyone :+)