From: "Jim Langston" <tazmaster@rocketmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
Date: Sat, 20 Oct 2007 18:08:11 -0700 [thread overview]
Message-ID: <2ExSi.169$GB1.96@newsfe06.lga> (raw)
In-Reply-To: 1192913158.922454.108100@k35g2000prh.googlegroups.com
<gnuist006@gmail.com> wrote in message
news:1192913158.922454.108100@k35g2000prh.googlegroups.com...
>I have some code like this:
>
> (if (test)
> (exit)
> (do something))
>
>
> or
>
> (if (test)
> ( do something)
> (exit))
>
>
> Various levels of nestings.
>
> I have several questions, basic to sophisticated.
>
> (1) What is the lisp equivalent idiom for (exit) as in bash or
> in C.
Your message post is asking "How to exit out of a function..." yet you are
showing code that almost exits out of the program. exit() would do that.
If you wish to exit out of the function then the correct keyword is return.
Such as:
if (condition)
return;
(do something)
if ( condition )
(do something )
return;
return can return a value if the function returns a value, such as
return 1;
return foo;
return bar();
etc..
> (2) What is the best practice to handle this kind of problems?
It depends on coding style. There is some coding style that states that any
function will only have one return point. In which case you would do:
if ( condition )
( do something )
return;
Personally, I chose to return early if it's what I would consider an error
condition.
if ( condition )
return NULL;
( do something )
return Value;
There really is no best way, it depends on coding standard and how easy the
code is to write, read and maintain.
> (3) What is the intermediate practice to handle this kind of
> problems.
Again, it depends on the problems. At different times in my career I have
gone with returning whenever I've wanted to to returning only at the very
end.
I find that huge if statments, however, make my code harder to write, read
and maintain, so I will check for conditions early and return early if I
can. I try not to return in the middle of a function, but only at the top
or bottom. But it depends on the situation.
> NOTE: I am really afraid of try-catch-throw. I have never been
> able to understand it since it does not exist in C and I cant
> really visualize the construct in terms of C. That is what my
> brain can process. If you understand it so well, you can show
> me how one would really implement that kind of construct in
> C and then by extension I can see that kind of program flow
> in LISP. Whether its imperative programming or functional,
> beneath there is program counter and assembly. C is close
> to machine so much that it is almost assembly. So understanding try-c-
> t in C is equivalent to understanding at
> the level of machine language.
>
> I therefore take the liberty to crosspost in C and C++ groups.
As for try...catch blocks, I try to reserve those only for actual errors.
One reason I have found I need to use them when a function is supposed to
return a reference to an object, and that object doesn't exist. I have to
return something, and something is not good enough. So I get around it by
throwing out of the function.
(Untested code)
Player& FindPlayer( const std::string& Name, std::map<std::string, Player> )
{
std::map<std::string, Player>::iterator it = Player.find( Name );
if ( it == Player.end() )
{
// Player was not found in map. Have to return something. Lets just
throw.
throw std::exception("Player not found");
}
return (*it).second;
}
int main()
{
// ...
try
{
Player ThisPlayer& = FindPlayer( SomePlayer );
// use ThisPlayer
}
castch ( std::exception e )
{
std::cout << "Player " << SomePlayer << " not found.\n";
}
}
next prev parent reply other threads:[~2007-10-21 1:08 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-10-20 20:45 How to exit out of a function ? what is try-catch-throw in terms of Program Counter gnuist006
2007-10-20 20:57 ` gnuist006
2007-10-20 22:55 ` Alf P. Steinbach
2007-10-23 18:06 ` gnuist006
2007-10-23 19:53 ` Alf P. Steinbach
2007-11-04 23:02 ` David Thompson
2007-11-05 5:07 ` Alf P. Steinbach
2007-11-05 5:59 ` Alf P. Steinbach
2007-12-02 23:20 ` David Thompson
2007-12-02 23:51 ` Alf P. Steinbach
2007-10-21 1:08 ` Jim Langston [this message]
2007-10-21 2:30 ` Keith Thompson
2007-10-21 10:44 ` Kenny McCormack
2007-10-21 10:55 ` santosh
2007-10-21 12:26 ` Richard Heathfield
2007-10-21 22:35 ` Mark McIntyre
2007-10-21 12:09 ` Malcolm McLean
2007-10-21 15:23 ` Kenny McCormack
2007-10-21 15:54 ` santosh
2007-10-21 17:39 ` Malcolm McLean
2007-10-21 21:53 ` Kenny McCormack
2007-10-21 8:03 ` Malcolm McLean
2007-10-21 16:07 ` abhy
2007-10-21 17:43 ` santosh
2007-10-23 9:04 ` Joel Yliluoma
2007-10-23 16:33 ` Stefan Monnier
2007-10-23 16:44 ` gnuist006
2007-10-23 16:45 ` Victor Bazarov
2007-10-24 1:06 ` Stefan Monnier
2007-10-23 16:54 ` gnuist006
2007-10-23 17:14 ` Alf P. Steinbach
2007-10-24 1:09 ` Stefan Monnier
2007-10-24 0:02 ` Joel Yliluoma
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='2ExSi.169$GB1.96@newsfe06.lga' \
--to=tazmaster@rocketmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).