-
Website
http://awads.net/wp/ -
Original page
http://awads.net/wp/2006/02/16/whats-in-a-label/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
jgarry
3 comments · 1 points
-
Andy C
22 comments · 47 points
-
dahowlett
1 comment · 2 points
-
Don Seiler
9 comments · 1 points
-
davidhaimes
4 comments · 3 points
-
-
Popular Threads
You missed what I think is a genuine use for labels: identifying nested loops.
<pre>
<<dept>>
FOR d_rec IN ( SELECT deptno FROM scott.dept ) LOOP
do_whatever;
<<emp>>
FOR e_rec IN (SELECT empno FROM scott.emp
WHERE deptno = d_rec.deptno ) LOOP
blah(e-rec.empno);
END LOOP emp;
clean_up;
END LOOP dept;
</pre>
And what's wrong with GOTOs anyway? haven't they been officially considered not harmful for sometime now? I take it you never raise exceptions either.
Cheers, APC
Unfortunately there are lots of developers insisting to use goto's. It is a tradition some people cannot give up.
It was imposible to get a degree for students using goto's in their assignments in my college years.
I use labels in 3 situations: Loops, case statement and begin end block inside a procedure/function. I like that you can end the case or the begin with a label like this:
<pre>
<<myBlock>>
begin
x := y + 1;
end myBlock;
</pre>
<pre>
<<myCase>>
case xx
when ..
end case myCase;
</pre>
I don't use GOTOs, but I do use labels for loops sometimes. It's really a clarity thing. If I have nested loops, the labels are handy to see which loop has ended, although a comment would do the same. I also use labels if I'm planning to exit a loop prematurely.
Cheers
Tim...
> And what's wrong with GOTOs anyway? haven't they been officially considered not harmful for sometime now?
They have? Nobody tells me anything.
I take it you never raise exceptions either
I take it you use GOTO to raise exceptions ;)
I'm not a developer, but the term GOTO reminds me of writing BASIC on my Commodore 64. I can't believe it still exists, besides in Windows batch files.
I take it you use GOTO to raise exceptions
Of course not. I was merely pointing out that raising an exception is a special case of GOTO. Particularly given a case like this:
<pre>
....
do_something_a;
do_something_b;
EXCEPTION
WHEN others THEN blah;
</pre>
If do_something_a raises an exception to be handle by the calling program then essentially there is an invisible GOTO that bypasses do_something_b.
Before I started in Oracle I worked on a particular flavour of COBOL that didn't support conditional ERFORMs. GOTOs were a way of life. So I know bad use of GOTO create spaghetti code. I also know that well structured GOTOs have their place.
Of course, Oracle PL/SQL has a panoply of control structures and I have never had to use a GOTO since I stopped COBOLing. Indeed the other day I found myself with a LOOP that had more than one EXIT statement and felt unclean.
And what's wrong with GOTOs anyway? haven't they been officially considered not harmful for sometime now?
They have? Nobody tells me anything.
Actually I was thinking about the NTK newsletter and not some computer science pronouncement. I don't think Dijkstra has revoked his fatwa. Still, if you've ever had to follow the possible paths through a function that has over a dozen RETURN statements you might find yourself pining for a nice GOTO exit_point.
GOTOs are like triggers: there's nothing inherently wrong with the construct, it's simply the potential for abuse that renders their use suspect. Simply avoiding the use of GOTO statement does not make our code well-structured.
[OT] I don't think Dijkstra has revoked his fatwa.
I don't think he will ever be able to revoke it given that he passed away few years back :(
Label could be an elegant solution to exit 2 nested loop
<pre>
SQL> Declare
2 LN$I pls_integer := 0 ;
3 LN$J pls_integer := 0 ;
4 Begin
5 <<boucle1>>
6 Loop
7 LN$I := LN$I + 1 ;
8 Loop
9 LN$J := LN$J + 1 ;
10 dbms_output.put_line( to_char( LN$I ) || ',' || to_char( LN$J ) ) ;
11 EXIT boucle1 WHEN LN$J > 3 ;
12 End loop ;
13 End loop ;
14 End ;
15 /
1,1
1,2
1,3
1,4
Procédure PL/SQL terminée avec succès.
</pre>
APC: I was merely pointing out that raising an exception is a special case of GOTO.
Ah! I see. when you raise an exception, you basically "go to" the corresponding exception block.
APC: Before I started in Oracle I worked on a particular flavour of COBOL that didn't support conditional ERFORMs. GOTOs were a way of life.
I started my programming career with COBOL, that was in 1992. COBOL and GOTO were best buddies.
In PL/SQL, I just cannot see a real need to use labels and GOTO's. Labels can be easily replaced with comments. GOTO's can be easily replaced with many other programming controls that provide a much better clarity to the program logic.