Q&A on Java assertions.


Subject: Q&A on Java assertions.
From: Adam Krolnik (krolnik@lsil.com)
Date: Thu Apr 18 2002 - 08:12:44 PDT


Good morning all;

I found this page about Java assertions. They had some interesting
comments about why the designed the construct as they did:

http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html

In particular I found these comments interesting (about use of the if()
statement):

1.Why provide an assertion facility, given that one can program
assertions atop the Java programming language
     with no special support?

     Although ad hoc implementations are possible, they are of necessity
either ugly (requiring an if statement for each assertion)
     or inefficient (evaluating the condition even if assertions are
disabled). Further, each ad hoc implementation has its own
     means of enabling and disabling assertions, which lessens the
utility of these implementations, especially for debugging in the
     field. As a result of these shortcomings, assertions have never
become a part of the culture among engineers using the Java
     programming language. Adding assertion support to the platform
stands a good chance of rectifying this situation.

       
   2.Why does this facility justify a language change, as opposed to a
library solution?

     We recognize that a language change is a serious effort, not to be
undertaken lightly. The library approach was considered. It
     was, however, deemed essential that the runtime cost of assertions
be negligible if they are disabled. In order to achieve this
     with a library, the programmer is forced to hard-code each
assertion as an if statement. Many programmers would not do
     this. Either they would omit the if statement and performance would
suffer, or they would ignore the facility entirely. Note also
     that assertions were contained in James Gosling's original
specification for the Java programming language. Assertions were
     removed from the Oak specification because time constraints
prevented a satisfactory design and implementation.

  Adam Krolnik
  Verification Mgr.
  LSI Logic Corp.
  Plano TX. 75074



This archive was generated by hypermail 2b28 : Thu Apr 18 2002 - 08:14:11 PDT