Minimize
Blogger List

Johannes Ahrends
Toad and Oracle

Ben Boise
Toad SC Discussions

Kevin Dalton
Benchmark Factory

Steven Feuerstein
PL/SQL Obsession

Devin Gallagher
Toad SC discussions

Stuart Hodgins
JProbe Discussions

  Henrik "Mauritz" Johnson
Toad Tips & Tricks on the "other" Toads
  Mark Kurtz
Toad SC discussions
  Michael Lumbard
Toad SC discussions
Daniel Norwood
Toad for Data Analysts,
Toad Extension for Visual Studio
Debbie Peabody
Toad for Data Analysts
Gary Piper
Toad Reports Manager
John Pocknell
Toad for Oracle, JProbe
Kuljit Sangha
Toad SC discussions
Bert Scalzo Indicates Oracle ACE status
Toad for Oracle, Data Modeling, Benchmarking
Jeff Smith
Toad product family
Richard To
SQL Optimization
Jim Wankowski
DB2 - LUW and z/OS
John Weathington
  Toad Data Modeler Opens in a new window
Data Modeling
 
  Real Automated Code Testing for Oracle
Quest Code Tester blog
 
Minimize
Blog Tags
toad for oracle (122)
oracle (62)
plsql (46)
sql optimization (37)
toad for data analysts (28)
code tester (19)
toad for ibm db2 (13)
automation (11)
batch optimizer (10)
virtualization (10)
schema browser (9)
toad for sql server (9)
data grid (8)
sql (8)
sql editor (8)
toad data modeler (8)
benchmark factory (7)
excel (7)
query builder (7)
report manager (7)
toad extension (7)
visual studio (7)
11g (6)
configuration (6)
freeware (6)
health check (6)
vmware (6)
connect (5)
dba module (5)
er diagrammer (5)
F4 (5)
linux (5)
refactoring (5)
spotlight (5)
unicode (5)
compare (4)
debugger (4)
export (4)
formatter (4)
make code (4)
rman (4)
strip code (4)
benchmark (3)
bfscript (3)
bulk collect (3)
code templates (3)
code xpert (3)
database browser (3)
db2 (3)
notebook (3)
oem (3)
RAC (3)
session browser (3)
speed (3)
sql optimizer (3)
toad for mysql (3)
tpc-c (3)
9.7 (2)
alert log (2)
app designer (2)
awr (2)
code insight (2)
code snippets (2)
collection (2)
compare and sync (2)
compliance (2)
data generator (2)
data warehouse (2)
database explorer (2)
database monitor (2)
explain (2)
forall (2)
ftp (2)
group execute (2)
handbook (2)
installation (2)
job scheduler (2)
multi-task (2)
nested table (2)
os command (2)
profiler (2)
recovery (2)
release history (2)
save as (2)
schema compare (2)
sql recall (2)
stats pack (2)
subversion (2)
team coding (2)
trace file browser (2)
while loop (2)
10g (1)
64 bit (1)
7zip (1)
action (1)
addm (1)
alter (1)
ansi join (1)
array (1)
ccleaner (1)
code coverage (1)
code road map (1)
CRON (1)
cursor for loop (1)
data browser (1)
data subset (1)
database probe (1)
dbms_flashback (1)
dbms_profiler (1)
ddl (1)
feuerstein (1)
filezilla (1)
flash drive (1)
flow control (1)
for loop (1)
group policy manager (1)
hints (1)
import (1)
index (1)
inheritance (1)
invoker rights (1)
ipad (1)
java (1)
latency (1)
log switch (1)
logical model (1)
ltrim (1)
master-detail browser (1)
monitor (1)
multi-select (1)
naming standards (1)
network (1)
object explorer (1)
OEBS (1)
package (1)
parser (1)
partitioning (1)
performance (1)
pragma (1)
project manager (1)
RAT (1)
revo (1)
REXEC (1)
schema report (1)
script manager (1)
search (1)
set operator (1)
sga (1)
slow (1)
sonarsource (1)
source control (1)
space projection (1)
sql monitor (1)
sql navigator (1)
sql script (1)
sql tracker (1)
sql*plus (1)
standards (1)
statistics (1)
stored procedure (1)
string parser (1)
sub-model (1)
sub-type (1)
synch (1)
synchback (1)
TELNET (1)
toad (1)
trace (1)
unit test (1)
unix (1)
usb (1)
utility (1)
v10 (1)
v9.5 (1)
version control (1)
waits (1)
workload replay (1)
workspace (1)
xml (1)
 
WELCOME, GUEST
 
 

Blogs
Toad and Database Commentaries

Toad World blogs are a mix of insightful how-tos from Quest experts as well as their commentary on experiences with new database technologies.  Have some views of your own to share?  Post your comments!  Note:  Comments are restricted to registered Toad World users.

Do you have a topic that you'd like discussed?  We'd love to hear from you.  Send us your idea for a blog topic.


Sep 26

Written by: StevenFeuersteinTW
Wednesday, September 26, 2007 10:23 AM  RssIcon

Use templates to accelerate high quality coding.

When you open a new edit window in Toad, it looks like this:

Well, of course it does....it's an empty, new edit widow!

That's fine, but suppose I now need to create a new package for my application. Sadly, what many of us do is start typing:

And that, dear friends, is a very unproductive way to write code (from "scratch" each time), much less write code that meets the coding standards of your team, much less write code that incorporates best practices.

