Tag Info

Hot answers tagged

20

The notion of invariant is strongly linked with 'side effects'. I believe it was promoted by Bertrand Meyer's 'Design by Contract (DbC)' approach for software design. DbC enriches Abstract Data Types (backbone of classes) with 3 important notions, preconditions, postconditions, invariants. It is easily explained when referring to procedures, so I'll try to ...


9

In OOP, an invariant is a set of assertions that must always hold true during the life of an object for the program to be valid. It should hold true from the end of the constructor to the start of the destructor whenever the object is not currently executing a method that changes its state. An example of invariant could be that exactly one of two member ...


9

A loop invariant is simply something that is true on every iteration of the loop. For example, take a really trivial while loop: while x <= 5: x = x + 1 Here the loop invariant would be that x ≤ 6. Obviously, in real life, loop invariants are going to be more complicated--finding the loop invariant in general is something of an art and cannot easily ...


7

An invariant is a condition that can be relied upon to be true during execution of a program. For example, a loop invariant is a condition that is true at the beginning and end of every execution of a loop. An assertion is a predicate (a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that ...


6

The need for covariance and contravariance can be best understood with an example. Let's say you have a function that accepts a parameter of type List<Base>, where Base is a base class that other classes inherit from. You would intuitively think that it would be just fine to pass a List<Derived1> to this routine, if Derived1 inherits from Base, ...


5

An algorithm is a repeatable process. If it is repeatable, it has to have attributes that do not change with repetition. These are your invariants. The invariants are combined with and/or operate on the (potentially) varying data that will be fed into your algorithm. Thus the whole point of programming is to identify what does not vary--that is ...


4

Choose solution #1. There is nothing wrong with setters containing validation or coercion logic. In fact, that is the only reason why we'd use setters and getters instead of public member fields! Your create factory method is also a bit awkward, as there is no reason not to use the constructor instead: public class UserCredentials { private String ...


4

I only recently learned that I needed to have contravariant interface to be able to pass that interface as a parameter in C# and this feature was only added in .NET 4.0. .NET 4 added co/contra-variance for generics. The variance concept though exists in any language with subtyping. When doing type checking, a parameter type is acceptable if it is the ...


3

My favorite: failure to initialize correctly minIndex = 0 minValue= someRandomValue # NOT someList[minIndex] for i in range( 0, len(someList) ) # NOT range( 1, len(someList) ): assert min(someList) == someList[minIndex] # not true initially if someList[i] <= someList[minIndex]: minIndex= i minValue= someList[minIndex] ...


3

Common Numeric Problems Overflow, underflow. What is wrong with int newArrayLength = arr.length * 2; T[] newArray = new T[newArrayLength]; ? Floating Point Failure to account for NaN when comparing using <, >, <=, =>. What is wrong with this double max(double a, double b) { return a < b ? b : a; } ? Filtering Failing to account for ...


2

This is mostly a rewording of this excellent blog by Eric Lippert: C# sub types have always been assignment compatible with their base types e.g. given that Teacher is derived from Person Person p = new Teacher(); is valid i.e. there is a relation isAssignable(x,y) which is true IFF x= y is allowed. before C# 4 generic collections of sub types were not ...


2

An invariant is a logical property that is preserved by some operation(s). You need invariants to reason about loops. Since you don't know beforehand how many iterations there will be (or you wouldn't need a loop), each iteration must preserve the invariant, so that at the end you can prove some useful property about the loop. You need invariants to reason ...


2

An invariant (in common sense) means some conditions that must be true at some point in time or even always while your program is executing. e.g. PreConditions and PostConditions can be used to assert some conditions that must be true when a function is called and when it returns. Object invariants can be used to assert that a object must have a valid state ...


1

The different forms of variance are only needed because you have two independent axes of polymorphism going on: Subtype polymorphism: A function that is defined to operate on values of type Base can also operate on values of type Derived, if Derived is a subtype of Base. Within the function, only the operations exposed by type Base may be used (unless ...


1

In general, mutating state is the biggest cause (and may in fact be the only cause) of corrupted invariants. I don't mean to start the age-old imperative vs functional debate, but there's a reason why languages like Haskell are so popular amongst academics: it's much easier to prove purely functional code correct. Of course, whether functional code is ...


1

Based on the following quote from Coders At Work... But once you know the invariant that it's maintaining, you can see, ah, if we maintain that invariant then we'll get log lookup time. ...I guess "invariant" = "condition you want to maintain to ensure a desired effect". It seems that invariant has two senses that differ in a subtle way: Something ...



Only top voted, non community-wiki answers of a minimum length are eligible