Know Your Code

download Know Your Code

of 26

Transcript of Know Your Code

  • 7/30/2019 Know Your Code

    1/26

    Use PL/SQL to automate standards compliance

    Lewis CunninghamData Architect and ACE Director

  • 7/30/2019 Know Your Code

    2/26

    2

    IntroductionOracle ACE Director

    AuthorConsultant

    Data Architect

    Blogger,An Expert's Guide to OracleTechnology

    http://it.toolbox.com/blogs/oracle-guidehttp://it.toolbox.com/blogs/oracle-guide
  • 7/30/2019 Know Your Code

    3/26

    My books

  • 7/30/2019 Know Your Code

    4/26

    AuthorsJohn Beresniewicz, Adrian Billington,

    Martin Buchi, Melanie Caffery, Ron

    Crisco, Lewis Cunningham, DominicDelmolino, Sue Harper, TorbenHolm, Connor McDonald, Arup

    Nanda, Stephan Petit, MichaelRosenblum, Robyn Sands, RiyajShamsudeen

  • 7/30/2019 Know Your Code

    5/26

    Standards and Compliance Standards

    Coding Standards

    Naming Standards Performance Standards

    Testing Standards

    Compliance

    Code Reviews Test Scripts and Results

  • 7/30/2019 Know Your Code

    6/26

    Source Code AnalysisStatic Analysis

    What you know at compile time

    Dynamic Analysis

    What you learn from running the code

    Instrumentation

    What you should make your code tell you

  • 7/30/2019 Know Your Code

    7/26

    When to analyze 2 AM on the day before vacation?

    After implementation?

    After testing?After coding?

    During Coding?

    I vote for during coding.

  • 7/30/2019 Know Your Code

    8/26

    Know Your CodeData Dictionary - Static

    PL/Scope - StaticProfilers - Dynamic

    Source Code StaticLogging - Instrumentation

  • 7/30/2019 Know Your Code

    9/26

    The Data Dictionary USER_SOURCE

    ALL_SOURCE

    DBA_SOURCE

    Viewable source code

    Manual Code Reviews

  • 7/30/2019 Know Your Code

    10/26

    The Data Dictionary USER_DEPENDENCIES

    ALL_DEPENDENCIES

    DBA_DEPENDENCIES

    View dependencies between code and database objects(including other code units)

    Who calls who

  • 7/30/2019 Know Your Code

    11/26

    The Data Dictionary USER_PROCEDURES

    ALL_PROCEDURES

    DBA_PROCEDURES

    View information about code (aggregate,deterministic)

    Identify program units in packages and type bodies

  • 7/30/2019 Know Your Code

    12/26

    The Data Dictionary USER_ARGUMENTS

    ALL_ARGUMENTS

    DBA_ARGUMENTS

    View argument names and data types

    Used with user_procedures it is a starting point forautomated code reviews Naming

    Data Types

  • 7/30/2019 Know Your Code

    13/26

    The Data Dictionary USER_TYPES and USER_TYPE_METHODS

    ALL_TYPES and ALL_TYPE_METHODS

    DBA_TYPES and DBA_TYPE_METHODS

    Combine with USER_PROCEDURES andUSER_ARGUMENTS for information about types

    For reviewing code related to types

  • 7/30/2019 Know Your Code

    14/26

    The Data Dictionary PL/Scope

    USER_IDENTIFIERS

    ALL_IDENTIFIERS DBA_IDENTIFIERS

    Everything discussed and more

    Great for Automating Code Reviews

  • 7/30/2019 Know Your Code

    15/26

    PL/Scope Introduced in 11g

    Need to turn it on for it to work

    SQL Developer turns it on for you

    SQL*Plus: ALTER SESSION SETPLSCOPE_SETTINGS =

    'IDENTIFIERS:ALL';

    Compile the code

  • 7/30/2019 Know Your Code

    16/26

    PL/Scope Works with identifiers Almost everything is an identifier

    Data types and subtypes used (including collections) Cursors and ref cursors Procedures Functions Packages and package bodies Triggers Object types and type bodies Parameters Function return values Variables, iterators, constants Exceptions Synonyms

  • 7/30/2019 Know Your Code

    17/26

    PL/Scope Data Gathered on every

    Declaration procedures, variables, etc

    Definition the implementation of an identifier(such as a procedure within a package)

    Assignment

    Reference Calls

  • 7/30/2019 Know Your Code

    18/26

    PL/Scope Can validate naming standards of variables as well as

    parameters

    Identifies scoping issues Change impact analysis

    Find unused identifiers

    Validate data types

  • 7/30/2019 Know Your Code

    19/26

    Dynamic AnalysisDBMS_PROFILER

    Code Profiling

    Timing between callsDBMS_TRACE

    Code Call Context

    Who called whoDBMS_HPROF

    Code profiling and context

  • 7/30/2019 Know Your Code

    20/26

    Dynamic Analysis Run a baseline profile

    Save timings

    Run a new profile after changes are made Can be mostly automated

    Validate the original timings and only worry if latertimes go beyond defined criteria

  • 7/30/2019 Know Your Code

    21/26

    Code Coverage Make sure all executable lines are executed during

    testing

    Doesn't validate the logic, just makes sure the line wasrun

    Proves that for a certain scenario, the code will executesuccessfully

    Does not prove that the code is correct

    Does not prove that the code will execute correctly fora different data set

  • 7/30/2019 Know Your Code

    22/26

    Code Coverage Combines Static Analysis with Dynamic Analysis

    Static analysis (PL/Scope) knows what lines areexecutable

    Dynamic Analysis shows what actually is executed

    Subtract dynamic from static and whats left are thoselines of code not executed

  • 7/30/2019 Know Your Code

    23/26

    Code CoverageCode Coverage is not 100%

    Doesn't account for all lines of SQL (because

    PL/Scope doesn't track SQL) I'm hoping for additional information from

    PL/Scope in the future

  • 7/30/2019 Know Your Code

    24/26

    SummaryCreate standards

    Follow standards

    Test for standards compliance

    Automate when possible

    Be creative with the data dictionary

  • 7/30/2019 Know Your Code

    25/26

    Buy the book

  • 7/30/2019 Know Your Code

    26/26

    Use PL/SQL to automate standards compliance

    Lewis CunninghamData Architect and ACE Director