PL/SQL Practicum #2: Assertions, Exceptions and Module Stability



Similar documents

Handling PL/SQL Errors

Oracle PL/SQL Best Practices

Handling PL/SQL Errors

Oracle For Beginners Page : 1

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Quotes from Object-Oriented Software Construction

Software Engineering Techniques

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

Error Management in Oracle PL/SQL

Handling Exceptions. Schedule: Timing Topic 45 minutes Lecture 20 minutes Practice 65 minutes Total

Handling Exceptions. Copyright 2008, Oracle. All rights reserved.

Handling Exceptions. Schedule: Timing Topic. 45 minutes Lecture 20 minutes Practice 65 minutes Total

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Outline. 1 Denitions. 2 Principles. 4 Implementation and Evaluation. 5 Debugging. 6 References

Making the Most of Oracle PL/SQL Error Management Features

STUDY OF PL/SQL BLOCK AIM: To Study about PL/SQL block.

Bullet Proof Your PL/SQL

22C:22 (CS:2820) Object-Oriented Software Development

PL / SQL Basics. Chapter 3

OPP ODTUG Kaleidoscope. An ODTUG SP* Oracle PL/SQL Programming Conference. WOW-Wide Open World, Wide Open Web!

Darshan Institute of Engineering & Technology PL_SQL

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

Software Component Specification Using Design by Contract

Software Construction

Persistent Stored Modules (Stored Procedures) : PSM

How To Write A Test Engine For A Microsoft Microsoft Web Browser (Php) For A Web Browser For A Non-Procedural Reason)

Answers to the Try It Yourself Sections

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Oracle 11g PL/SQL training

Database Programming with PL/SQL: Learning Objectives

Oracle PL/SQL Programming

Specification and Analysis of Contracts Lecture 1 Introduction

An Introduction to PL/SQL. Mehdi Azarmi

Adapting C++ Exception Handling to an Extended COM Exception Model

Writing Control Structures

02-201: Programming for Scientists

Programming Database lectures for mathema

Manufacturing View. User View. Product View. User View Models. Product View Models

Oracle PL/SQL Language. CIS 331: Introduction to Database Systems

When an exception occur a message which explains its cause is received. PL/SQL Exception message consists of three parts.

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

Chapter 1: Key Concepts of Programming and Software Engineering

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

Software Quality. Software Quality Assurance and Software Reuse. Three Important Points. Quality Factors

1 Stored Procedures in PL/SQL 2 PL/SQL. 2.1 Variables. 2.2 PL/SQL Program Blocks

Some quick definitions regarding the Tablet states in this state machine:

CS 241 Data Organization Coding Standards

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

SQL/PSM. Outline. Database Application Development Oracle PL/SQL. Why Stored Procedures? Stored Procedures PL/SQL. Embedded SQL Dynamic SQL

VB.NET Programming Fundamentals

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

Design by Contract beyond class modelling

PL/SQL (Cont d) Let s start with the mail_order database, shown here:

Two Flavors in Automated Software Repair: Rigid Repair and Plastic Repair

Computer Programming I

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Maintaining Stored Procedures in Database Application

Optimizing with Open Source Technology Postgres

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Loop Invariants and Binary Search

Chapter 24 - Quality Management. Lecture 1. Chapter 24 Quality management

The ConTract Model. Helmut Wächter, Andreas Reuter. November 9, 1999

Bull s Database Migration Business Unit Oracle PL/SQL to PostgreSQL PL/pgSQL Translation Technical Notes

Oracle Database: Develop PL/SQL Program Units

SQL NULL s, Constraints, Triggers

Oracle 10g PL/SQL Training

C H A P T E R Condition Handling

Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

Testing and Inspecting to Ensure High Quality

The Essentials of Finding the Distinct, Unique, and Duplicate Values in Your Data

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Software Testing & Analysis (F22ST3): Static Analysis Techniques 2. Andrew Ireland

Chapter 4: Design Principles I: Correctness and Robustness

Costing In Oracle HRMS CONCEPTS

Virtual Private Database Features in Oracle 10g.

Programming by Contract vs. Defensive Programming: A Comparison of Run-time Performance and Complexity

sqlite driver manual

SQL Server Database Coding Standards and Guidelines

On the Relation between Design Contracts and Errors: A Software Development Strategy

Introduction to Software Engineering. 8. Software Quality

Oracle Database: SQL and PL/SQL Fundamentals

Object Oriented Software Design II

ARIZONA CTE CAREER PREPARATION STANDARDS & MEASUREMENT CRITERIA SOFTWARE DEVELOPMENT,

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Lecture Notes on Linear Search

BUSINESS RULES CONCEPTS... 2 BUSINESS RULE ENGINE ARCHITECTURE By using the RETE Algorithm Benefits of RETE Algorithm...

A Framework for the Semantics of Behavioral Contracts

Writing in the Computer Science Major

Defense In-Depth to Achieve Unbreakable Database Security

Oracle PL/SQL Injection

No no-argument constructor. No default constructor found

Unit testing using Python nose

Transcription:

PL/SQL Practicum #2: Assertions, Exceptions and Module Stability John Beresniewicz Technology Manager Precise Software Solutions

Agenda Design by Contract Assertions Exceptions Modular Code

DESIGN BY CONTRACT A software engineering discipline for building reliable systems

Design by Contract Design by Contract is a powerful metaphor that... makes it possible to design software systems of much higher reliability than ever before; the key is understanding that reliability problems (more commonly known as bugs) largely occur at module boundaries, and most often result from inconsistencies in both sides expectations. Bertrand Meyer, Object Success

Design by Contract Software modules have client-supplier relationships Client requests and supplier responds These relationships can be expressed as contracts between client and supplier Formalizing and enforcing module contracts promotes software reliability

Contract elements PRECONDITIONS What will be true when module is entered? Caller s obligation and module s benefit POSTCONDITIONS What will be true when module completes? Module s obligation and caller s benefit INVARIANTS Anything that should not change as a result of module execution

Design by Contract and code stability TRUST Preconditions allow modules to trust their input data Postconditions allow clients to trust module output CORRECTNESS Explicit contracts require careful consideration Trusted data + correct algorithms = solid code SAFETY Invariant preservation minimizes risk to other modules

PL/SQL Call Structure PROCEDURE foo IS... IF pkg.fcn(p1var) THEN...END IF; p1var = 1234 (pre) TRUE or FALSE (post) The contract PACKAGE pkg FUNCTION fcn (p1_in IN INTEGER) RETURN BOOLEAN

PL/SQL and Design by Contract Design by Contract = formalizing interfaces Preconditions are obligations of calling module Postconditions are obligations of called module Invariants are preserved system states Module IN parm values must obey preconditions Module OUT parm and function RETURN values must satisfy postconditions Implemented by module logic Exception handling state = invariant violation

ASSERTIONS Enforcing contracts programmatically

PL/SQL assertions Test a boolean condition and complain if not TRUE What does complain mean? PL/SQL assertions implemented as a procedure Always executed, unlike some language environments PROCEDURE Assert (cond_in IN BOOLEAN); Assert(parm1 BETWEEN 0 AND 100); Assert(plsqltbl.COUNT > 0); Assert(vbl2 IS NOT NULL); Assert(fcnX > constanty);

Simplest assert procedure PROCEDURE assert (cond_in BOOLEAN) IS IF NOT NVL(cond_IN,FALSE) THEN RAISE ASSERTFAIL; END IF; END assert; Complain = raise assertfail exception System state change: exception handling NULL tests FALSE and raises the exception

Assert contract preconditions Module calls have contract obligations Module parameters implement the contract IN parameter values must obey preconditions OUT and RETURN values must obey postconditions Assert preconditions at module entry points Enforces one side of all contracts Increased probability all contracts obeyed equates to increased code stability

Packaging assertions I PACKAGE foo IS ASSERTFAIL EXCEPTION; PROCEDURE proc1 (p1 integer); END foo; PACKAGE BODY foo IS PROCEDURE proc1 (p1 integer) IS assert(p1 < 100); -- precondition /* proc1 code */ END proc1; Standard local assertion module in each package reduces coupling

Callers can program defensively -- other code callme(p1val); EXCEPTION WHEN ASSERTFAIL THEN apologize_for_p1val; END; -- more code Assert does not externalize error, catching scope decides what to do

Performance considerations Each call to assert is additional overhead BUT...assert is package local and code minimal Assertion mechanism cannot be turned on/off Differences of opinion exist on turning off assertion checks Modules called very frequently may need attention Invariant within large loops

Turning off assertions Simply comment them out but leave in code They are part of module s specification Only suppress for production performance issue FUNCTION calledoften (p1 varchar2, p2 integer) RETURN BOOLEAN IS -- assert(length(p1) BETWEEN 10 AND 100); -- assert(bitand(p2,3) = 3); /* code for module... */ END calledoften;

EXCEPTIONS Dealing with problems systematically

Exception fundamentals Something undesirable or unexpected happens We call that something an EXCEPTION Either Oracle or application may signal exception Processing jumps from execution block to exception block If no exception block, exit to caller s exception block... Declaration exceptions exit to caller Not good, a local problem that cannot be dealt with locally

Exception classes and treatments Anticipated, recoverable and false alarms Preserve normal program flow using sub-blocks Anticipated, unrecoverable Contract violations (Assertfail exceptions) Fix modules to obey contracts Unanticipated, uncatchable Declaration exceptions Unanticipated, catchable Clean up, log error and fail out for analysis DO NOT catch and continue (unless mandatory)

Nesting, program flow, exceptions END; EXCEPTION END; END; EXCEPTION END; Exception out Exception handled Exception out Exception handled Normal exit Use nesting to continue normal program flow

Catching an exception on purpose FUNCTION IsNumber (txt_in IN varchar) RETURN BOOLEAN IS test NUMBER; test := TO_NUMBER(txt_IN); EXCEPTION WHEN VALUE_ERROR THEN null; END; RETURN (test IS NOT NULL); END IsNumber; The exception (or not) provides the essential information

Let s clean that function up some... FUNCTION IsNumber (txt_in IN varchar) RETURN BOOLEAN IS test NUMBER; myboolreturn BOOLEAN := FALSE; test := TO_NUMBER(txt_IN); myboolreturn := TRUE; EXCEPTION WHEN VALUE_ERROR THEN myboolreturn := FALSE; END; RETURN myboolreturn; END IsNumber;

Best Practice: smart scoping WHEN an Oracle exception can be anticipated in a section of code, AND that exception can be safely handled, THEN enclose the code in a sub-block and handle the exception (and only that exception)

Declaration exception PROCEDURE notsogood IS codevar CHAR(1) := TOO LONG ; RAISE VALUE_ERROR; EXCEPTION WHEN OTHERS THEN null; END notsogood; ORA-06502: PL/SQL: numeric or value error Easily preventable by code inspection.

Declaration time bomb PROCEDURE notmuchbetter IS mycode codes.code%type := SIZE? ; RAISE VALUE_ERROR; EXCEPTION WHEN OTHERS THEN null; END notmuchbetter; ORA-06502: PL/SQL: numeric or value error Code may break due to change in codes.code datatype.

Declaration mystery error PROCEDURE reallybad IS localvar integer := somefcn; RAISE VALUE_ERROR; EXCEPTION WHEN OTHERS THEN null; END reallybad; ORA-06502: PL/SQL: numeric or value error Where is the exception generated?

Best Practice: declare safely Initialize declarations with safe assignments only Remembering that safe today may not be safe tomorrow DO NOT use functions to initialize declarations Unless the functions are absolutely trusted PROCEDURE willnotfail IS localvar INTEGER; localvar := initfunction; EXCEPTION WHEN OTHERS THEN null; END willnotfail;

Worst practice: catch and ignore FUNCTION badfcn(p1_in integer) RETURN BOOLEAN IS /* some code */ EXCEPTION WHEN OTHERS THEN RETURN null; END badfcn; Masks out ALL errors: callers will think all is fine when something really bad may have happened Returns NULL for BOOLEAN, losing opportunity to escape problematic three-valued logic of SQL

Catch, cleanup and RAISE EXCEPTION WHEN OTHERS THEN log_error(sqlcode); /* local clean up (e.g.close cursors) */ RAISE; Serious errors should be logged for analysis Clean up any resources that persist beyond call Re-raise exception to pass on to caller Dead programs tell no lies

Who should catch exceptions?? EXCEPTION END; call A nontrivial question with many possible answers.? EXCEPTION END; call? EXCEPTION END; ORA-345

MODULAR CODE Assembling systems from stable components

Why should we modularize? Increased contract enforcement More interfaces, more asserts, more problems caught Code normalization and reuse Do things correctly in one place (implement once, call many) Smaller, tighter source code units promote correctness Better algorithm inspection (especially by others)

Increased contract enforcement Assert Assert Assert Assert Assert Assert

Where should we modularize? At the system level: Divide functionality into logical components Organize components hierarchically Around data: Encapsulate (table) data access and transactions Shared abstract data types Within modules: Private package modules to encapsulate shared functions Private modules within procedures and functions Basically everywhere and as much as possible!

Stable, compact module FUNCTION isweekend(loc_in IN varchar2,date_in IN date) RETURN BOOLEAN IS tmp_dy integer; assert(loc_in IN ( US, IL )); assert(date_in IS NOT NULL); tmp_dy := TO_CHAR(date_IN, D ); -- problem? CASE loc_in WHEN US THEN RETURN (tmp_dy IN (7,1)); WHEN IL THEN RETURN (tmp_dy IN (6,7)); END CASE; END isweekend; Boolean function determines if date is weekend Weekend depends on location

isweekend contract elements PRECONDITIONS Date_IN not null Loc_IN not null Loc_IN either US or IL POSTCONDITIONS RETURN TRUE if date_in is weekend for loc_in, FALSE otherwise POTENTIAL PROBLEM? Do we REALLY know how TO_CHAR works (given NLS options)? We could introduce a new precondition: -- September 2,2001 is Sunday assert(1 = TO_CHAR(TO_DATE( 09:02:2001, MM:DD:YYYY ), D ) );

Potential problem confirmed The date format element D returns the number of the day of the week (1-7). The day of the week that is numbered 1 is specified implicitly by the initialization parameter NLS_TERRITORY. Oracle8i SQL Reference

Summary Points Use standardized assertions Enforce preconditions in all modules Code clearly and carefully Postconditions depend on proper algorithms Use code inspection Clear, documented logic promotes accuracy Modularize More modules = more contracts Small execution sections promote better inspections Eliminate exceptions Assert, anticipate, avoid invariant violations

Resources Object-oriented Software Construction, 2 nd Edition by Bertrand Meyer (Prentice-Hall, 2001) Object Success by Bertrand Meyer (out of print) The Pragmatic Programmer by Andrew Hunt, et al (Addison-Wesley, 1999) PL/SQL Best Practices by Steven Feuerstein (O Reilly & Associates, 2001)