Diagnosing Java code: assertions and temporal logic in Java programming

Source: Internet
Author: User
Tags assert

Although traditional assertions can increase the number of checks performed on Java code, there are many checks that cannot be performed with them. The remedy for this flaw is to use "temporal logic", a formal system that describes how the state of a program changes over time. In this article, Eric Allen discusses assertions, introduces temporal logic, and describes the tools used to handle temporal logic assertions in a program (the next article examines previous error patterns and temporal logic applications).

We all agree that the more we check the Java code the better, we examine the assertion's use in testing new and improved programming. Although traditional assertions can increase the number of checks performed, there are many checks that cannot be performed with them.

However, there is a way to make up for the check gap left by assertions. That is the use of temporal logic. Temporal logic is a formal system that describes how a program's state changes over time. Let's discuss assertions and their characteristics to see how the time state logic fits into the check. Then we will look at the tools used to handle temporal logic assertions.

Assertions and their attributes

In addition to type checking and unit testing, assertions provide an excellent way to determine whether a variety of features are maintained in a program.

Let's take a quick look at three types of common assertion features (although common, however, they do not provide the full range we need to compare them to the types of program attributes that can be represented in the traditional assertion language, and to examine what is necessary for multithreaded contexts, but it is not possible to represent the assertion attribute of a regular assertion. We will also provide some code examples.

Common assertion Attributes

Traditionally, the assertion attribute is divided into the following three types:

A code block asserts before it executes the condition held by the attribute.

Asserts after the code block executes the conditions held by the attribute.

The invariant assertion that the attribute holds before and after the code block executes.

As useful as these typical forms of assertions, they are less likely to have all the features we want to hold in the program. Let's take a look at the typical program features represented by assertions.

Program attributes that can be represented as assertions

This is just a short list of program attribute types that can be represented in the traditional assertion language-features that all programmers want to include in their code:

Ensure that any one-time attributes are generated only once

Assert that the document is never accessed by an unauthorized agent

Assertion provides a running opportunity to each thread

Assert that the system will never put itself into deadlock

The security protocol uses a one-time feature (a number used once) to make sure that the transaction has not been used. As a simple concept in security, it is important to ensure that once a special one-time feature is generated, it is no longer generated. Another important security assertion is that secure documents are never accessed by unauthorized agents.

In multithreaded code, we want to assert that each thread will eventually have a chance to run. We also want to make sure that the system never locks itself into a deadlock state (that is, they are waiting to provide resources to each other until two or more threads can continue processing).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.