invariants

How to maintain a recursive invariant in a MySQL database?

I have a tree encoded in a MySQL database as edges: CREATE TABLE items ( num INT, tot INT, PRIMARY KEY (num) ); CREATE TABLE tree ( orig INT, term INT FOREIGN KEY (orig,term) REFERENCES items (num,num) ) For each leaf in the tree, items.tot is set by someone. For interior nodes, items.tot needs to be th...

What is an invariant?

The word seems to get used in a number of contexts. The best I can figure is that they mean a variable that can't change. Isn't that what constants/finals (darn you Java!) are for? ...

How do you validate an object's internal state?

I'm interested in hearing what technique(s) you're using to validate the internal state of an object during an operation that, from it's own point of view, only can fail because of bad internal state or invariant breach. My primary focus is on C++, since in C# the official and prevalent way is to throw an exception, and in C++ there's n...

Are missing invariants for an object always a sign of bad design?

I was just thinking of cases when invariant(s) for mainly classes, but to some extent also structs, cannot be readily defined. Would the lack of invariant(s) be a definitive sign of bad design of the class or struct, or can you see valid reasons not to be able to define them? To me, it feels like that's where you end up when things are ...

Empty constructors and setters on JPA Entites

Hi! I don't like the requirement on have at least one empty constructor and public setters on JPA entities. While I understand the issue on the EntityManager side, this invalidates class invariants. Does anyone have a solution for this (design pattern or idiom level) ? Thanks! Igor ...

How free can I be in the code in an object invariant?

I'm trying to demonstrate invariants in Code Contracts, and I thought I'd give an example of a sorted list of strings. It maintains an array internally, with spare space for additions etc - just like List<T>, basically. When it needs to add an item, it inserts it into the array, etc. I figured I had three invariants: The count must be ...

Are preconditions and postconditions needed in addition to invariants in member functions if doing design by contract?

I understand that in the DbC method, preconditions and postconditions are attached to a function. What I'm wondering is if that applies to member functions as well. For instance, assuming I use invariants at the beginning at end of each public function, a member function will look like this: edit: (cleaned up my example) void Charcoa...

Do you use invariants when you program?

I am taking an intermediate programming course which stresses the use of invariants. I have never used them before and they seem to take up more time to create. Does the software engineering industry stress the use of invariants? ...

Opinion on "loop invariants", and are these frequently used in the industry?

I was thinking back to my freshman year at college (five years ago) when I took an exam to place-out of intro-level computer science. There was a question about loop invariants, and I was wondering if loop invariants are really necessary in this case or if the question was simply a bad example... the question was to write an iterative de...

What is the best way of determining a loop invariant?

When using formal aspects to create some code is there a generic method of determining a loop invariant or will it be completely different depending on the problem? ...

Beginner question - Loop invariants (Specifically Ch.3 of "Accelerated C++")

Hi - as I said, a complete beginner question here. I'm currently working my way through "Accelerated C++" and just came across this in chapter 3: // invariant: // we have read count grades so far, and // sum is the sum of the first count grades while (cin >> x) { ++count; sum += x; } The authors follow this by explaining t...

Heuristics for finding class invariants.

Hello everybody! Is there any heuristics for finding class invariants, I mean pay attantion on ...; never rely on ...; Maybe there is common advices. Any links on paper where studing real-life examples will be welcome. ...

Adding Invariants to Interfaces in Java

I've been thinking about creating a Java framework that would allow programmers to specify invariants (pre- and post-conditions) on interfaces. The purpose would be to make code more robust and reduce the number of unit tests that would need to be written for different implementations of the same interface. I envisage creating some way ...

Set size on char array in Java

Hello. I'm developing an Android application. I want to set size to a char array like this: public char[5] language; But it doesn't work. I have to delete number five to make it work. I want to limit to five characters to language variable. How can I do that? Thanks. ...