Suggestion: "fix" assert(obj)
Ary Manzana
ary at esperanto.org.ar
Fri Jun 15 18:47:11 PDT 2007
What I meant was: if the compiler runs the invariants before and after
each method call... when would you like to explicitly check the
invariant using assert?
---
SomeObject o = new SomeObject;
// Here the invariant is called automaticaly
o.someMethod();
// Here the invariant is called automaticaly
assert(o); // what's the point? It was done just a second.
---
Unless... Unless you modify a field, and then call the invariant to
check if everything is in place. But that's a smell of bad design.
Georg Wrede escribió:
> ((Reversing another top-post))
>
> Ary Manzana wrote:
>> Kristian Kilpi escribió:
>>
>>> This issue has been proposed before. Well, I think it's the time
>>> suggest it again...
>>>
>>> The problem is that
>>>
>>> assert(obj);
>>>
>>> does not first check if 'obj' is null. It just executes the
>>> object's invariants.
>
>> Why would you want to check an object's invariant? Isn't the compiler
>> doing that before and after you execute a public method? What's the
>> use for it?
>
> First: checking invariants is (most normally) a runtime excercise.
> Normally the situations that lead to a risk of "breaking" the invariants
> are due to circumstances the programmer didn't think of at first hand,
> which naturally suggest a runtime check, usually with Real Use Case Data.
More information about the Digitalmars-d
mailing list