2

For example I have the following code:

public class Calc(){
  final int PI = 3.14; //is this an invariant?

  private int calc(int a, int b){ 
      return a + b;
      //would the parameters be pre-conditions and the return value be a post-condition?
  }
}

I am just confused on what exactly these terms mean? The code above is what I think it is, however can anyone point me into the right direction with my theory?

5 Answers5

6

Your code is in a contract with other bits and pieces of code. The pre-condition is essentially what must be met initially in order for your code to guarantee that it will do what it is supposed to do.

For example, a binary search would have the pre-condition that the thing you are searching through must be sorted.

On the other hand, the post-condition is what the code guarantees if the pre-condition is satisfied. For example, in the situation of the binary search, we are guaranteed to find the location of what we were searching for, or return -1 in the case where we don't find anything.

The pre-condition is almost like another thing on top of your parameters. They don't usually affect code directly, but it's useful when other people are using your code, so they use it correctly.

Clark
  • 1,357
  • 1
  • 7
  • 18
5

A invariant is a combined precondition and postcondition. It has to be valid before and after a call to a method. A precondition has to be fullfilled before a method can be run and a postcondition afterwards.

Java has no mechanisms for the condition checking built in but, here's a little example.

public class Calc {
    private int value = 0;

    private boolean isValid() {
        return value >= 0;
    }

    // this method has the validity as invariant. It's true before and after a successful call.
    public void add(int val) {
        // precondition
        if(!isValid()) { 
           throw new IllegalStateException(); 
        }

        // actual "logic"
        value += val;

        // postcondition
        if(!isValid()) { 
            throw new IllegalStateException(); 
        }
    }
}

As you can see the conditions can be violated. In this case you (normally) use exceptions in Java.

Clark
  • 1,357
  • 1
  • 7
  • 18
schlingel
  • 8,560
  • 7
  • 34
  • 62
2
 private int calc(int a, int b){ 
      return a + b;
      //would the parameters be pre-conditions and the return value be a post-condition?
  }

Is a function that takes two int and returns an int, which is the summation of a and b.

You would normally call the calc function in main as

public static void main(String[] args)
{
  int a = 3, b = 4;
   int sum =  calc(a, b);
}

when you do that, a copy of a and b is passed to calc but the original values of a and b are not affected by the calc function as parameters are passed by value in Java.

Andromeda
  • 1,370
  • 2
  • 10
  • 15
  • All true, but what does it have to do with OP's question about the terms "precondition" and "postcondition"? – Ted Hopp Oct 01 '13 at 15:16
  • I assumed he was asking the wrong question. There is no if/else in his code and so no condition. – Andromeda Oct 01 '13 at 15:17
  • pre/post conditions and invariants are separate things that have no logical connection to the presence of if/else tests in code. They are more closely related to `assert` statements. – Ted Hopp Oct 01 '13 at 15:19
2

A precondition is something that has to be true about the parameters that a function takes. So it isn't enough to say what the variables are, but you need to say something about their nature. For example, a and b must be integers. A post condition states what must be true after the function completes. In your example, it would be the fact that your function must produce the sum of a and b. The precondition and post condition can actually result in two methods, especially in a language like Java. What if you had a precondition that stated simply "The two parameters must be numerical". Then you would have to account for not only integers, but floating points.

Hope that helps.

babernathy
  • 803
  • 2
  • 8
  • 23
1

Just a word of warning, casting a floating-point number (3.14) to an int is going to leave you with trouble. You might want to cast it to a float:

final float PI = 3.14f;

final means that the variable can no longer be changed.

a and b are just parameters that you pass into calc(). Before, they can be called whatever you want them to be, but inside calc() you can refer to them as a and b.

So you can have this:

int foo = 5;
int bar = 7;
int sum = calc(foo, bar); //12
Bucket
  • 7,415
  • 9
  • 35
  • 45