To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 21650
21649  |  21651
Subject: 
Re: Are there GOTO statements in NQC?
Newsgroups: 
lugnet.robotics
Date: 
Thu, 20 Nov 2003 22:02:38 GMT
Original-From: 
Steve Baker <sjbaker1@airmail.net%spamless%>
Viewed: 
1030 times
  
Kevin L. Clague wrote:

I'm guessing you have a similar view on the statement:

"A function should have one and only one exit point" :-)


I think it is a stupid rule!

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.

However, in high level languages like C, C++ and NQC, the compiler
handles all of this ugliness completely automaticaly - so you can
have as many return points as you need to make the code readable.

The canonical example of how this makes code easier to read is a
typical function that checks some conditions to see if it can do
some work and returns TRUE if it can - and FALSE if it cannot:


int my_function ()
{
   if ( problem_exists ) return FALSE ;

   if ( some_other_problem_exists ) return FALSE ;

   do_some_work ;
   return TRUE ;
}


If you erroneously apply the 'exit in one place' rule, you end up
with:

int my_function ()
{
   int return_code ;

   if ( problem_exists )
     return_code = FALSE ;
   else
     if ( some_other_problem_exists )
       return_code = FALSE ;
     else
     {
       do_some_work ;
       return_code = TRUE ;
     }

   return return_code ;
}

...which is much harder to understand.   It's better to clear out
and finally resolve as many error conditions as possible early on
in the function so the reader can mentally cross off what happens
under those fault conditions and can concentrate on the meat of the
code without having to think about them anymore.

It's alarming that some teachers of programming are still teaching
the "only one exit" rule for high level languages!

Educator - educate thyself!

---------------------------- Steve Baker -------------------------
HomeEmail: <sjbaker1@airmail.net>    WorkEmail: <sjbaker@link.com>
HomePage : http://www.sjbaker.org
Projects : http://plib.sf.net    http://tuxaqfh.sf.net
            http://tuxkart.sf.net http://prettypoly.sf.net
-----BEGIN GEEK CODE BLOCK-----
GCS d-- s:+ a+ C++++$ UL+++$ P--- L++++$ E--- W+++ N o+ K? w--- !O M-
V-- PS++ PE- Y-- PGP-- t+ 5 X R+++ tv b++ DI++ D G+ e++ h--(-) r+++ y++++
-----END GEEK CODE BLOCK-----



Message has 1 Reply:
  Re: Are there GOTO statements in NQC?
 
(...) 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 (...) (21 years ago, 20-Nov-03, to lugnet.robotics)

Message is in Reply To:
  Re: Are there GOTO statements in NQC?
 
(...) Ralph, I think it is a stupid rule! (...) Kevin (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
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR