Comment On Horrible Orable Code

Orable ... as in Oracle .. get it? Eh, eh? Oh yes, Oracle fans can find that and all sorts of other creative puns over at the IHOC. But I didn't pick Matt's submission just so I could plug the club ... or even so I could share the obligatory Oracle Consultant link. No, today's code actually represents an impressive combination of both the use and avoidance of the built-in string padding function, RPAD ... [expand full text]
« PrevPage 1 | Page 2Next »

Re: Horrible Orable Code

2005-08-30 14:45 • by christoofar
It's cr@p like this that reminds me why I prefer to use DB/2 and Microsoft SQL Server.

Re: Horrible Orable Code

2005-08-30 14:50 • by Xepol
Dang, almost made first reply.

RPAD ('', 9, ' '); was apparently too easy.

Re: Horrible Orable Code

2005-08-30 14:55 • by El Duderino
42410 in reply to 42407

christoofar:
It's cr@p like this that reminds me why I prefer to use DB/2 and Microsoft SQL Server.


I'm sure there's some WTFers out there that can abuse those in equal degrees.


What's up with special cases for 1 and 2 space pads?  Bizarre.

Re: Horrible Orable Code

2005-08-30 14:57 • by ItsAllGeekToMe

Oracle...........more like............Boracle....................right?


 

Re: Horrible Orable Code

2005-08-30 15:00 • by Billy Madison
42413 in reply to 42411
ItsAllGeekToMe:

Oracle...........more like............Boracle....................right?


 



 


NO I WILL NOT MAKE OUT WITH YOU!


This girl wants to make out with me in the middle of class!

Re: Horrible Orable Code

2005-08-30 15:01 • by Manni

I don't write any functions inside SQL/Oracle queries, so this one took me a few minutes to figure out. Then I realized it's because I don't have nearly enough brain damage for this to make sense. Our "programmer" made a function that recreates the functionality of a built-in function but inexplicably gives special circumstances to a few situations depending on the parameter passed to it.


This is especially funny because RPAD seems to pad spaces on by default, so there is no reason to create a wrapper function that uses the space as the parameter to pass to RPAD. Unless ya got the dain brammage, that is.

Re: Horrible Orable Code

2005-08-30 15:10 • by kipthegreat
42416 in reply to 42414
Dumb question from someone who is certainly not
a brillant database genius... Is it normal for everything except
variable names to be in upper case?  That is really bad netiquette
(codiquette?) if you ask me....

Re: Horrible Orable Code

2005-08-30 15:11 • by scheky
42417 in reply to 42414

it's....it's beautiful..........


 

Re: Horrible Orable Code

2005-08-30 15:18 • by Ross
This code is brilliant. It optimizes out the function RPAD, which is
known to be slow, with faster code for one or two char strings.

Re: Horrible Orable Code

2005-08-30 15:20 • by Ross
42420 in reply to 42416
kipthegreat:
Dumb question from someone who is certainly not
a brillant database genius... Is it normal for everything except
variable names to be in upper case?  That is really bad netiquette
(codiquette?) if you ask me....


Yes, it is typical for SQL reserved words / built-in functions to be in
upper case, and database / table  / column names to be in mixed
case.  That's the style in MS SQL Books Online, anyway. But it's a
rule most often observed in the breach.

Re: Horrible Orable Code

2005-08-30 15:24 • by titltn21
42421 in reply to 42410
El Duderino:

christoofar:
It's cr@p like this that reminds me why I prefer to use DB/2 and Microsoft SQL Server.


I'm sure there's some WTFers out there that can abuse those in equal degrees.


What's up with special cases for 1 and 2 space pads?  Bizarre.



I know ...  before yesterday this code looked like


  IF SPACES = 1
    THEN
        RETURN PAD;
    ELSIF SPACES = 2
    THEN
        RETURN PAD || PAD;


    ELSIF SPACES = 3
    THEN
        RETURN PAD || PAD || PAD;


    ELSIF SPACES = 4
    THEN
        RETURN PAD || PAD || PAD || PAD;


