|
|
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
|
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
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...
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
|
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
|
|
|