WELCOME, GUEST
Minimize
Blogger List

Steven Feuerstein Indicates Oracle ACE director status
PL/SQL Obsession

Guy Harrison Indicates Oracle ACE status
Database topics

Bert Scalzo Indicates Oracle ACE status
Toad for Oracle, Data Modeling, Benchmarking
Dan Hotka Indicates Oracle ACE director status
SQL Tuning & PL/SQL Tips

Valentin Baev
It's all about Toad

Ben Boise
Toad SC Discussions

Dan Clamage
SQL and PL/SQL

Kevin Dalton
Benchmark Factory

Peter Evans 
Business Intelligence, Data Integration, Cloud and Big Data

Vaclav Frolik  
Toad Data Modeler, Toad Extension for Eclipse

Devin Gallagher
Toad SC discussions

Stuart Hodgins
JProbe Discussions

Julie Hyman
Toad for Data Analysts

  Henrik "Mauritz" Johnson
Toad Tips & Tricks on the "other" Toads
  Mark Kurtz
Toad SC discussions
Daniel Norwood
Tips & Tricks on Toad Solutions
Amit Parikh
Toad for Oracle, Benchmark Factory,Quest Backup Reporter
Debbie Peabody
Toad Data Point
Gary Piper
Toad Reports Manager
John Pocknell
Toad Solutions
Jeff Podlasek
Toad for DB2
Kuljit Sangha
Toad SC discussions
Michael Sass 
Toad for DB2
Brad Wulf
Toad SC discussions
Richard To
SQL Optimization
  Toad Data Modeler Opens in a new window
Data Modeling
 
  Toad Higher Education
How Hi-Ed Uses Toad
  Real Automated Code Testing for Oracle
Quest Code Tester blog
  中文技术资料库
技术文章
 

Blogs

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.


Nov 28

Written by: StevenFeuersteinTW
Wednesday, November 28, 2012 1:36 PM  RssIcon

We at Toad World are very pleased to offer excerpts from Steven Feuerstein's Oracle PL/SQL Best Practices Second Edition in this blog. Many thanks to O'Reilly Media for giving permission to publish the excerpts. Here's a description of the Oracle PL/SQL Best Practices: "In this compact book, Steven Feuerstein, widely recognized as one of the world's leading experts on the Oracle PL/SQL language, distills his many years of programming, teaching, and writing about PL/SQL into a set of best practices-recommendations for developing successful applications. Covering the latest Oracle release, Oracle Database 11g Release 2, Feuerstein has rewritten this new edition in the style of his best-selling Oracle PL/SQL Programming. The text is organized in a problem/solution format, and chronicles the programming exploits of developers at a mythical company called My Flimsy Excuse, Inc., as they write code, make mistakes, and learn from those mistakes-and each other."

Problem: One person's clarity is another person's bewilderment.

Jasper is having a good time! He has just learned about collections, collections of records, and collections of collections, and he is enjoying himself tremendously using these complex data structures. He is deep into his program, fully conceptualizing the structures—what they mean, how they relate. And while he is in "the zone," he writes code like this without giving it a second thought:

IF master_list(l_curr_index).properties_flag.field1 = 'N' THEN

And at that moment, he understands exactly what it means. A few months later, however, a user reports a bug in the application, and it is traced back to the program containing this line of code. Jasper is on holiday, so Lizbeth is assigned the job of fixing this bug.

When she looks at the code, she gets really angry. What does it mean? How is she supposed to have any idea how to fix the program when she can't even figure out what is going on? Well, there is nothing to do but push through her frustration. But she sends off a somewhat nasty note to Jasper telling him, "You should put in some comments if you are going to write such cryptic stuff, Jasper!"

Jasper comes back to work and is surprised by the email. He remembers with joy the clarity of the moment when he wrote that code. What was hard to understand? To his utter dismay, however, when he opens up the file and looks at the code himself, he realized that even he can't understand the intent of that line of code....how embarrassing!

Solution: Sometimes you really do need to add comments.

Well, Jasper knows he has lots to learn, and he has clearly made a mistake here. A comment is needed. And so he changes the code to this:

/* If the first field of the properties record is N... */
IF master_list(l_curr_index).properties_flag.field1 = 'N' THEN

Unfortunately, when he shows this to Lizbeth, she gets even more irritated. "Jasper, this doesn't help. It doesn't tell me anything beyond what the code already says!" Jasper sighs, "So I should explain what my code is doing, right? OK, how about this."

/* If the customer is not eligible for a discount... */
IF master_list(l_curr_index).properties_flag.field1 = 'N' THEN

Lizbeth exclaims:

Much better! That would have helped me tremendously last week, but it's never too late to improve one's code. Of course, there are still some problems. For one thing you've got that hardcoded "N" sitting there. Also, frankly, while it’s nice to provide the comment, it would be even nicerto write your code so that it truly is self-documenting.

