Subject:
|
Re: Are there GOTO statements in NQC?
|
Newsgroups:
|
lugnet.robotics
|
Date:
|
Thu, 20 Nov 2003 23:35:51 GMT
|
Original-From:
|
T. Alexander Popiel <POPIEL@antispamWOLFSKEEP.COM>
|
Viewed:
|
1114 times
|
| |
| |
In message: <3FBD39FE.3020300@airmail.net>
Steve Baker <sjbaker1@airmail.net> writes:
> > >
> > > "A function should have one and only one exit point" :-)
>
> The thing everyone has forgotton about the "one exit point" rule
> is that it applies to MACHINE CODE PROGRAMMING - not high level
> languages.
>
> This single misunderstanding has resulted in more convoluted and
> illegible code than I care to remember!
>
> When writing in machine code, you allocate yourself some stack space
> for local variables at the top of the function and save whatever
> hardware registers you are using onto the stack - and it's crucially
> important that you free up that space and restore the saved registers
> before you return from the function.
>
> If the function has many exit points - and someone comes along and
> adds another locals - or saves an additional register - then it's
> crucially important to find each and every exit point and change the
> code to reflect this new stack usage.
>
> It's ALARMINGLY easy to forget one or more exit points - resulting
> in flakey code. Hence the very good rule to have just one exit point.
This exact same argument can be applied to high-level languages
where you dynamically allocate memory or other resources that need
to be released before leaving the function. While I agree that
forcing people to have only one exit point at all times is overzealous,
there are times when it's quite appropriate... and amusingly enough, at
those times, I also find goto quite helpful.
Consider:
int my_function ()
{
int result = FALSE;
allocate_buffer;
open_file;
save_visual;
create_gfx_context;
if ( problem_exists ) goto exit_sequence;
do_some_work;
if ( some_other_problem_exists ) goto exit_sequence;
do_more_work;
result = TRUE;
exit_sequence:
close_file;
free_buffer;
restore_visual;
free_gfx_context;
return result;
}
Some would argue that this just means that my_function() should be
broken into another_function_which_allocates_and_frees(), calling
my_new_function(with_everything_passed_in_as_args)... but I tend to
think that's silly and leads to horribly deep stackframes.
> It's alarming that some teachers of programming are still teaching
> the "only one exit" rule for high level languages!
I think that it's merely another aspect of overgeneralization.
It's useful sometimes, but not at other times.
- Alex
|
|
Message has 1 Reply: | | Re: Are there GOTO statements in NQC?
|
| (...) Which is why we have C++. With memory constructors and destructors, quite complex resources can be automatically and cleanly free'd up on exit from a function. ---...--- Steve Baker ---...--- HomeEmail: <sjbaker1@airmail.net> WorkEmail: (...) (21 years ago, 20-Nov-03, to lugnet.robotics)
|
Message is in Reply To:
| | Re: Are there GOTO statements in NQC?
|
| (...) The thing everyone has forgotton about the "one exit point" rule is that it applies to MACHINE CODE PROGRAMMING - not high level languages. This single misunderstanding has resulted in more convoluted and illegible code than I care to (...) (21 years ago, 20-Nov-03, to lugnet.robotics)
|
19 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
This Message and its Replies on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|