This question puzzled me several times.
Imagine a class representing a resource, and in order to be able to use this resource, you must first call the Open method on it, otherwise an InvalidOperationException will be thrown.
Should my code also check if someone is trying to open an open resource or close an already closed one?
Should the code prevent a logically invalid call, even if no harm was done?
I think that programming in this way will help to write better code on the other side, but I feel that I can take on too much responsibility and affect reuse.
What do you guys think?
Edit:
I do not think that this could be called defensive programming, because it would not allow possible misuse to slip through, and another InvalidOperationException will be thrown.
source
share