"For example," continues Lizbeth, taking control of Jasper's keyboard, "rather than expose all that very low-level structural code, why not create a function whose name describes the activity and whose body contains the complicated logic? Like this...."

/* If the customer is not eligible for a discount... */
IF customer_not_eligibility (l_curr_index) = 'N' THEN

"Now we don't really need the comment," points out Jasper, getting into the swing of things, "Hey, and we could even avoid the hardcoded 'N' by making the function a Boolean that returns TRUE or FALSE. Like this...."

IF customer_not_eligible (l_curr_index) THEN

Jasper is very pleased with himself. Not only has he written some really cool code, but now it’s even possible to understand that code!

Wait a minute, though! Does this mean that, in fact, you don't really ever need to put comments in your code? Well, certainly, if you can write code that speaks for itself without additional comments, that is ideal. There are, however, some scenarios in which comments really are required; these include:

  • Program headers: An example is given in the previous best practice. A standard header makes it easy for a person to get a basic orientation to what the program is supposed to do. In addition, the modification history for a program is critical.
  • Special steps: These include explanations of workarounds, patches, operating-system dependencies, and other “exceptional” circumstances. If you have to take special steps when you are writing your code, you should include an explanation or "tag" to point out the special nature of that code.
  • Complex or opaque logic: Let's face it, lots of what we write really is quite complicated. When you are implementing a very challenging algorithm, you should include a comment offering insights to everyone coming along later (including yourself!) so that they are not totally baffled by what you have done.

2 comment(s) so far...


Gravatar

Re: Who needs comments? My code is self-documenting! AKA: Comment tersely with value-added information.

Hello, Steven!
By title, I thought that it would be about "plsqldoc" or other selfdocumeting system :)
BTW, i use doxygen for selfdocumenting DML,DDL and PL/SQL. Can not find the time to write about it in detail.

By Sayan on   Wednesday, December 12, 2012 8:30 AM
Gravatar

Re: Who needs comments? My code is self-documenting! AKA: Comment tersely with value-added information.

I know coding style differs. But I am particularly edgy about a function returning TRUE for a negative check (I know, I know, it is just me, but still saying). I would rather rewrite the following:

IF customer_not_eligible (l_curr_index) THEN

into

IF NOT customer_eligible (l_curr_index) THEN

this way, I can use the same function to see if the customer is eligible (otherwise, you would be doing, " IF NOT customer_not_eligible (l_curr_index) " which is double negation and could be confusing.

Hey! commenting is all about understand ability of the code. Thats my 2 cent there. ;)

By guruparan18 on   Wednesday, December 12, 2012 8:31 AM
Search Blog Entries
 
Blog Archives
 
Archive
<May 2013>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
Monthly
May, 2013 (14)
April, 2013 (13)
March, 2013 (10)
February, 2013 (5)
January, 2013 (7)
December, 2012 (6)
November, 2012 (10)
October, 2012 (8)
September, 2012 (6)
August, 2012 (8)
July, 2012 (8)
June, 2012 (12)
May, 2012 (21)
April, 2012 (10)
March, 2012 (16)
February, 2012 (19)
January, 2012 (20)
December, 2011 (19)
November, 2011 (14)
October, 2011 (12)
September, 2011 (17)
August, 2011 (15)
July, 2011 (16)
June, 2011 (13)
May, 2011 (15)
April, 2011 (8)
March, 2011 (21)
February, 2011 (17)
January, 2011 (16)
December, 2010 (13)
November, 2010 (13)
October, 2010 (7)
September, 2010 (15)
August, 2010 (11)
July, 2010 (13)
June, 2010 (12)
May, 2010 (14)
April, 2010 (12)
March, 2010 (13)
February, 2010 (12)
January, 2010 (7)
December, 2009 (10)
November, 2009 (12)
October, 2009 (15)
September, 2009 (18)
August, 2009 (13)
July, 2009 (23)
June, 2009 (14)
May, 2009 (17)
April, 2009 (7)
March, 2009 (14)
February, 2009 (7)
January, 2009 (12)
December, 2008 (7)
November, 2008 (11)
October, 2008 (19)
September, 2008 (14)
August, 2008 (11)
July, 2008 (14)
June, 2008 (19)
May, 2008 (12)
April, 2008 (18)
March, 2008 (13)
February, 2008 (8)
January, 2008 (7)
December, 2007 (5)
November, 2007 (8)
October, 2007 (13)
September, 2007 (13)
August, 2007 (16)
July, 2007 (11)
June, 2007 (6)
May, 2007 (5)
April, 2007 (5)
March, 2007 (8)
February, 2007 (6)
January, 2007 (6)
December, 2006 (5)
November, 2006 (8)
October, 2006 (4)
August, 2006 (3)