....


    ELSIF SPACES = 99
    THEN
        RETURN PAD || PAD || ..|| PAD||PAD || PAD || PAD;



But thanks to yesterday's WTF State Management post, someone actually tried to clean up this code getting rid of nearly all those if statements.  That has to be what is going on here.

Re: Horrible Orable Code

2005-08-30 15:26 • by A Wizard A True Star
42422 in reply to 42419

I love it. "I'm gonna call this custom function to do the right-padding. And just in case it doesn't pad it with the right number of spaces, I'm going to call the built-in function to do the right-padding all over again."


Translation: "Sometimes this RPAD_SPACES function works, and sometimes it doesn't, but I'll be damned if I spend the 30 minutes it would take to figure out why. So let me just do this double-checking thing and be done with it. Sure it's going to increase maintenance time in the future, but I am a highly-paid consultant and I will be long gone by then."


 

Re: Horrible Orable Code

2005-08-30 15:27 • by Mung Kee
The world definitely doesn't have enough complex padding
algorithms.  And if it does, what's one more?  Probably one
of those Oracle consultants, just padding his/her check.

Re: Horrible Orable Code

2005-08-30 15:29 • by A Wizard A True Star
42424 in reply to 42422

Wait, it's worse than I thought! It's calling the RPAD_SPACES function when the value is NULL. So they could have just hardcoded a string of nine or fifteen spaces, and not even called RPAD_SPACES in the first place.


 

Re: Horrible Orable Code

2005-08-30 15:35 • by JoeS
I've never worked with Oracle, and I spent about 15 seconds reading the link to the RPAD function.  I'm also totally taking syntax based on the WTF code.  But wouldn't this do the same thing?  Unless RPAD doesn't treat NULL values as blanks in which case you just need a simple IF to eliminate that case.

 

/* Preparing First and Last Name */

v_first_nm := RPAD(v_first_nm,9);

v_last_nm := RPAD(v_last_nm,15);

 

Re: Horrible Orable Code

2005-08-30 15:41 • by John Smallberries
42426 in reply to 42410
El Duderino:

christoofar:
It's cr@p like this that reminds me why I prefer to use DB/2 and Microsoft SQL Server.


I'm sure there's some WTFers out there that can abuse those in equal degrees.




True. I've seen christoofar's SQL...*shiver*


Re: Horrible Orable Code

2005-08-30 15:45 • by dubwai
42428 in reply to 42424

A Wizard A True Star:
Wait, it's worse than I thought! It's calling the RPAD_SPACES function when the value is NULL. So they could have just hardcoded a string of nine or fifteen spaces, and not even called RPAD_SPACES in the first place.


No way dude, you never know when 'space' will change.

Re: Horrible Orable Code

2005-08-30 15:47 • by El Duderino
42429 in reply to 42425
JoeS:

I've never worked with Oracle, and I spent about 15 seconds reading the link to the RPAD function.  I'm also totally taking syntax based on the WTF code.  But wouldn't this do the same thing?  Unless RPAD doesn't treat NULL values as blanks in which case you just need a simple IF to eliminate that case.

 

/* Preparing First and Last Name */

v_first_nm := RPAD(v_first_nm,9);

v_last_nm := RPAD(v_last_nm,15);


Without checking I couldn't say exactly how RPAD handles nulls but I know that


v_first_nm := RPAD(nvl(v_first_nm, ''), 9);

v_last_nm := RPAD(nvl(v_last_nm, ''), 15);

 

would deal with it just fine.

Re: Horrible Orable Code

2005-08-30 16:01 • by mck9
42433 in reply to 42429
>>

Without checking I couldn't say exactly how RPAD handles nulls but I know that


v_first_nm := RPAD(nvl(v_first_nm, ''), 9);

v_last_nm := RPAD(nvl(v_last_nm, ''), 15);

 

would deal with it just fine.

<<

 

...for suitable values of "fine".

 

It doesn't accomplish much to use the nvl function to convert a null value to a null value.  The code above is exactly equivalent to the code it is correcting, except possibly slower.

 

