Tuesday, February 12, 2008

Why do we need to follow coding standerds?

Why do we need to follow coding standerds? Over a period of time i have been tought to follow certain coding standards. Mainly like

Local variable - Starts with l

Global variables - Starts with g
Parameter - Starts with p

and so on...

Are these standards just to make our code look beautiful or are thes things our life savers. lets see a small example....

sql> create table hx_tmp(hx_val INTEGER);
Table created.

sql> insert into hx_tmp values (1);
1 row created.

sql> insert into hx_tmp values (2);
1 row created.

sql> commit;
Commit complete.

sql> create or replace function hx_f (hx_val in integer) return integer
2 as
3 hx_out INTEGER := 0;
4 begin
5 select hx_val into hx_out from hx_tmp where hx_val = hx_val;
6
7 return hx_out;
8 end;
9 /
Function created.

sql> variable a number

sql> exec :a := hx_f(2);
BEGIN :a := hx_f(2); END;
*
ERROR at line 1:
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "SYSADM.HX_F", line 5
ORA-06512: at line 1

Oooooops.....

If you where expecting to get a result think again. This is all because i dint follow the coding standard that i was supposed to follow. So i was expecting that input value 2 will be passed to the SQL. But it did the other way. Its all because of bad way of coding.

So conclution....

Coding standards is not just to make your code look beautiful or understandable or maintanable or.... or..... or.... but also to make your code bug free.

No comments: