Logout

Home Topic 4 Last Next

4.1.11

Explain the need for pre-conditions when executing an algorithm.

 

Teaching Note:

 

Sample Question:

sdfsdfsf

JSR Notes:


Intro

Firstly note that this assessment statement is one that says "explain the need", rather than give specific examples - that is the next assessment statement. Another interesting thing to note is it doesn't ask the need for post-conditions. And frankly, there's often not such a need. Post-conditions are simply a way to generally communicate what a method will do, and often that's obvious just by the name of it.


What are Pre-conditions?

Meantime, there is indeed a need for pre-conditions. Pre-conditions are conditions which must be met in order for the method to function properly. So, in terms of Java, they are the number, the type, and the range of the parameters taken in, along with any other things which would have had to be done first, like initialing an array of template objects.

 

Examples

A binarySearch( ) method requires two parameters, the item to be searched doe, and the array to search it from. But another per-condition is that the array being sent is already sorted.

Or a mathematical method may be taking in a parameter which will be used as a divisor. A per-condition for that parameter is that it is not 0.

Or a method may be intended to work with a set of data that first needs to be loaded from a file. And so the per-condition is that indeed that file has been loaded into an internal array.

And so on. You can think of some of your own pre-conditions for methods in your IA project.


Actual Programming Example

Here is a simple method, which will convert a number grade to a letter grade:

63  
64     /*Pre-conditions: a number between 0 and 100 sent.
65      
66       Post-conditions: a single letter (A to F) will be returned as a String.
67     */
68     
69     public static String conversationalist(double grade){
70         if(grade > 89){
71             return "A";
72         }
73         else if(grade > 79){
74             return "B";
75         }
76         else if(grade > 69){
77             return "C";
78         }
79         else if(grade > 59){
80             return "D";
81         }
82         else{
83             return "F";
84         }
85     }

Pre and post-conditions for Planning to Write Methods

Pr and post-conditions are useful both for you as the programmer, and the users of your method. For you, the programmer making the method, thinking about per and post-conditions helps you get ready to make your method - to get your mind around the way it will work properly.

Pre and post-conditions for Using Methods (already written)

And pre and post-conditions added as comments before your method also act as good communication to users of your method. It tells them what needs to be the state of the program before calling your method for it to work as intended. And it tells them what to expect as a result of using the method, including any possible return value.

 

Back to "the Need" of Pre-conditions in particular

But getting back to "the need" of pre-conditions, there really is a need to communicate the things that will be necessary for a given function to work. Otherwise the function may not work as intended, or indeed work at all, and as a result, the programs in which those methods is call may either not work as intended, or even freeze. So consistent use of pre-conditions results in more reliable programs.