If the first argument to rpad() is null, then the value returned is null.  An empty string is just another way of representing null.

Re: Horrible Orable Code

2005-08-30 16:15 • by foxyshadis
42435 in reply to 42420
Anonymous:
kipthegreat:
Dumb question from someone who is certainly not
a brillant database genius... Is it normal for everything except
variable names to be in upper case?  That is really bad netiquette
(codiquette?) if you ask me....


Yes, it is typical for SQL reserved words / built-in functions to be in
upper case, and database / table  / column names to be in mixed
case.  That's the style in MS SQL Books Online, anyway. But it's a
rule most often observed in the breach.


Upper-case SQL always seemed like a throwback to the COBOL and FORTRAN
days to me; after all, they are contemporaries. Weird how that
particular habit has survived the lower_casing and then CamelCasing of
every other language, even if it is slowly dying.

Re: Horrible Orable Code

2005-08-30 16:17 • by JoeS
42436 in reply to 42433
So the correct version would be:

 


v_first_nm := RPAD(nvl(v_first_nm, ' '), 9);

v_last_nm := RPAD(nvl(v_last_nm, ' '), 15);

 

Using nvl() to convert any null value into a single space which then gets padded correctly by RPAD() to the proper length.  I can see why the original WTF code came about though.  Nested function calls are just too confusing.  The WTF is able to make the entire process much cleaner.

Re: Horrible Orable Code

2005-08-30 16:21 • by Otto

Here's a question for you, don't ponder it too hard...


How many drugs would you have to take, over what period of time, for this loop to actually make any sense?


        FOR LOOPVAR IN 1 ..SPACES - 2
        LOOP
            PADVAR := PAD || RPAD ( PAD
                                  , LOOPVAR + 1
                                  , ' ' );
        END LOOP;
        RETURN PADVAR;

Re: Horrible Orable Code

2005-08-30 16:35 • by Thuktun
42439 in reply to 42433
Anonymous:
An empty string is just another way of
representing null.
In some versions of Oracle, yes.  In
many (most?) other relational databases, no.

Re: Horrible Orable Code

2005-08-30 16:50 • by Alex Papadimoulis
42440 in reply to 42435

foxyshadis:
Upper-case SQL always seemed like a throwback to the COBOL and FORTRAN days to me; after all, they are contemporaries. Weird how that particular habit has survived the lower_casing and then CamelCasing of every other language, even if it is slowly dying.


It's actually the recommend way of doing it. Studies have shown that UPPERCASE keywords are much easier to read ...

SELECT col1, col2
  FROM sometable
 WHERE col3 = 'asdf'
   AND col4 IS NULL

Re: Horrible Orable Code

2005-08-30 17:20 • by omg-wtf

 


        FOR LOOPVAR IN 1 ..SPACES - 2
        LOOP
            PADVAR := PAD || RPAD ( PAD
                                  , LOOPVAR + 1
                                  , ' ' );
        END
        LOOP;
        RETURN PADVAR;


