|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
D - invariants and infinite loops
Here's the code that an (unexperienced) programmer
could write:
class Control
{
public:
int Left() { return left; }
int Top() { return top; }
int Right() { return right; }
int Bottom() { return bottom; }
private:
int left, right, top, bottom;
invariant()
{
assert(Right >= Left); // note the use of
assert(Bottom >= Top); // property-getters
}
}
Now this would result in an infinite loop, since
on each call to method invariant block is checked
which calls a method... etc. Mistake is especially
easy to commit if method is a property-getter.
Maybe any method calls on "this" should be forbidden
in invariant blocks?
BTW, why () after "invariant"? unittest doesn't have
them, nor do try, in/out/body...
Nov 11 2001
Pavel \"EvilOne\" Minayev wrote:Here's the code that an (unexperienced) programmer could write: Now this would result in an infinite loop... Nov 11 2001
John> Properly, you shouldn't be allowed to call a public John> method from a private method, because the public method John> assumes you're outside the object, and the private method doesn't John> reestablish the invariant before the call. John> John> This is probably too formalist, but it's right. Thats far to strict in my eyes, against the paradigm, and from the human programmers view people will feel nothing but beeing pestered. I would vote for not writing this extra 'access functions' which do nothing but return a private class variable. Why not create a new access level for variables called maybe "show". Anyone may read it, but only the class internal routines itself may modify it. - Axel -- |D) http://www.dtone.org Nov 11 2001
"Axel Kittenberger" <axel dtone.org> wrote in message news:9smksj$2qfc$1 digitaldaemon.com...Thats far to strict in my eyes, against the paradigm, and from the human programmers view people will feel nothing but beeing pestered. Nov 11 2001
"Pavel "EvilOne" Minayev" <evilone omen.ru> wrote in message news:9snmng$emt$1 digitaldaemon.com...Besides, with all those aggressive optimizations that D is supposed to perform, Nov 29 2001
|