Essential feature: assertions

new topic     » topic index » view thread      » older message » newer message

I'm in the mood to make some confident assertions.

(There has been quite a bit of talk about Euphoria's suitability for commercial
development.)

A programming language suitable for commercial development, or ANY serious
development, MUST have assertions.

Eiffel, for example (and it is an excellent, if not especially well-known,
example) goes the distance by providing the programmer with pre-conditions,
postconditions, and a couple of flavors of invariants.

Upon entering a routine or block of code, the preconditions are those conditions
which must be true for the code to perform its intended task. These are the
conditions that must be guaranteed to exist in order for the code to function
properly (free of error).

Upon exiting a routine (block), the postconditions are those conditions that the
code guarantees to be true to the calling or succeeding code. They essentially
define what the code is supposed to have accomplished, or the state in which it
leaves the system.

Invariants are conditions that should hold true throughout the execution of a
sequence of code, or a loop. Violation of the invariants indicates that the code
has produced an unintended side effect, a potential if not actual error. The most
familiar example is probably indexing outside the bounds of an array.

Having an assertion facility of this caliber virtually (almost) enables the
programmer to state IN EXECUTABLE CODE, that enables the program to thoroughly
check itself, what the conditions are for correct execution of the code. Woohoo,
that certainly gets us a long way toward writing "commercial-quality" code,
doesn't it? (The non-use of such facilities is one of the reasons Micro$oft's
code, to pick on a company that certainly has NO EXCUSE, is chock-full of
errors.)

Now before everyone (especially Rob?) gets unduly anxious, I am not suggesting
that Euphoria provide precondition, invariant, and postcondition checking -
although it would be fantastic if it did. Right now I would be happy with a
simple assert() facility, such as any ANSI C compiler (or anything close to one)
provides.

When I peruse the Euphoria docs, I find that the list of routines jumps form
arctan to atom without a pause. Where is assert? And I am not aware of an assert
keyword, or any sort of facility that comes close to it, other than

if not myCondition then
  ? "myCondition failed"
  abort(1)
end if

- and there just has to be a better solution than that.

In C, if I recall correctly, assert is a macro that expands into both the
expression you write, and the text of the code, so that if the assertion were
something meaningful for Euphoria, such as

assert(id_vitalProc >= 0)
call_proc(id_vitalProc, vitalArgs)

the code that gets called could display a message something like "assertion
failed: 'id_vitalProc >= 0'".

Euphoria can and should implement assertions in a more elegant way than C. All
that's needed is an "assert" *keyword*, and the syntax "assert <expression>". So
one would write:

assert id_vitalProc >= 0

just as one can currently write

return id_vitalProc >= 0

One of my first thoughts was along the lines of executing a string at runtime,
with the syntax

assert("id_vitalProc >= 0")

The problem with this is that the expression needs to be evaluated in the
context of the current scope. In the various mutations of my example,
id_vitalProc would most likely NOT be a global variable visible inside some
assert() procedure. And it certainly should not have to be.

Anyway, assertions could save me and everyone else lots of grief, lots of stupid
needless program crashes and logic errors, and produce ultimately much more
error-free (or to put it another way, less error-prone, less error-infested)
code, if they were a part of the language.

(That code you think is error-free, isn't - especially if it wasn't developed
using assertions.)

George


Get your small business started at Lycos Small Business at
http://www.lycos.com/business/mail.html

____________________________________________________________
T O P I C A  -- Learn More. Surf Less. 
Newsletters, Tips and Discussions on Topics You Choose.
http://www.topica.com/partner/tag01

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu