SlideShare ist ein Scribd-Unternehmen logo
1 von 42
Downloaden Sie, um offline zu lesen
<Insert Picture Here> 
Contract-oriented PL/SQL programming 
John Beresniewicz 
Technical staff, Server Technologies 
Oracle USA
The following is intended to outline our general 
product direction. It is intended for information 
purposes only, and may not be incorporated into 
any contract. It is not a commitment to deliver any 
material, code, or functionality, and should not be 
relied upon in making purchasing decisions. 
The development, release, and timing of any 
features or functionality described for Oracle’s 
products remains at the sole discretion of Oracle.
<Insert Picture Here> 
Agenda 
• Theory 
• Design by Contract 
• Technique 
• Standardized assertions 
• Rigorous precondition checking 
• Function-oriented programming 
• Practice 
• Useful contract patterns 
• Example: prime number package
<Insert Picture Here> 
Theory 
Design by contract
References 
• Object-oriented Software Construction, 2nd Edition 
• Bertrand Meyer. Prentice Hall PTR 1997. 
• The Pragmatic Programmer 
• Andrew Hunt, Dave Thomas. Addison Wesley Longman, Inc 
2000. 
• Design by Contract, by Example 
• Richard Mitchell, Jim McKim. Addison-Wesley 2002. 
• Design Patterns and Contracts 
• Jean-Marc Jezequel, Michel Train, Christine Mingins. Addison 
Wesley Longman 2000.
<Insert Picture Here> 
Bertrand Meyer 
Object Success 
“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.”
Contracts 
• Legal contracts govern interactions between parties 
• Each party has obligations and expects benefits 
under the contract 
• Each party typically benefits from the other’s 
obligations, and vice versa
Software contracts 
• Software modules have interactions as calling and 
called program 
• Client-supplier relationship between modules 
• Interaction boundary also known as the API 
• We can describe these interactions as conforming to 
contracts 
• Formalizing and enforcing software contracts 
promotes reliability and maintainability
Contract elements 
• PRECONDITIONS 
• What will be true when module is entered? 
• Caller obligation and supplier benefit 
• POSTCONDITIONS 
• What will be true when module completes? 
• Supplier obligation and caller benefit 
• INVARIANTS 
• Any state that will not be changed as a result of module 
execution
Contracting benefits 
• Preconditions allow suppliers to trust data in 
• Postconditions allow callers to trust data out 
• Rigid postcondition obligations promote stricter 
module scoping 
• Better functional decomposition 
• Bugs are detected early and isolated easily 
• Trusted data + correct algorithms = bug-free code
Software defects (bugs) 
• Contract violations are BUGS…always 
• Precondition violations are caller bugs 
• Postcondition violations are supplier bugs
<Insert Picture Here> 
Bertrand Meyer 
Object-oriented Software 
Construction 
“Good contracts are those which exactly specify the rights 
and obligations of each party...In software design, where 
correctness and robustness are so important, we need to 
spell out the terms of the contracts as a prerequisite to 
enforcing them. Assertions provide the means to state 
precisely what is expected from and guaranteed to each 
side in these arrangements.”
Assertions: enforcing contracts 
• Test a contract condition and complain if not met 
• Contract condition is expressed as a BOOLEAN 
• Contract violated when expression is FALSE 
• Complaint is to raise a well-known EXCEPTION 
• Implement in PL/SQL as a simple procedure
Benefits of assertions 
• Help to write correct software 
• Documentation aid 
• Support for testing, debugging and QA 
• Support for software fault tolerance 
Source: Object-oriented Software Construction
PL/SQL assert 
PROCEDURE assert (condition_in IN BOOLEAN) 
IS 
BEGIN 
if NOT NVL(condition_in,FALSE) 
then 
raise ASSERTFAIL; 
end if; 
END assert; 
• Exits silently or raises ASSERTFAIL 
• Null input throws ASSERTFAIL
Asserting contract elements 
FUNCTION distance 
(x_in in number, y_in in number) RETURN number 
IS 
l_return number := null; 
BEGIN 
assert_pre(x_in not null,’x_in not null’); 
assert_pre(y_in not null,’y_in not null’); 
assert_pre(y_in > 0,’y_in >0’); 
l_return := ABS(x_in – y_in); 
assert_post(l_return >=0,’return>=0’); 
RETURN l_return; 
END distance;
<Insert Picture Here> 
Technique 
Minimum-optimum
Technique 
• Standardize 
• Standard private local assert procedure in all packages 
• Standard common exception ASSERTFAIL in all packages 
• Enforce preconditions aggressively 
• Crash on ASSERTFAIL 
• Modularize 
• Each module implements a distinct function 
• Package implements a coherent set of modules
Standard declarations 
-- standard package public assertion declarations 
ASSERTFAIL EXCEPTION; 
PRAGMA EXCEPTION_INIT(ASSERTFAIL, -20999); 
-- standard package private declarations 
ASSERTFAIL_C CONSTANT INTEGER := -20999; 
PKGNAME_C CONSTANT VARCHAR2(20) := 'Primes‘ 
• All packages declare private version of common 
exception ASSERTFAIL 
• Externally identical but internally each can be distinguished 
• Reserve an exception number for global convention
Original standard assert (ca 2001) 
-- standard local packaged assertion procedure 
PROCEDURE assert (bool_IN IN BOOLEAN 
,msg_IN IN VARCHAR2 := null) 
IS 
BEGIN 
IF NOT NVL(bool_IN,FALSE) -- assertfail on null 
THEN 
RAISE_APPLICATION_ERROR 
( ASSERTFAIL_C, 'ASSERTFAIL:'||PKGNAME_C|| 
':'||SUBSTR(NVL(msg_IN,'nomsg'),1,200) ); 
END IF; 
END assert; 
• This procedure identical and private to all packages
Proposed asserts (2007) 
PROCEDURE assert_pre 
(condition_in IN BOOLEAN 
,progname_in IN progname_t 
,msg_in IN varchar2) 
IS 
BEGIN 
assert(condition_in 
,progname_in||':PRE:'||msg_in); 
END assert_pre; 
• We similarly define assert_post
Aggressively assert preconditions 
• A contract enforced on one side only is still a contract 
enforced 
• Think of it as tests built into code and checked at runtime 
• Decomposition increases stability through 
multiplication of contracts 
• Thus an incentive to fine-grain modularization 
• Enables trusted software layer to grow up through the 
hierarchy
Modularize ruthlessly and coherently 
• Hierarchical layers of packages and modules 
• Top layer: outward facing 
• Accept user input 
• Present high-level client interfaces and errors 
• Middle layers: complex application logic 
• Bottom layer: trusted base components 
• Ruthless precondition testing
Assertion-hardened code 
Assert 
Assert 
• Modules organized hierarchically 
• Each module asserts preconditions 
• More modules 
• => more contracts 
Assert 
• => more stable code 
Assert Assert Assert
Crash on ASSERTFAIL 
• It is a bug and must become known 
• Catch ASSERTFAIL only for good reason: 
• Testing 
• Refactoring 
• Making a contract less demanding 
• Externalizing a local check function 
• Dead Programs Tell No Lies 
• Hunt and Thomas, The Pragmatic Programmer
Testing 
• Testing takes client (calling program) perspective 
• Whereas coding takes called program perspective 
• Regression test postconditions 
• Issue: only externally facing modules can be externally tested 
• Unit test contract preconditions against many inputs 
assert(0=computefunction(0.2,100),’test 1001’);
Performance considerations 
• Estimates for contracting overhead (from Meyer): 
• 50% for precondition checking 
• 100-200% for postcondition and invariant checking 
• Minimum-optimum principle favors precondition enforcement 
• Oracle 10g conditional compilation enables assertions 
to be compiled into or out of pl/sql programs 
• CAR Hoare quote
<Insert Picture Here> 
C.A.R. Hoare 
“The current practice of compiling subscript range checks 
into the machine code while a program is being tested, then 
suppressing the checks during production runs, is like a 
sailor who wears his life preserver while training on land but 
leaves it behind when he sails!” 
http://whats.all.this.brouhaha.com/?page_id=479
10g Conditional assertions 
PROCEDURE trusted (p1_in in number) 
IS 
BEGIN 
$IF $$ASSERTPRE 
$THEN 
assert(p1_in > 0); 
$END 
/* do trusted logic */ 
END trusted; 
SQL> alter session set 
PLSQL_CCFLAGS=’ASSERTPRE:TRUE; 
SQL> alter procedure trusted compile; 
• After recompiling assert will be included 
• Recommend restricted rather than global application
BOOLEAN functions in contracts 
FUNCTION ready_to_process(ID_in) return BOOLEAN; 
PROCEDURE process_id(ID_in) IS 
BEGIN 
assert(ready_to_process(ID_in)); --pre 
/* do the processing */ 
END; 
• This can be very powerful, and potentially very 
dangerous 
• When is it OK to assert functions as preconditions?
<Insert Picture Here> 
Bertrand Meyer 
Object-oriented Software 
Construction 
“If you exert the proper care by sticking to functions that are 
simple and self-evidently correct, the use of function 
routines in assertions can provide you with a powerful 
means of abstraction.”
Check functions 
FUNCTION check_all_ok return BOOLEAN is 
BEGIN 
assert(condition1,…); 
assert(condition2,…); 
… 
assert(conditionN,…); 
return TRUE; 
END check_all_ok; 
• Compound set of assertions 
• Self-evidently correct - TRUE or ASSERTFAIL 
• Useful for compressing shared sets of preconditions 
assert(check_all_ok,module_name||’:check_all_ok’);
<Insert Picture Here> 
Practice 
Using contracts in programs
Useful contract patterns 
• NOT NULL IN 
• Near-universal precondition 
• RETURN NOT NULL 
• Very attractive postcondition 
• RETURN BOOLEAN NOT NULL 
• Direct function references in conditional code 
• RETURN BOOLEAN TRUE or ASSERTFAIL 
• Check functions
PL/QL Best Practices, 2nd Edition 
FUNCTION excuse_in_use (excuse_in IN excuse_excuse_t) 
RETURN BOOLEAN 
IS 
c_progname CONSTANT progname_t:='EXCUSE_IN_USE'; 
l_return BOOLEAN; 
BEGIN 
-- check caller obligations 
assert_pre(condition_in => excuse_in is not null 
,progname_in => c_progname 
,msg_in => 'excuse_in not null'); 
-- compute return value 
l_return := g_excuses_used.EXISTS(excuse_in); 
-- check return obligations 
assert_post(condition_in => l_return is not null 
,progname_in => c_progname 
,msg_in => 'l_return not null'); 
RETURN l_return; 
END excuse_in_use;
Bugs find you… 
SQL> l 
1 begin 
2 if excuse_tracker.excuse_in_use('lame excuse') 
3 then 
4 dbms_output.put_line('lame excuse in use'); 
5 elsif 
6 excuse_tracker.excuse_in_use(NULL) 
7 then 
8 dbms_output.put_line('NULL is no excuse!'); 
9 end if; 
10* end; 
SQL> / 
begin 
* 
ERROR at line 1: 
ORA-20999: ASSERTFAIL:EXCUSE_TRACKER:EXCUSE_IN_USE:PRE:excuse_in not null 
ORA-06512: at "SYS.EXCUSE_TRACKER", line 62 
ORA-06512: at "SYS.EXCUSE_TRACKER", line 85 
ORA-06512: at "SYS.EXCUSE_TRACKER", line 30 
ORA-06512: at line 6
Primes package: factoring by division 
FUNCTION factor(num_IN INTEGER, divisor_IN INTEGER) 
RETURN INTEGER 
IS 
tmp_power integer; tmp_num integer; 
BEGIN 
assert(num_IN IS NOT NULL,'factor:num_IN not null'); 
assert(divisor_IN IS NOT NULL,'factor:divisor_IN not null'); 
assert(divisor_IN >1 AND num_IN >=divisor_IN 
,'factor:1<divisor<=num_IN'); 
tmp_num := num_IN; tmp_power := 0; -- (1) initialize 
WHILE MOD(tmp_num,divisor_IN) = 0 -- still divides evenly 
LOOP 
tmp_power := tmp_power + 1; 
tmp_num := tmp_num / divisor_IN; 
END LOOP; 
RETURN tmp_power; 
END factor;
Concluding remarks 
• Design by Contract principles are powerful constructs 
for producing correct software 
• Using standardized package assertions we can rigidly 
enforce contract preconditions in PL/SQL 
• Native support for DbC in PL/SQL highly desirable 
• Standardized ASSERTFAIL exception 
• Precondition, postcondition, and invariant assertion syntax 
• Produce source line and offending condition automatically
10g goodies 
• Conditional compilation: PLSQL_CCFLAGS 
• Compile assertions into or out of code 
• Inquiry directives 
• $$PLSQL_UNIT 
• Replace constant literal PKGNAME_C 
• $$PLSQL_LINE 
• Identify line of code throwing ASSERTFAIL 
• DBMS_PREPROCESSOR 
• Surface the line of code throwing ASSERTFAIL
dbc10g.sql – 10g assert experiments 
-- new 10g signature 
PROCEDURE assert (line_no_IN IN PLS_INTEGER 
,bool_expr_IN IN BOOLEAN 
,msg_IN IN VARCHAR2 := null); 
-- simple test procedure 
PROCEDURE p1 
is 
begin 
assert($$PLSQL_LINE -- assert comment on first line 
,25 < to_number('24') -- assert boolean expr 
,to_char(SYSDATE,'YYYY:MM:DD:HH24:MI:SS')); --msg 
end p1;
10g assert: debug info in ASSERTFAIL 
BEGIN dbc10g.p1; END; 
* 
ERROR at line 1: 
ORA-20999: ASSERTFAIL:PKGBDY:DBC10G.18: assert( 18 -- assert 
comment on first line 
,25 < to_number('24') -- assert boolean expr 
:2008:10:10:22:06:53 
ORA-06512: at "SYS.DBC10G", line 75 
ORA-06512: at "SYS.DBC10G", line 18 
ORA-06512: at line 1
Contract-oriented PLSQL Programming

Weitere ähnliche Inhalte

Was ist angesagt?

Oracle performance tuning_sfsf
Oracle performance tuning_sfsfOracle performance tuning_sfsf
Oracle performance tuning_sfsf
Mao Geng
 
OOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AASOOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AAS
Kyle Hailey
 
Why & how to optimize sql server for performance from design to query
Why & how to optimize sql server for performance from design to queryWhy & how to optimize sql server for performance from design to query
Why & how to optimize sql server for performance from design to query
Antonios Chatzipavlis
 
OGG Architecture Performance
OGG Architecture PerformanceOGG Architecture Performance
OGG Architecture Performance
Enkitec
 

Was ist angesagt? (20)

Oracle performance tuning_sfsf
Oracle performance tuning_sfsfOracle performance tuning_sfsf
Oracle performance tuning_sfsf
 
Aioug vizag oracle12c_new_features
Aioug vizag oracle12c_new_featuresAioug vizag oracle12c_new_features
Aioug vizag oracle12c_new_features
 
Crack the complexity of oracle applications r12 workload v2
Crack the complexity of oracle applications r12 workload v2Crack the complexity of oracle applications r12 workload v2
Crack the complexity of oracle applications r12 workload v2
 
Oracle Performance Tools of the Trade
Oracle Performance Tools of the TradeOracle Performance Tools of the Trade
Oracle Performance Tools of the Trade
 
Ten query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should knowTen query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should know
 
Best practices for_large_oracle_apps_r12_implementations
Best practices for_large_oracle_apps_r12_implementationsBest practices for_large_oracle_apps_r12_implementations
Best practices for_large_oracle_apps_r12_implementations
 
EM12c: Capacity Planning with OEM Metrics
EM12c: Capacity Planning with OEM MetricsEM12c: Capacity Planning with OEM Metrics
EM12c: Capacity Planning with OEM Metrics
 
Awr1page OTW2018
Awr1page OTW2018Awr1page OTW2018
Awr1page OTW2018
 
OOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AASOOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AAS
 
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
 
Analyzing and Interpreting AWR
Analyzing and Interpreting AWRAnalyzing and Interpreting AWR
Analyzing and Interpreting AWR
 
How to find what is making your Oracle database slow
How to find what is making your Oracle database slowHow to find what is making your Oracle database slow
How to find what is making your Oracle database slow
 
Database and application performance vivek sharma
Database and application performance vivek sharmaDatabase and application performance vivek sharma
Database and application performance vivek sharma
 
Awr1page - Sanity checking time instrumentation in AWR reports
Awr1page - Sanity checking time instrumentation in AWR reportsAwr1page - Sanity checking time instrumentation in AWR reports
Awr1page - Sanity checking time instrumentation in AWR reports
 
Performance Tuning intro
Performance Tuning introPerformance Tuning intro
Performance Tuning intro
 
Why & how to optimize sql server for performance from design to query
Why & how to optimize sql server for performance from design to queryWhy & how to optimize sql server for performance from design to query
Why & how to optimize sql server for performance from design to query
 
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
 
Performance tuning in sql server
Performance tuning in sql serverPerformance tuning in sql server
Performance tuning in sql server
 
Oracle ebs capacity_analysisusingstatisticalmethods
Oracle ebs capacity_analysisusingstatisticalmethodsOracle ebs capacity_analysisusingstatisticalmethods
Oracle ebs capacity_analysisusingstatisticalmethods
 
OGG Architecture Performance
OGG Architecture PerformanceOGG Architecture Performance
OGG Architecture Performance
 

Andere mochten auch

PL/SQL Code for Sample Projects
PL/SQL Code for Sample ProjectsPL/SQL Code for Sample Projects
PL/SQL Code for Sample Projects
jwjablonski
 

Andere mochten auch (12)

Plsql programs
Plsql programsPlsql programs
Plsql programs
 
Plsql Ref
Plsql RefPlsql Ref
Plsql Ref
 
Oracl DBA lab manual
Oracl DBA lab manualOracl DBA lab manual
Oracl DBA lab manual
 
PLSQL Standards and Best Practices
PLSQL Standards and Best PracticesPLSQL Standards and Best Practices
PLSQL Standards and Best Practices
 
Introduction to Machine Learning for Oracle Database Professionals
Introduction to Machine Learning for Oracle Database ProfessionalsIntroduction to Machine Learning for Oracle Database Professionals
Introduction to Machine Learning for Oracle Database Professionals
 
Oracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practicesOracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practices
 
Oracle PLSQL Step By Step Guide
Oracle PLSQL Step By Step GuideOracle PLSQL Step By Step Guide
Oracle PLSQL Step By Step Guide
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Oracle Complete Interview Questions
Oracle Complete Interview QuestionsOracle Complete Interview Questions
Oracle Complete Interview Questions
 
PL/SQL Code for Sample Projects
PL/SQL Code for Sample ProjectsPL/SQL Code for Sample Projects
PL/SQL Code for Sample Projects
 
Plsql programs(encrypted)
Plsql programs(encrypted)Plsql programs(encrypted)
Plsql programs(encrypted)
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
 

Ähnlich wie Contract-oriented PLSQL Programming

Coldbox developer training – session 4
Coldbox developer training – session 4Coldbox developer training – session 4
Coldbox developer training – session 4
Billie Berzinskas
 
[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...
[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...
[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...
Stowarzyszenie Jakości Systemów Informatycznych (SJSI)
 

Ähnlich wie Contract-oriented PLSQL Programming (20)

DIG1108C Lesson 7 Fall 2014
DIG1108C Lesson 7 Fall 2014DIG1108C Lesson 7 Fall 2014
DIG1108C Lesson 7 Fall 2014
 
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
 
Andreas Grabner - Performance as Code, Let's Make It a Standard
Andreas Grabner - Performance as Code, Let's Make It a StandardAndreas Grabner - Performance as Code, Let's Make It a Standard
Andreas Grabner - Performance as Code, Let's Make It a Standard
 
ASIC design verification
ASIC design verificationASIC design verification
ASIC design verification
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Apex Trigger Debugging: Solving the Hard Problems
Apex Trigger Debugging: Solving the Hard ProblemsApex Trigger Debugging: Solving the Hard Problems
Apex Trigger Debugging: Solving the Hard Problems
 
Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013Rock Your Code With Code Contracts -2013
Rock Your Code With Code Contracts -2013
 
Meg bernal insight2014 4219
Meg bernal insight2014 4219Meg bernal insight2014 4219
Meg bernal insight2014 4219
 
Automated infrastructure as a service testing with VMware NSX
Automated infrastructure as a service testing with VMware NSXAutomated infrastructure as a service testing with VMware NSX
Automated infrastructure as a service testing with VMware NSX
 
Design for Testability in Practice
Design for Testability in PracticeDesign for Testability in Practice
Design for Testability in Practice
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Infrastructure as Code with Ansible
Infrastructure as Code with AnsibleInfrastructure as Code with Ansible
Infrastructure as Code with Ansible
 
Practical C++ Generative Programming
Practical C++ Generative ProgrammingPractical C++ Generative Programming
Practical C++ Generative Programming
 
Agile for Software as a Medical Device
Agile for Software as a Medical DeviceAgile for Software as a Medical Device
Agile for Software as a Medical Device
 
Sorted
SortedSorted
Sorted
 
Coldbox developer training – session 4
Coldbox developer training – session 4Coldbox developer training – session 4
Coldbox developer training – session 4
 
Tdd,Ioc
Tdd,IocTdd,Ioc
Tdd,Ioc
 
Enterprise Node - Code Quality
Enterprise Node - Code QualityEnterprise Node - Code Quality
Enterprise Node - Code Quality
 
Testing Plan
Testing PlanTesting Plan
Testing Plan
 
[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...
[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...
[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...
 

Mehr von John Beresniewicz

Mehr von John Beresniewicz (9)

ASHviz - Dats visualization research experiments using ASH data
ASHviz - Dats visualization research experiments using ASH dataASHviz - Dats visualization research experiments using ASH data
ASHviz - Dats visualization research experiments using ASH data
 
NoSQL is Anti-relational
NoSQL is Anti-relationalNoSQL is Anti-relational
NoSQL is Anti-relational
 
AAS Deeper Meaning
AAS Deeper MeaningAAS Deeper Meaning
AAS Deeper Meaning
 
JB Design CV: products / mockups / experiments
JB Design CV: products / mockups / experiments JB Design CV: products / mockups / experiments
JB Design CV: products / mockups / experiments
 
Awr1page - Sanity checking time instrumentation in AWR reports
Awr1page - Sanity checking time instrumentation in AWR reportsAwr1page - Sanity checking time instrumentation in AWR reports
Awr1page - Sanity checking time instrumentation in AWR reports
 
AWR Ambiguity: Performance reasoning when the numbers don't add up
AWR Ambiguity: Performance reasoning when the numbers don't add upAWR Ambiguity: Performance reasoning when the numbers don't add up
AWR Ambiguity: Performance reasoning when the numbers don't add up
 
Ash Outliers UKOUG2011
Ash Outliers UKOUG2011Ash Outliers UKOUG2011
Ash Outliers UKOUG2011
 
Average Active Sessions - OaktableWorld 2013
Average Active Sessions - OaktableWorld 2013Average Active Sessions - OaktableWorld 2013
Average Active Sessions - OaktableWorld 2013
 
Average Active Sessions RMOUG2007
Average Active Sessions RMOUG2007Average Active Sessions RMOUG2007
Average Active Sessions RMOUG2007
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Contract-oriented PLSQL Programming

  • 1. <Insert Picture Here> Contract-oriented PL/SQL programming John Beresniewicz Technical staff, Server Technologies Oracle USA
  • 2. The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
  • 3. <Insert Picture Here> Agenda • Theory • Design by Contract • Technique • Standardized assertions • Rigorous precondition checking • Function-oriented programming • Practice • Useful contract patterns • Example: prime number package
  • 4. <Insert Picture Here> Theory Design by contract
  • 5. References • Object-oriented Software Construction, 2nd Edition • Bertrand Meyer. Prentice Hall PTR 1997. • The Pragmatic Programmer • Andrew Hunt, Dave Thomas. Addison Wesley Longman, Inc 2000. • Design by Contract, by Example • Richard Mitchell, Jim McKim. Addison-Wesley 2002. • Design Patterns and Contracts • Jean-Marc Jezequel, Michel Train, Christine Mingins. Addison Wesley Longman 2000.
  • 6. <Insert Picture Here> Bertrand Meyer Object Success “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.”
  • 7. Contracts • Legal contracts govern interactions between parties • Each party has obligations and expects benefits under the contract • Each party typically benefits from the other’s obligations, and vice versa
  • 8. Software contracts • Software modules have interactions as calling and called program • Client-supplier relationship between modules • Interaction boundary also known as the API • We can describe these interactions as conforming to contracts • Formalizing and enforcing software contracts promotes reliability and maintainability
  • 9. Contract elements • PRECONDITIONS • What will be true when module is entered? • Caller obligation and supplier benefit • POSTCONDITIONS • What will be true when module completes? • Supplier obligation and caller benefit • INVARIANTS • Any state that will not be changed as a result of module execution
  • 10. Contracting benefits • Preconditions allow suppliers to trust data in • Postconditions allow callers to trust data out • Rigid postcondition obligations promote stricter module scoping • Better functional decomposition • Bugs are detected early and isolated easily • Trusted data + correct algorithms = bug-free code
  • 11. Software defects (bugs) • Contract violations are BUGS…always • Precondition violations are caller bugs • Postcondition violations are supplier bugs
  • 12. <Insert Picture Here> Bertrand Meyer Object-oriented Software Construction “Good contracts are those which exactly specify the rights and obligations of each party...In software design, where correctness and robustness are so important, we need to spell out the terms of the contracts as a prerequisite to enforcing them. Assertions provide the means to state precisely what is expected from and guaranteed to each side in these arrangements.”
  • 13. Assertions: enforcing contracts • Test a contract condition and complain if not met • Contract condition is expressed as a BOOLEAN • Contract violated when expression is FALSE • Complaint is to raise a well-known EXCEPTION • Implement in PL/SQL as a simple procedure
  • 14. Benefits of assertions • Help to write correct software • Documentation aid • Support for testing, debugging and QA • Support for software fault tolerance Source: Object-oriented Software Construction
  • 15. PL/SQL assert PROCEDURE assert (condition_in IN BOOLEAN) IS BEGIN if NOT NVL(condition_in,FALSE) then raise ASSERTFAIL; end if; END assert; • Exits silently or raises ASSERTFAIL • Null input throws ASSERTFAIL
  • 16. Asserting contract elements FUNCTION distance (x_in in number, y_in in number) RETURN number IS l_return number := null; BEGIN assert_pre(x_in not null,’x_in not null’); assert_pre(y_in not null,’y_in not null’); assert_pre(y_in > 0,’y_in >0’); l_return := ABS(x_in – y_in); assert_post(l_return >=0,’return>=0’); RETURN l_return; END distance;
  • 17. <Insert Picture Here> Technique Minimum-optimum
  • 18. Technique • Standardize • Standard private local assert procedure in all packages • Standard common exception ASSERTFAIL in all packages • Enforce preconditions aggressively • Crash on ASSERTFAIL • Modularize • Each module implements a distinct function • Package implements a coherent set of modules
  • 19. Standard declarations -- standard package public assertion declarations ASSERTFAIL EXCEPTION; PRAGMA EXCEPTION_INIT(ASSERTFAIL, -20999); -- standard package private declarations ASSERTFAIL_C CONSTANT INTEGER := -20999; PKGNAME_C CONSTANT VARCHAR2(20) := 'Primes‘ • All packages declare private version of common exception ASSERTFAIL • Externally identical but internally each can be distinguished • Reserve an exception number for global convention
  • 20. Original standard assert (ca 2001) -- standard local packaged assertion procedure PROCEDURE assert (bool_IN IN BOOLEAN ,msg_IN IN VARCHAR2 := null) IS BEGIN IF NOT NVL(bool_IN,FALSE) -- assertfail on null THEN RAISE_APPLICATION_ERROR ( ASSERTFAIL_C, 'ASSERTFAIL:'||PKGNAME_C|| ':'||SUBSTR(NVL(msg_IN,'nomsg'),1,200) ); END IF; END assert; • This procedure identical and private to all packages
  • 21. Proposed asserts (2007) PROCEDURE assert_pre (condition_in IN BOOLEAN ,progname_in IN progname_t ,msg_in IN varchar2) IS BEGIN assert(condition_in ,progname_in||':PRE:'||msg_in); END assert_pre; • We similarly define assert_post
  • 22. Aggressively assert preconditions • A contract enforced on one side only is still a contract enforced • Think of it as tests built into code and checked at runtime • Decomposition increases stability through multiplication of contracts • Thus an incentive to fine-grain modularization • Enables trusted software layer to grow up through the hierarchy
  • 23. Modularize ruthlessly and coherently • Hierarchical layers of packages and modules • Top layer: outward facing • Accept user input • Present high-level client interfaces and errors • Middle layers: complex application logic • Bottom layer: trusted base components • Ruthless precondition testing
  • 24. Assertion-hardened code Assert Assert • Modules organized hierarchically • Each module asserts preconditions • More modules • => more contracts Assert • => more stable code Assert Assert Assert
  • 25. Crash on ASSERTFAIL • It is a bug and must become known • Catch ASSERTFAIL only for good reason: • Testing • Refactoring • Making a contract less demanding • Externalizing a local check function • Dead Programs Tell No Lies • Hunt and Thomas, The Pragmatic Programmer
  • 26. Testing • Testing takes client (calling program) perspective • Whereas coding takes called program perspective • Regression test postconditions • Issue: only externally facing modules can be externally tested • Unit test contract preconditions against many inputs assert(0=computefunction(0.2,100),’test 1001’);
  • 27. Performance considerations • Estimates for contracting overhead (from Meyer): • 50% for precondition checking • 100-200% for postcondition and invariant checking • Minimum-optimum principle favors precondition enforcement • Oracle 10g conditional compilation enables assertions to be compiled into or out of pl/sql programs • CAR Hoare quote
  • 28. <Insert Picture Here> C.A.R. Hoare “The current practice of compiling subscript range checks into the machine code while a program is being tested, then suppressing the checks during production runs, is like a sailor who wears his life preserver while training on land but leaves it behind when he sails!” http://whats.all.this.brouhaha.com/?page_id=479
  • 29. 10g Conditional assertions PROCEDURE trusted (p1_in in number) IS BEGIN $IF $$ASSERTPRE $THEN assert(p1_in > 0); $END /* do trusted logic */ END trusted; SQL> alter session set PLSQL_CCFLAGS=’ASSERTPRE:TRUE; SQL> alter procedure trusted compile; • After recompiling assert will be included • Recommend restricted rather than global application
  • 30. BOOLEAN functions in contracts FUNCTION ready_to_process(ID_in) return BOOLEAN; PROCEDURE process_id(ID_in) IS BEGIN assert(ready_to_process(ID_in)); --pre /* do the processing */ END; • This can be very powerful, and potentially very dangerous • When is it OK to assert functions as preconditions?
  • 31. <Insert Picture Here> Bertrand Meyer Object-oriented Software Construction “If you exert the proper care by sticking to functions that are simple and self-evidently correct, the use of function routines in assertions can provide you with a powerful means of abstraction.”
  • 32. Check functions FUNCTION check_all_ok return BOOLEAN is BEGIN assert(condition1,…); assert(condition2,…); … assert(conditionN,…); return TRUE; END check_all_ok; • Compound set of assertions • Self-evidently correct - TRUE or ASSERTFAIL • Useful for compressing shared sets of preconditions assert(check_all_ok,module_name||’:check_all_ok’);
  • 33. <Insert Picture Here> Practice Using contracts in programs
  • 34. Useful contract patterns • NOT NULL IN • Near-universal precondition • RETURN NOT NULL • Very attractive postcondition • RETURN BOOLEAN NOT NULL • Direct function references in conditional code • RETURN BOOLEAN TRUE or ASSERTFAIL • Check functions
  • 35. PL/QL Best Practices, 2nd Edition FUNCTION excuse_in_use (excuse_in IN excuse_excuse_t) RETURN BOOLEAN IS c_progname CONSTANT progname_t:='EXCUSE_IN_USE'; l_return BOOLEAN; BEGIN -- check caller obligations assert_pre(condition_in => excuse_in is not null ,progname_in => c_progname ,msg_in => 'excuse_in not null'); -- compute return value l_return := g_excuses_used.EXISTS(excuse_in); -- check return obligations assert_post(condition_in => l_return is not null ,progname_in => c_progname ,msg_in => 'l_return not null'); RETURN l_return; END excuse_in_use;
  • 36. Bugs find you… SQL> l 1 begin 2 if excuse_tracker.excuse_in_use('lame excuse') 3 then 4 dbms_output.put_line('lame excuse in use'); 5 elsif 6 excuse_tracker.excuse_in_use(NULL) 7 then 8 dbms_output.put_line('NULL is no excuse!'); 9 end if; 10* end; SQL> / begin * ERROR at line 1: ORA-20999: ASSERTFAIL:EXCUSE_TRACKER:EXCUSE_IN_USE:PRE:excuse_in not null ORA-06512: at "SYS.EXCUSE_TRACKER", line 62 ORA-06512: at "SYS.EXCUSE_TRACKER", line 85 ORA-06512: at "SYS.EXCUSE_TRACKER", line 30 ORA-06512: at line 6
  • 37. Primes package: factoring by division FUNCTION factor(num_IN INTEGER, divisor_IN INTEGER) RETURN INTEGER IS tmp_power integer; tmp_num integer; BEGIN assert(num_IN IS NOT NULL,'factor:num_IN not null'); assert(divisor_IN IS NOT NULL,'factor:divisor_IN not null'); assert(divisor_IN >1 AND num_IN >=divisor_IN ,'factor:1<divisor<=num_IN'); tmp_num := num_IN; tmp_power := 0; -- (1) initialize WHILE MOD(tmp_num,divisor_IN) = 0 -- still divides evenly LOOP tmp_power := tmp_power + 1; tmp_num := tmp_num / divisor_IN; END LOOP; RETURN tmp_power; END factor;
  • 38. Concluding remarks • Design by Contract principles are powerful constructs for producing correct software • Using standardized package assertions we can rigidly enforce contract preconditions in PL/SQL • Native support for DbC in PL/SQL highly desirable • Standardized ASSERTFAIL exception • Precondition, postcondition, and invariant assertion syntax • Produce source line and offending condition automatically
  • 39. 10g goodies • Conditional compilation: PLSQL_CCFLAGS • Compile assertions into or out of code • Inquiry directives • $$PLSQL_UNIT • Replace constant literal PKGNAME_C • $$PLSQL_LINE • Identify line of code throwing ASSERTFAIL • DBMS_PREPROCESSOR • Surface the line of code throwing ASSERTFAIL
  • 40. dbc10g.sql – 10g assert experiments -- new 10g signature PROCEDURE assert (line_no_IN IN PLS_INTEGER ,bool_expr_IN IN BOOLEAN ,msg_IN IN VARCHAR2 := null); -- simple test procedure PROCEDURE p1 is begin assert($$PLSQL_LINE -- assert comment on first line ,25 < to_number('24') -- assert boolean expr ,to_char(SYSDATE,'YYYY:MM:DD:HH24:MI:SS')); --msg end p1;
  • 41. 10g assert: debug info in ASSERTFAIL BEGIN dbc10g.p1; END; * ERROR at line 1: ORA-20999: ASSERTFAIL:PKGBDY:DBC10G.18: assert( 18 -- assert comment on first line ,25 < to_number('24') -- assert boolean expr :2008:10:10:22:06:53 ORA-06512: at "SYS.DBC10G", line 75 ORA-06512: at "SYS.DBC10G", line 18 ORA-06512: at line 1