Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
As you know, an assertion is a statement evaluating to a boolean (ie. true/false) value; Assertion predicates are typically used to check the state of code blocks at entry (precondition), during execution (invariants) and at exit (postcondition), thus verifying runtime compliance with a specified design contract (pre/post) and with assumptions of program state (invariants) for the implementation of an algorithm.

Typically an assertion failure will throw an exception with an error message and a context/stack trace, thus facilitating debugging and error fixing. In Perl we have several mechanisms for implementing assertions, ranging from the simple and general <predicate> or die "Can't ...: $!"; over the eval { <predicate>; }; die $@ if $@; to the use of specialized Smart comments (require, assert, ensure, insist etc.), Test predicates (ok, is, isnt, like, unlike, cmp_ok, can_ok etc.), -- and a range of other CPAN Assert modules.

Programming with assertions for debug scaffolding and unit testing has long been considered a Good Thing for ensuring compliance of program runtime behaviour viz application design and specs; (-- for an interesting historical record, see: Tony Hoare Assertions: a personal perspective).

This is basically how i have been using assertions in my own programming in the past, -- though not as rigoristic as promoted by the Design by contract methodology.Now I'm interested in hearing to what extent and with what means you fellow monks are using assertions in your Perl programming?

My renewed interest in the use of assertions was sparked by a recent article in DDJ CodeTalk, where Walter Bright raised the question of using 'resilient' assertions in high reliability production code, -- rightly questioned, I think, by the commenters of the article, -- but food for thought.Any comments on this approach, based on your own experiences?

Best regards
Allan Dystrup

update
In the above Assertions... reference Tony Hoare writes :

The primary role of an assertion today is as a test oracle, defining the circumstances under which a program under test is considered to fail. ... many teams leave the assertions in ship code to generate an exception when false; to continue execution in such an unexpected and untested circumstance would run a grave risk of crash. So instead, the handler for the exception makes a recovery that is sensible to the customer in the environment of use.

That nicely wraps up (I think) the discussion of what i've called 'resilient' assertions in production code (cf. DDJ CodeTalk); What's perhaps more interesting in Hoare's paper is his aim of using assertions to express program intention/semantics thus allowing automatic verification/proof. As he writes :

...the axiomatic method has been explicitly used to guide the design of languages like Euclid and Eiffel 20, 21. These languages were prepared to accept the restrictions on the generality of expression that are necessary to make the axioms consistent with efficient program execution.
...there are now strong signs that the actual quality of the language is beginning to matter. ...One day, I expect the new programming language designer will learn how to use assertional methods as a design tool, to evaluate and refine the objectively evaluated quality of familiar language features, as well as averting the risks involved in the introduction of new features.


I was wondering which (if any) effect this kind of conderation has (had) on the development of Perl6, -- taking into account the special nature of dynamic languages and the explicit design goal of flexibility/TimToadyNess of Perl ?

In reply to Your use of assertions in Perl ? by ady

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-19 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found