 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Msangali Guest
|
Posted: Sun Aug 28, 2005 3:42 pm Post subject: Would something like this be cool? |
|
|
Hi,
Do you think this
if SomethingIsNotOK then
begin
ShowMessage('It cannot be done');
Exit;
end;
//code continues...
could be better expressed this way
if SomethingIsNotOK
then ExitWithMessage('It cannot be done');
//code continues...
|
|
| Back to top |
|
 |
JohnE Guest
|
Posted: Sun Aug 28, 2005 4:06 pm Post subject: Re: Would something like this be cool? |
|
|
This is similar
If SomethingIsNotOk then
Raise Exception.Create('It cannot be done');
|
|
| Back to top |
|
 |
Jarle Stabell Guest
|
Posted: Sun Aug 28, 2005 4:13 pm Post subject: Re: Would something like this be cool? |
|
|
JohnE wrote:
| Quote: | This is similar
If SomethingIsNotOk then
Raise Exception.Create('It cannot be done');
|
I don't know about similar, but at least it is superior. :-)
Cheers,
Jarle
|
|
| Back to top |
|
 |
Yannis Guest
|
Posted: Sun Aug 28, 2005 4:24 pm Post subject: Re: Would something like this be cool? |
|
|
Msangali wrote:
| Quote: | Hi,
Do you think this
if SomethingIsNotOK then
begin
ShowMessage('It cannot be done');
Exit;
end;
//code continues...
could be better expressed this way
if SomethingIsNotOK
then ExitWithMessage('It cannot be done');
//code continues...
|
I consider this code as a patch I do prefare
If SomethingIsOK then
Code
else
showMessage('It cannot be done');
This is probably because of my habbits of programming than anything
else but I find easier to follow the code when there is a single exit
point of a procedure.
as for your question it feels the same to me eitherway.
Regards
Yannis.
|
|
| Back to top |
|
 |
Richard Foersom Guest
|
Posted: Sun Aug 28, 2005 5:19 pm Post subject: Re: Would something like this be cool? |
|
|
Yannis wrote:
| Quote: | This is probably because of my habbits of programming than anything
else but I find easier to follow the code when there is a single exit
point of a procedure.
|
Thanks Yannis, I agree. Exit smells like goto in disguise.
Doei RIF
|
|
| Back to top |
|
 |
Jarle Stabell Guest
|
Posted: Sun Aug 28, 2005 5:54 pm Post subject: Re: Would something like this be cool? |
|
|
Robert Giesecke wrote:
| Quote: | JohnE wrote:
This is similar
If SomethingIsNotOk then
Raise Exception.Create('It cannot be done');
I don't know about similar, but at least it is superior. :-)
Yeah, whereas I'd prefer something like this:
if SomethingCannotBeDone then
raise CannotBeDoneException.Create();
|
If this situation is so frequent that it warrants its own exception class,
I'd probably implement a produre to cast the exception, making the code
ending up like:
if SomethingCannotBeDone then
RaiseCannotBeDone('because');
But then, ECannotBeDone sounds like an abstract class to me, with subclasses
like ENotImplementedYet, EProgrammerMadeStupidBug, EUserMadeStupidAction,
ESomethingReallyStrangeHappened etc. ;-)
Cheers,
Jarle
|
|
| Back to top |
|
 |
Msangali Guest
|
Posted: Sun Aug 28, 2005 6:16 pm Post subject: Re: Would something like this be cool? |
|
|
Hi,
I know this is a matter of taste, but I like "exit" because with it the code
doesn't slide too much to the right and then I have more space on each line.
Besides, using exit, I often can put all the necessary tests at the top of a
procedure, and, only after them, I write the main branch. IMHO, this make
the main branch more readable than putting it in a dense If-Then-Else
structure.
Regards,
Marco
"Yannis" <None (AT) noware (DOT) non> wrote
| Quote: | Msangali wrote:
Hi,
Do you think this
if SomethingIsNotOK then
begin
ShowMessage('It cannot be done');
Exit;
end;
//code continues...
could be better expressed this way
if SomethingIsNotOK
then ExitWithMessage('It cannot be done');
//code continues...
I consider this code as a patch I do prefare
If SomethingIsOK then
Code
else
showMessage('It cannot be done');
This is probably because of my habbits of programming than anything
else but I find easier to follow the code when there is a single exit
point of a procedure.
as for your question it feels the same to me eitherway.
Regards
Yannis.
|
|
|
| Back to top |
|
 |
Msangali Guest
|
Posted: Sun Aug 28, 2005 6:22 pm Post subject: Re: Would something like this be cool? |
|
|
Hi,
Why is it superior? IMHO, creation of a exception is a much more costly
operation than just exit. And,.AFAIK, exception handling should be used to,
well, handle exceptions, not predictable branches of code.
Regards,
Marco
"Jarle Stabell" <jarle (AT) remove_stuff_dlogikk_spam_kills_email (DOT) com> wrote in
message news:4311e28d (AT) newsgroups (DOT) borland.com...
| Quote: | JohnE wrote:
This is similar
If SomethingIsNotOk then
Raise Exception.Create('It cannot be done');
I don't know about similar, but at least it is superior. :-)
Cheers,
Jarle
|
|
|
| Back to top |
|
 |
