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.


Mar 1

Written by: StevenFeuersteinTW
Monday, March 01, 2010 9:26 AM  RssIcon

We all know that hard-coding is a bad thing in software. But most developers think of hard-coding simply as typing a literal value into your program whenever you need it.

Example: the maximum value for a salary allowed in our company is $10,000,000. So I write code like:

IF l_employee.salary > 10000000 THEN

So what's wrong with doing this? Nothing - as long as the value is never going to change. But what's the chance of that happening? In fact, what's the chance of anything staying the same (never changing) in our application requirements and resulting code?

Almost nil.

So the best thing we can to improve the maintainability of our code is to internalize deeply the following:

  1. Everything is going to change, so don't repeat anything.
     
  2. Aim for a single point of definition (SPOD) for everything in your application.
     
  3. Reference/invoke that SPOD whenever that "thing" is needed.

So instead of typing "10000000" when I needed to specify the "maximum value", I would do the following:

  1. Create a package to hold/hide the value (if an appropriate package does not already exist):
CREATE OR REPLACE PACKAGE app_config
IS
   FUNCTION maximum_salary
      RETURN NUMBER;
END app_config;
/
 
CREATE OR REPLACE PACKAGE BODY app_config
IS
   FUNCTION maximum_salary
      RETURN NUMBER
   IS
   BEGIN
      RETURN 10000000;
   END maximum_salary;
END app_config;
/
  1. Then I can go back to my code and change it to:
IF l_employee.salary > app_config.maximum_salary() THEN

No more hard-coding, and if the value ever needs to change, I only change it in the package body, so none of the programs that invoke the package will have to be recompiled.

Just about the biggest challenge when it comes to avoiding hard-coding in your application is to recognize when you are hard-coding. So here's a list of some of the scenarios which I believe represent a hard-coding:

Scenario
What to do about it
Any literal value
Hide the value behind a constant or a function. The function is a bit slower than referencing a constant but you can hide the value.
Constrained declarations like VARCHAR2(10)
Whenever you declare a variable with a limited size, you run the risk of raising a VALUE_ERROR exception. Better instead to define a subtype and hide the constraint behind the name of the subtype. Then when you declare your variable, use the subtype as the type.
Fetch into a list of variables
This is a hard-coding because if the number or type of expressions in the select list ever changes, you have to change the fetch statement as well. Better instead to fetch into a record based on the cursor with %ROWTYPE.
Business rules and formulas
Rules change all the time and usually get more complex, so you should always hide rules and formulas behind a function, and then just call the function.
SQL statements
This is not obvious to most developers, but every time you write an SQL statement you are taking a "snapshot" of a small slice of your business model - and that model is also always in the process of changing. So....hide all your SQL statements behind procedures (to change data in tables) and functions (to return data) - a table or transaction API. The Quest CodeGen Utility can help you do this.
 

1 comment(s) so far...


Gravatar

Re: Say goodbye to hard-coding!

why not use a constant?

CREATE OR REPLACE PACKAGE p_const AS
maximum_salary CONSTANT l_employee.salary%TYPE := 10000000;
END;
--no body

--excuse me for replying to an old post.

By MarcW on   Monday, November 01, 2010 6:47 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)