So, let me see if I have this straight - if I call RPAD_SPACES(99) (assume I have a head injury or whatever), I have the satisfaction of knowing that it constructed the return values for RPAD_SPACES(3) through RPAD_SPACES(98) (using RPAD, 'natch) and promptly tossed them aside.  Brillant!


 

Re: Horrible Orable Code

2005-08-30 17:29 • by El Duderino
42447 in reply to 42433
Mck9:
>>

An empty string is just another way of representing null.



I've only worked with Oracle version 8 and higher and I guarantee that '' <> NULL.

Re: Horrible Orable Code

2005-08-30 17:44 • by ammoQ
42448 in reply to 42433
Anonymous:
An empty string is just another way of representing null.


In Oracle, this is unfortunately true (at least in the versions I know)
and these NULL strings can give you many happy debugging hours, because




length('') is not 0, but NULL

lpad('', 5) is not '     ' but NULL

rpad('', 5) is not '     ' but NULL



Re: Horrible Orable Code

2005-08-30 17:48 • by ammoQ
42449 in reply to 42447
El Duderino:

I've only worked with Oracle version 8 and higher and I guarantee that '' <> NULL.






SQL*Plus: Release 8.1.7.0.0 - Production on Di Aug 30 23:46:03 2005



(c) Copyright 2000 Oracle Corporation.  All rights reserved.





Connected to:

Oracle9i Enterprise Edition Release 9.2.0.6.0 - 64bit Production

With the Partitioning option

JServer Release 9.2.0.6.0 - Production



SQL> select * from dual where '' is null;



D

-

X



SQL>




Maybe there are some settings to make '' <> NULL, but in a default install of anything from Oracle 6 to Oracle 9i, '' is NULL

Re: Horrible Orable Code

2005-08-30 17:54 • by Michael Casadevall
42450 in reply to 42447
I just need to say - it burns, it really, REALLY REALLY burns. This reminds me why I hate Oracle.

Re: Horrible Orable Code

2005-08-30 18:59 • by zephc
42453 in reply to 42440
"It's actually the recommend way of doing it. Studies have shown that UPPERCASE keywords are much easier "

That's why we have syntax highlighting now :) Probably just as quick to notice a color as it is to notice UPPERCASE, and it has the upshot that it doesn't make your code look like it was pulled from a vault circa the Nixon administration.

Re: Horrible Orable Code

2005-08-30 19:17 • by Sou|cutter
42456 in reply to 42440
Studies have shown that UPPERCASE keywords are much easier to read
Care
to cite any studies?  A quick google found studies supporting
mixed case, but I haven't seen anything supporting your claim.  As
far as I am aware there is no consensus on that topic outside of
specific contexts (and even then...).  I do know that for me
it is more difficult to read all uppercase than mixed or
lowercase.  When reading literature, mixed and all-lowercase is
the norm, so I guess my theory is that the eye is quicker to recognize
words presented in this, the normal manner.



Since I asked you for a link, I guess it's only fair to give one to
support my view also... http://www.tc-forum.org/topicus/ru28theu.htm

Re: Horrible Orable Code

2005-08-30 19:37 • by boxmonkey
42457 in reply to 42447
El Duderino:
Mck9:
>>

An empty string is just another way of representing null.



I've only worked with Oracle version 8 and higher and I guarantee that '' <> NULL.





I don't know about Oracle, but in MS SQL, NULL <> NULL pi

Re: Horrible Orable Code

2005-08-30 19:40 • by ammoQ
42458 in reply to 42456
UPPERCASE or lowercase is just a matter of personal style in PL/SQL,
since PL/SQL is not case sensitive. Anyway, some tools like TOAD have a
tendency to force their opinion about "why UPPERCASE is so much better"
to innocent programs.

Re: Horrible Orable Code

2005-08-30 19:42 • by boxmonkey
42459 in reply to 42435
foxyshadis:


Upper-case SQL always seemed like a throwback to the COBOL and FORTRAN
days to me; after all, they are contemporaries. Weird how that
particular habit has survived the lower_casing and then CamelCasing of
every other language, even if it is slowly dying.




Upper-case SQL is useful for a couple of reasons. As mentioned already,
it makes keywords stand out, and it makes it very easy to differentiate
between keywords and table and field names (as these are usually
lowercase). Finally, it is also useful because SQL frequently appears
mixed in with other programming languages, so it makes it easier to
tell when you're looking at code that will execute on the server, and
code that is part of your application. Imagine if T-SQL looked just
like C.

Re: Horrible Orable Code

2005-08-30 19:55 • by emurphy
42460 in reply to 42445
Anonymous:

So, let me see if I have this
straight - if I call RPAD_SPACES(99) (assume I have a head injury
or whatever), I have the satisfaction of knowing that it constructed
the return values for RPAD_SPACES(3) through RPAD_SPACES(98) (using
RPAD, 'natch) and promptly tossed them aside.  Brillant!


 




Actually, no, you won't have that satisfaction.


Exercise for the student:  Why not?




Re: Horrible Orable Code

2005-08-30 20:51 • by Oracle fanboy
42461 in reply to 42447
El Duderino:
Mck9:
>>

An empty string is just another way of representing null.



I've only worked with Oracle version 8 and higher and I guarantee that '' <> NULL.



this is almost a wtf on its own, '' is null is documented

SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Release 10.1.0.3.0 - Production
PL/SQL Release 10.1.0.3.0 - Production
CORE 10.1.0.3.0 Production
TNS for Linux: Version 10.1.0.3.0 - Production
NLSRTL Version 10.1.0.3.0 - Production

SQL> select nvl('', 'The empty string is NULL') wtf from dual;

WTF
------------------------
The empty string is NULL

Re: Horrible Orable Code

2005-08-30 21:00 • by DZ-Jay
42463 in reply to 42440
Alex Papadimoulis:

foxyshadis:
Upper-case
SQL always seemed like a throwback to the COBOL and FORTRAN days to me;
after all, they are contemporaries. Weird how that particular habit has
survived the lower_casing and then CamelCasing of every other language,
even if it is slowly dying.


It's actually the recommend way of doing it. Studies have shown that UPPERCASE keywords are much easier to read ...

SELECT col1, col2
  FROM sometable
 WHERE col3 = 'asdf'
   AND col4 IS NULL





Yes, the fifth dentist recommends it!



    dZ.



Re: Horrible Orable Code

2005-08-30 21:02 • by DZ-Jay
42464 in reply to 42419
Anonymous:
This code is brilliant. It optimizes out the function RPAD, which is
known to be slow, with faster code for one or two char strings.




Don't you mean Brillant?



    dZ.



Re: Horrible Orable Code

2005-08-30 21:36 • by Jeff S
42467 in reply to 42413
Anonymous:
ItsAllGeekToMe:

Oracle...........more like............Boracle....................right?


 



 


NO I WILL NOT MAKE OUT WITH YOU!


This girl wants to make out with me in the middle of class!



well done !

Re: Horrible Orable Code

2005-08-30 23:43 • by Alex Papadimoulis
42468 in reply to 42456
Anonymous:

Studies have shown that UPPERCASE keywords are much easier to read

Care to cite any studies?  A quick google found studies supporting mixed case, but I haven't seen anything supporting your claim.  As far as I am aware there is no consensus on that topic outside of specific contexts (and even then...).  I do know that for me it is more difficult to read all uppercase than mixed or lowercase.  When reading literature, mixed and all-lowercase is the norm, so I guess my theory is that the eye is quicker to recognize words presented in this, the normal manner.


I seem to have misplaced my references. They were in SQL PROGRAMMING STYLE by Joe Celko. I used to do lower case with syntax hilighting, but he had good evidence from recent studies (1997 I think) which demonstrated that SQL code with the upper case keywords was easier to read, understand, and therefore less error prone.


Consider that, especially when it comes to SQL, syntax hilighting is less avaiable than other languages. SQL Strings in applications for example.

Re: Horrible Orable Code

2005-08-31 00:17 • by Gary
42470 in reply to 42460
emurphy:
Anonymous:

So, let me see if I have this
straight - if I call RPAD_SPACES(99) (assume I have a head injury
or whatever), I have the satisfaction of knowing that it constructed
the return values for RPAD_SPACES(3) through RPAD_SPACES(98) (using
RPAD, 'natch) and promptly tossed them aside.  Brillant!


 




Actually, no, you won't have that satisfaction.


Exercise for the student:  Why not?







Because it will fall over on anything over 60 characters

PADVAR VARCHAR2 ( 60 );


Re: Horrible Orable Code

2005-08-31 00:24 • by foxyshadis
42471 in reply to 42468
Alex Papadimoulis:


to have misplaced my references. They were in SQL PROGRAMMING STYLE
by Joe Celko. I used to do lower case with syntax hilighting, but he
had good evidence from recent studies (1997 I think) which demonstrated
that SQL code with the upper case keywords was easier to read,
understand, and therefore less error prone.


Consider that, especially when it comes to SQL, syntax hilighting is
less avaiable than other languages. SQL Strings in applications for
example.



I can get behind that. It doesn't work for me, but if it's better on
average... Personally, I find too many letters in a row with straight
lines and the same metrics blends into line noise for me; lowercase
offers more variety for the pattern-matching, even without color. I
don't know eclipse highlights, haven't used sql in it yet, but I think
phped did and I know textpad and editplus don't.



On the other hand the whole second half of that is uppercase, I guess he was never taught that sql variables aren't keywords. =p



I love your spelling of highlightning. I know it's becoming more
common, but it always makes me think of h-dawg and his buddy's hilita
fix. The onion used to be so cool. =D

Re: Horrible Orable Code

2005-08-31 01:07 • by Dalamar
42472 in reply to 42460
Because those values, once calculated, are concatenated onto the
working value, not tossed aside. Each calculation is an integral part
of the final result.

Re: Horrible Orable Code

2005-08-31 02:44 • by felix
42474 in reply to 42472
You guys think style guides reccomending uppercase are a WTF? Wait
until you've programmed in Oberon. Since it's case sensitive, Wirth
(yes, the same guy who invented Pascal) had the 'brillant' idea to make
keywords all uppercase. Imagine tapping CAPS LOCK every 7 characters on
average. I gave up after a few hours. I mean, I like to try new things,
but that was too much.



Remember: things can always, ALWAYS get worse.



Re: Horrible Orable Code

2005-08-31 02:49 • by ammoQ
42475 in reply to 42468
Alex Papadimoulis:

I seem to have misplaced my
references. They were in SQL PROGRAMMING STYLE by Joe Celko. I used to
do lower case with syntax hilighting, but he had good evidence from
recent studies (1997 I think) which demonstrated that SQL code with the
upper case keywords was easier to read, understand, and therefore less
error prone.


Consider that, especially when it comes to SQL, syntax hilighting is
less avaiable than other languages. SQL Strings in applications for
example.





If you do that in PL/SQL, also writing the PL/SQL keywords (like
FUNCTION, IF, WHILE etc.) in uppercase, you end up with about 60% of
all words in uppercase, which is a PITA to read.

Some editors, e.g. Slickedit, provide SQL syntax highlighting for SQL code embedded in C.

Re: Horrible Orable Code

2005-08-31 03:08 • by Kuri
42476 in reply to 42453
My eyes have no cones you insensitive clod!

Re: Horrible Orable Code

2005-08-31 04:52 • by Shawn Smith
42479 in reply to 42457
In most RDBMSs (Oracle, MS SQL, MySQL, PostgreSQL, Ingres) null
<> null. Oracle is the only one I've seen where isnull('') =
true. It's great (NOT) when you're using trim(firstname) =
trim(:c-variable) in your where clause and wondering why your counts
are incorrect.

Re: Horrible Orable Code

2005-08-31 05:27 • by ammoQ
42483 in reply to 42475
The ''=NULL thingy has a "nice" side effect in Java: of course, the
JDBC driver will give you a null pointer instead of an empty string for
columns containting ''. A good oportunity to test how the system
handles null pointer exceptins.

Re: Horrible Orable Code

2005-08-31 05:28 • by bullestock
42484 in reply to 42472
Anonymous:
Because those values, once calculated, are concatenated onto the
working value, not tossed aside. Each calculation is an integral part
of the final result.




Bzzt, wrong:



PADVAR := PAD || RPAD ( PAD, LOOPVAR + 1, ' ' );


The only assignment is to PADVAR. PADVAR does not occur on the right hand side of the assignment.

Re: Horrible Orable Code

2005-08-31 07:04 • by alext
Alex Papadimoulis:

or even so I could share the obligatory Oracle Consultant link.





Did anyone else check this link out?



I especially like the bit where he says "both my great grandparents". I guess those red necks have close families

« PrevPage 1 | Page 2Next »

Add Comment