Scala | Preconditions(assert, assume, require, ensuring)

29 Dec, 2019
Preconditions refer to some conditions in Scala that need to be fulfilled before going further with any code or program. In Scala, the process of Designing by Contract(DbC), a software designing process gives significance and meaning to Scala preconditions. The above approach was given by Betrand Mayer and it offers some post conditions and preconditions to a code block which if not satisfied, throws an exception.
Scala preconditions are a set of major functions that have different conditions a programmer must follow while designing a software. These functions come in Predef.scala package and one does not have to import any separate package.The methods are :
Assert – For general assertionsAssume – Stating an AxiomRequire – Specifically checking inputsEnsuring – is a post condition that has also been covered.
Assert – For general assertions
Assume – Stating an Axiom
Require – Specifically checking inputs
Ensuring – is a post condition that has also been covered.
AssertAssert method puts a necessity for a condition to be satisfied while performing a certain action. If this condition is satisfied, then the code works fine, otherwise it throws an exception. The exceptions are checked at run time. The assert() method is thus a way to dynamically check the invariants. The above method takes a Boolean statement as a parameter and checks throughout the program if anywhere it is violated. it throws out an exception or an assertion error.Consider the following program as a part of Driver’s license approval of application process. It produces error if the applicant age is not equal to or more than 18.// Code to check the age of the applicantval applicant_age = 16  // assert method callingassert(applicant_age>17)If the age is past the require minimum, the process is moved forward, otherwise, the program throws an Exception as below:Exception in thread "main" java.lang.AssertionError: assertion failedLooking up for the code for the same in Predef.scala, one would find a similar code like above:def assert(assertion: Boolean) {   if (!assertion)     throw new java.lang.AssertionError("assertion failed") } 
Assert method puts a necessity for a condition to be satisfied while performing a certain action. If this condition is satisfied, then the code works fine, otherwise it throws an exception. The exceptions are checked at run time. The assert() method is thus a way to dynamically check the invariants. The above method takes a Boolean statement as a parameter and checks throughout the program if anywhere it is violated. it throws out an exception or an assertion error.
Consider the following program as a part of Driver’s license approval of application process. It produces error if the applicant age is not equal to or more than 18.
// Code to check the age of the applicantval applicant_age = 16  // assert method callingassert(applicant_age>17)
If the age is past the require minimum, the process is moved forward, otherwise, the program throws an Exception as below:
Exception in thread "main" java.lang.AssertionError: assertion failed
Looking up for the code for the same in Predef.scala, one would find a similar code like above:
def assert(assertion: Boolean) {   if (!assertion)     throw new java.lang.AssertionError("assertion failed") } 
AssumeAssume method is expected to be consumed by static analysis tools. It is supposed to limit down the number of conditions that have to be checked for execution of a program. If the assume condition is violated, the checker, silently leaves the path and doesn’t allow the program to go much deeper. The assume() method has the same syntax as the previously mentioned assert(), the only difference being its execution.The following example talks about a program that executes assuming that the age of the applicant is in any case greater than or equal to 18. The main code had been avoided and only the code containing the call to assume() is incorporated.// Code to check the age of the applicantlicense_approval(17)  // Method to approve the License applicationdef license_approval(applicant_age:Int) {    assume(applicant_age>=18)//...............//............//............//The main code for checking the other details of the applicant. //...............//............//............}The assume() method gives the static checker an axiom that it could rely upon. This reduces the burden on the checker. The program runs deeper into the code if the Boolean condition of the assume() parameter is satisfied, otherwise, it throws an exception:Exception in thread "main" java.lang.AssertionError: assumption failedThe similar code can be found inside the Predef.scala package:def assume(assumption: Boolean) {   if (!assumption)     throw new java.lang.AssertionError("assumption failed") } While assert() is a method of asserting a condition in all execution paths or to check an invariant condition throughout the program, assume(), on the other hand, is used to reduce the load on the static analyser. It works locally to develop a divide-and-rule mechanism to help the analyzers to assume a condition and go through checking the code.
Assume method is expected to be consumed by static analysis tools. It is supposed to limit down the number of conditions that have to be checked for execution of a program. If the assume condition is violated, the checker, silently leaves the path and doesn’t allow the program to go much deeper. The assume() method has the same syntax as the previously mentioned assert(), the only difference being its execution.
The following example talks about a program that executes assuming that the age of the applicant is in any case greater than or equal to 18. The main code had been avoided and only the code containing the call to assume() is incorporated.
// Code to check the age of the applicantlicense_approval(17)  // Method to approve the License applicationdef license_approval(applicant_age:Int) {    assume(applicant_age>=18)//...............//............//............//The main code for checking the other details of the applicant. //...............//............//............}
The assume() method gives the static checker an axiom that it could rely upon. This reduces the burden on the checker. The program runs deeper into the code if the Boolean condition of the assume() parameter is satisfied, otherwise, it throws an exception:
Exception in thread "main" java.lang.AssertionError: assumption failed
The similar code can be found inside the Predef.scala package:
def assume(assumption: Boolean) {   if (!assumption)     throw new java.lang.AssertionError("assumption failed") } 
While assert() is a method of asserting a condition in all execution paths or to check an invariant condition throughout the program, assume(), on the other hand, is used to reduce the load on the static analyser. It works locally to develop a divide-and-rule mechanism to help the analyzers to assume a condition and go through checking the code.
Require:This method has an innovative mode of dealing with the problem. It usually blames the method caller if a certain condition is not satisfied. If the condition is satisfied, the program goes on executing, if it does not, it throws an exception. The syntax is the same as before for assert() and assume() following a Boolean condition as the parameter. The working of the require() function can be shown with an example:The following example shown a method to double any odd number. If the number passed in the function is odd, it works smoothly, if not, then an exception is thrown.// Code to double the odd numbersdef double_odd_numbers(number:Int) : Int = {      require(number%2==1) // Checking if the number is odd using assume method      number * 2;  } // Calling functiondouble_odd_numbers(13)If the condition is not satisfied, the following exception is thrown:Exception in thread "main" java.lang.IllegalArgumentException: requirement failedLooking up for the code for exception in Predef.scala, we come across the following code:def require(requirement: Boolean) {   if (!requirement)     throw new IllegalArgumentException("requirement failed") } 
This method has an innovative mode of dealing with the problem. It usually blames the method caller if a certain condition is not satisfied. If the condition is satisfied, the program goes on executing, if it does not, it throws an exception. The syntax is the same as before for assert() and assume() following a Boolean condition as the parameter. The working of the require() function can be shown with an example:The following example shown a method to double any odd number. If the number passed in the function is odd, it works smoothly, if not, then an exception is thrown.
// Code to double the odd numbersdef double_odd_numbers(number:Int) : Int = {      require(number%2==1) // Checking if the number is odd using assume method      number * 2;  } // Calling functiondouble_odd_numbers(13)
If the condition is not satisfied, the following exception is thrown:
Exception in thread "main" java.lang.IllegalArgumentException: requirement failed
Looking up for the code for exception in Predef.scala, we come across the following code:
def require(requirement: Boolean) {   if (!requirement)     throw new IllegalArgumentException("requirement failed") } 
Ensure:Ensure is a post-condition unlike the others. This method is usually applied along with the require() method to make a workable program. Considering the above example itself, we can modify the code by adding an ensure condition which requires the input number to be less than a certain limit.// Code to double the odd numbersdef double_odd_numbers(number:Int, lmt:Int) : Int = {      require(number%2==1) // Checking if the number is odd using assume method      number * 2;  } ensuring(number * 2 < lmt) // Ensuring that the number produced is less than the limit.   // Calling function// The method also requires a limit [parameter to be passed. double_odd_numbers(13, 100)The assert() method thus requires that the checker must prove the condition that follows as a parameter, assume(), on the other hand, helps checker to reduce the burden by assuming that a certain condition holds true. While, the require() method specifies the condition that has to be followed by the caller of the function.If the programmer seeks to remove the exceptions generated by the assume() and assert() methods, one can use the -Xdisable-assertions command to do so.
Ensure is a post-condition unlike the others. This method is usually applied along with the require() method to make a workable program. Considering the above example itself, we can modify the code by adding an ensure condition which requires the input number to be less than a certain limit.
// Code to double the odd numbersdef double_odd_numbers(number:Int, lmt:Int) : Int = {      require(number%2==1) // Checking if the number is odd using assume method      number * 2;  } ensuring(number * 2 < lmt) // Ensuring that the number produced is less than the limit.   // Calling function// The method also requires a limit [parameter to be passed. double_odd_numbers(13, 100)
The assert() method thus requires that the checker must prove the condition that follows as a parameter, assume(), on the other hand, helps checker to reduce the burden by assuming that a certain condition holds true. While, the require() method specifies the condition that has to be followed by the caller of the function.
If the programmer seeks to remove the exceptions generated by the assume() and assert() methods, one can use the -Xdisable-assertions command to do so.
Picked
Scala
scala-exception
Scala
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inheritance in Scala
How to Install Scala with VSCode?
Scala | Option
Hello World in Scala
Scala ListBuffer
Scala | Traits
Introduction to Scala
Implicit Parameters In Scala
Scala | Functions - Basics