Fortunately, you can take advantage of Toad Code Templates to both improve productivity and increase code quality, when creating new packages.

Here are the steps I took to standardize package design and development:

1. Create a template for any new packages in my application.
2. Store the template in the Toad's Code Template library.
3. Invoke the template whenever I or anyone else on my team needs to create a new package.

The following sections show precisely how to perform each of these steps.

1. Create the template

The following template may also be found in the demo.zip file stdpkg.sql file. Here are some of the main features of this template:

  • It includes a package-level initialization program that is initially NULL and is called in the initialization section. This encourages consolidation of all package setup code into a single program, which may then also be called from outside the package.
  • Templates for procedures and functions within the package. For each of these....
  • Initialization and cleanup modules for each subprogram to encourage proper isolation of this logic, and invocations of cleanup on all exit points from the program.
  • A checklist of key coding best practices embedded as a comment in the subprogram bodies.
  • An exception section that repeats the cleanup and also reminds me to use the standard error management facility.

By placing such code and comments inside the

CREATE OR REPLACE PACKAGE pkg

/*

| Copyright Information Here

|

| File name:

|

| Overview:

|

| Author(s):

|

| Modification History:

|   Date        Who         What

|

*/

IS

   /* Decide whether or not you want to expose the package

      initialization routine! */

   PROCEDURE initialize;

END pkg;

/

 

CREATE OR REPLACE PACKAGE BODY pkg

/*

| Copyright Information Here

|

| File name:

|

| Overview:

|

| Author(s):

|

| Modification History:

|   Date        Who         What

|

*/

IS

   PROCEDURE initialize

   IS

   BEGIN

      NULL;

   END initialize;

 

   PROCEDURE procedure1

   /*

   | Copyright Information Here

   |

   | File name:

   |

   | Overview:

   |

   | Author(s):

   |

   | Modification History:

   |   Date        Who         What

   |

   */

   IS

      PROCEDURE initialize

      IS

      BEGIN

         NULL;

      END initialize;

 

      PROCEDURE cleanup

      IS

      BEGIN

         NULL;

      END cleanup;

   BEGIN

      initialize;

      /* REMOVE AFTER REVIEW!

 

      Main body of program. Don't forget:

 

      ** Use BULK COLLECT and FORALL for multi-row SQL.

      ** Hide complex rules behind functions.

      ** Hide SQL statements behind a table API.

      ** Think about error handling now!

      ** Keep your executable small (< 50 lines).

 

      */

      cleanup;

   EXCEPTION

      WHEN OTHERS

      THEN

         /* Don't forget to clean up here, too! */

         cleanup;

         /* Use the standard error logging mechanism.

 

            Example: Quest Error Manager, available at:

            http://www.oracleplsqlprogramming.com/downloads/qem.zip

         */

         q$error_manager.raise_error (error_code_in      => SQLCODE

                                    , name1_in           => 'NAME1'

                                    , value1_in          => 'VALUE'

                                     /* Up to five name-value pairs accepted! */

                                     );

   END procedure1;

 

   FUNCTION function1

      RETURN datatype

   /*

   | Copyright Information Here

   |

   | File name:

   |

   | Overview:

   |

   | Author(s):

   |

   | Modification History:

   |   Date        Who         What

   |

   */

   IS

      /* The value returned by the function */

      l_return   datatype;

 

      PROCEDURE initialize

      IS

      BEGIN

         NULL;

      END initialize;

 

      PROCEDURE cleanup

      IS

      BEGIN

         NULL;

      END cleanup;

   BEGIN

      initialize;

      /* REMOVE AFTER REVIEW!

 

      Main body of program. Don't forget:

 

      ** Use BULK COLLECT and FORALL for multi-row SQL.

      ** Hide complex rules behind functions.

      ** Hide SQL statements behind a table API.

      ** Think about error handling now!

      ** Keep your executable small (< 50 lines).

 

      */

      cleanup;

      /* Just one return in the executable section! */

      RETURN l_return;

   EXCEPTION

      WHEN OTHERS

      THEN

         /* Don't forget to clean up here, too! */

         cleanup;

         /* Use the standard error logging mechanism.

 

            Example: Quest Error Manager, available at:

            http://www.oracleplsqlprogramming.com/downloads/qem.zip

            */

         q$error_manager.raise_error (error_code_in      => SQLCODE

                                    , name1_in           => 'NAME1'

                                    , value1_in          => 'VALUE'

                         /* Up to five name-value pairs accepted! */

                                     );

   END function1;

BEGIN

   initialize;

END pkg;

/


2. Store the template

So I like my template. Now it is time to turn it into a real Toad template. So I copy all of it into the clipboard and then right click inside the edit window. Way down at the bottom of the right click menu, you will see "Editing options." Choose that one.

Then you see the enormous, elaborate and quite wonderful World of Toad Options. Notice the bottom right corner lets you choose the language type to edit. Make sure PL/SQL is the current language and press the Edit button:

Then click on the Code Templates tab:

Then press the Add button to add a new one:

I used the string "stdpkg" as the name for my template:

And after pressing OK, I could paste in the contents of my template code:

Press OK, then OK a second time to return to the editor. The template is ready for use.

3. Use the template

And it sure is easy to use! The next time I open up a new editor to create a package, I simply type "stdpkg", then press Control-Space, and my phrase is replaced with all of my template package code.

Of course this is just one template of many you can and should create to standardize development and improve programmer productivity.

Tags:
Categories:
Search Blog Entries