Erwien Saputra Guest
|
Posted: Sun Aug 28, 2005 6:33 pm Post subject: Re: Would something like this be cool? |
|
|
Msangali wrote:
| Quote: |
Why is it superior? IMHO, creation of a exception is a much more
costly operation than just exit. And,.AFAIK, exception handling
should be used to, well, handle exceptions, not predictable branches
of code.
|
This is an error handling, you handle an error or an undesireable
condition by raising an exception.
The exception handling is done by the caller of your method.
The code was not clear whether the code was part of UI classes or
probably part of some business object classes.
If the code is part of business object class, the UI objects can decide
whether to display the message, or to do something with it.
It may not be a good idea to display a message window, as it requires
user interaction to close the message and to exit. Also, the
application may not have UI or should not interract with the user
(running behind the screen and store the errors to the log).
Wien.
--
http://blogs.slcdug.org/esaputra
Delphi Setting Manager - http://www.codeline.net/DelphiSettingManager
Reference Tool - http://www.codeline.net/RefTool
|
|
| Back to top |
|
 |
Robert Giesecke Guest
|
Posted: Sun Aug 28, 2005 6:36 pm Post subject: Re: Would something like this be cool? |
|
|
| Quote: | JohnE wrote:
This is similar
If SomethingIsNotOk then
Raise Exception.Create('It cannot be done');
I don't know about similar, but at least it is superior. :-)
|
Yeah, whereas I'd prefer something like this:
if SomethingCannotBeDone then
raise CannotBeDoneException.Create();
@Msangali
I don't know what useful something like this could be, as I doubt
ShowMessage has any real purose. ;-)
--- posted by geoForum on http://delphi.newswhat.com
|
|
| Back to top |
|
 |
Msangali Guest
|
Posted: Sun Aug 28, 2005 6:59 pm Post subject: Re: Would something like this be cool? |
|
|
Hi, Erwien,
Sorry if I wasn't clear, I was talking about a code in UI - a button, for
instance. That's why I showed the message. The ideia is to warn the user
that something didn't happened as expected. If it was a component code or
something more down-to-the guts like that, of course, an exception would be
the first option.
Exceptions are overused sometimes IMHO. I mean, once I saw a code like this
(IIRC)
try
x:=teta/gama;
except
on EDivByZero do x:=0;
end;
If you call this routine and gama is 0, the overhead is huge due to the
exception raised, AFAIK. This is one reason why I always try to use
exceptions only for "external" problems (I/O, databases) and avoid it on
other situations. Altough, of course, in this UI example, that would be no
impact in performance.
Regards,
Marco
* Is it only me that uses "exit"? :-)
|
|
| Back to top |
|
 |
Ivan Cruz Guest
|
Posted: Sun Aug 28, 2005 7:27 pm Post subject: Re: Would something like this be cool? |
|
|
Msangali wrote:
| Quote: | Hi,
Do you think this
if SomethingIsNotOK then
begin
ShowMessage('It cannot be done');
Exit;
|
Oh my god!!! GOTOs are so sixties!!!
http://www.acm.org/classics/oct95/
Ivan.
|
|
| Back to top |
|
 |
Michael Anonymous Guest
|
Posted: Sun Aug 28, 2005 7:43 pm Post subject: Re: Would something like this be cool? |
|
|
Msangali wrote:
| Quote: | Do you think this
if SomethingIsNotOK then
begin
ShowMessage('It cannot be done');
Exit;
end;
//code continues...
could be better expressed this way
if SomethingIsNotOK
then ExitWithMessage('It cannot be done');
//code continues...
|
Yes.
|
|
| Back to top |
|
 |
Michael Anonymous Guest
|
Posted: Sun Aug 28, 2005 7:48 pm Post subject: Re: Would something like this be cool? |
|
|
Ivan Cruz wrote:
The original poster used an exit statement not a goto.
In any event,
I still use gotos for a few things.
But I'm one of those types that
thinks marijuana might taste good on pizza.
After all, some rules are made to be broken.
|
|
| Back to top |
|
 |
Erwien Saputra Guest
|
Posted: Sun Aug 28, 2005 9:00 pm Post subject: Re: Would something like this be cool? |
|
|
Msangali wrote:
| Quote: | Exceptions are overused sometimes IMHO. I mean, once I saw a code
like this (IIRC)
|
I tend to use exception whenever applicable. You may want to check
Raymond Chen's blog post about exception. It is thought provoking.
http://blogs.msdn.com/oldnewthing/archive/2005/01/14/352949.aspx
About the exception overhead, I know exception is expensive, but will I
sacrifice a possibly more maintainable code for saving some CPU cycles?
Unless I can profile it and see significant difference, I won't bother.
| Quote: | * Is it only me that uses "exit"?
|
No. I do it too. :)
I find Exit if used properly makes code easier to read.
For example:
procedure TForm.A (const SomeString : string);
begin
if SameText (SomeString, 'A') then begin
DoSomethingA;
Exit;
end;
if SameText (SomeString, 'B') then begin
DoSomethingB;
Exit;
end;
if SameText (SomeString, 'C') then begin
DoSomethingC;
Exit;
end;
if SameText (SomeString, 'D') then begin
DoSomethingD;
Exit;
end;
end;
I pulled that example from nowhere. IMO, that is much cleaner than
nested if..else. I do not use Exit if it nested deeper than above.
Wien.
--
http://blogs.slcdug.org/esaputra
Delphi Setting Manager - http://www.codeline.net/DelphiSettingManager
Reference Tool - http://www.codeline.net/RefTool
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|