PC Review


Reply
Thread Tools Rate Thread

Best way to exit (newbie)

 
 
Craig
Guest
Posts: n/a
 
      21st Sep 2005
I have some code

Is the finish statement the best to use if method is of the type void


private void btnSave_Click(object sender, System.EventArgs e)
{

//If something happens here exit code block
if((string)Session["RecordID"] == "0")
{
//Execute code here
}
else
{
Goto Finish: //Exit routine
}
//More code here

Finish:
{
}

}


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      21st Sep 2005
Craig <(E-Mail Removed)> wrote:
> I have some code
>
> Is the finish statement the best to use if method is of the type void


No - just return, with no return value:

return;

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Scott Roberts
Guest
Posts: n/a
 
      21st Sep 2005

"Craig" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> I have some code
>
> Is the finish statement the best to use if method is of the type void
>
>
> private void btnSave_Click(object sender, System.EventArgs e)
> {
>
> //If something happens here exit code block
> if((string)Session["RecordID"] == "0")
> {
> //Execute code here
> }
> else
> {
> Goto Finish: //Exit routine
> }
> //More code here
>
> Finish:
> {
> }
>
> }



Well, it appears that the best thing to do would be to move "//More code
here" up underneath "//Execute code here". However, it appears that you are
simply trying to validate the value of "RecordID" prior to doing *anything*.
In that case, I've seen arguements for code along these lines:

private void btnSave_Click(object sender, System.EventArgs e)
{
// Check for valid method pre-conditions.
if ( (string)Session["RecordID"] != "0" )
return;

// Execute code here.
}


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      21st Sep 2005
Scott Roberts <(E-Mail Removed)> wrote:
> Well, it appears that the best thing to do would be to move "//More code
> here" up underneath "//Execute code here". However, it appears that you are
> simply trying to validate the value of "RecordID" prior to doing *anything*.
> In that case, I've seen arguements for code along these lines:
>
> private void btnSave_Click(object sender, System.EventArgs e)
> {
> // Check for valid method pre-conditions.
> if ( (string)Session["RecordID"] != "0" )
> return;
>
> // Execute code here.
> }


Indeed - although I'd put braces round the return statement, never
using an "if" without braces

I know the reasoning behind the ideology of only having a single exit
point from any method, but:

a) There are almost always potentially multiple exit points if you
consider exceptions as exit points

b) It quite often gets in the way of readability to have to
artificially plough on to the end of the method when the path through
it has effectively finished. In particular, it can lead to code being
indented much further than it would otherwise need to be, occasionally
leaving you wondering why.

c) When methods are kept shorts, it should still be fairly easy to find
all the normal exit points anyway.

(I know you weren't promoting this ideology - I just thought it worth
mentioning.)

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Scott Roberts
Guest
Posts: n/a
 
      21st Sep 2005

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> (I know you weren't promoting this ideology - I just thought it worth
> mentioning.)


I do promote the "single exit point" ideology. However, like my other
ideologies, I seem to break it on occassion. :-)


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      21st Sep 2005
Scott Roberts <(E-Mail Removed)> wrote:
> > (I know you weren't promoting this ideology - I just thought it worth
> > mentioning.)

>
> I do promote the "single exit point" ideology. However, like my other
> ideologies, I seem to break it on occassion. :-)


I suspected you might follow it usually given the "I've seen arguments
for" line, but respected the fact that you hadn't actually been pushing
it in this case

Personally, I think it was much more important in C where you really
needed to make sure you'd cleaned up all the memory you'd allocated
etc.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Steve Walker
Guest
Posts: n/a
 
      21st Sep 2005
In message <(E-Mail Removed)>, Jon Skeet
<?@pobox.com.invalid> writes
>Scott Roberts <(E-Mail Removed)> wrote:
>> > (I know you weren't promoting this ideology - I just thought it worth
>> > mentioning.)

>>
>> I do promote the "single exit point" ideology. However, like my other
>> ideologies, I seem to break it on occassion. :-)

>
>I suspected you might follow it usually given the "I've seen arguments
>for" line, but respected the fact that you hadn't actually been pushing
>it in this case
>
>Personally, I think it was much more important in C where you really
>needed to make sure you'd cleaned up all the memory you'd allocated
>etc.


In moments of zealotry I've often been drawn to the

string Foo(int x)
{
string result;
switch(x)
{
case 1:
result = "one"
break;
case 2:
result = "two"
break;
.....
}
return result;
}

approach for the sake of a single exit point, but I'm not sure that it
always makes for more readable code. At least with a return statement
you know that there's nothing further to read for that particular code
path.

--
Steve Walker
 
Reply With Quote
 
=?Utf-8?B?RGFuaWVsIEppbg==?=
Guest
Posts: n/a
 
      21st Sep 2005
>
> Personally, I think it was much more important in C where you really
> needed to make sure you'd cleaned up all the memory you'd allocated
> etc.
>


I've run into something similar in C#. we employ a transaction system
similar to .NET 2.0's TransactionScope within a 'using' block. and there had
been times where multiple exit points in a method sometimes cause problem for
us because someone, somewhere inevitably forgets to properly vote before
leaving the scope and causing a rollback that's quite difficulty to track
down at times.
 
Reply With Quote
 
=?Utf-8?B?SmF2YW1hbjU5?=
Guest
Posts: n/a
 
      22nd Sep 2005
I fully agree, Jon, (and others). When I started professional programming I
had some hesitation in using "break" and "return" statements. I got over that
hesitation, and have never looked back. For years i've used them as much as I
want, and never regretted it. They are usually more readable than flags and
nested conditionals. When we see a "return" we know for sure that that is the
end of the method.

 
Reply With Quote
 
Scott Roberts
Guest
Posts: n/a
 
      22nd Sep 2005

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> c) When methods are kept shorts, it should still be fairly easy to find
> all the normal exit points anyway.



Since this came up in another post, I just wanted to point out the "return"
does not mark an exit point.

public void MyUnnecessarilyComplexMethod()
{
// Do some stuff.
try
{
// Try to do some more stuff.

if (some_condition)
return; // This is not the end of execution in this method!!!

// Do some other stuff.
}
finally
{
// Do some finally stuff.
// This executes even if you "return" above.
}
}


Of course, this functionality is defined by the language and should be "well
known".


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie - Exit w/o updating Bumbino Microsoft Access 8 21st Jan 2008 12:23 AM
Newbie - Exit w/o update Bumbino Microsoft Access Form Coding 1 18th Jan 2008 11:11 PM
Code to Exit Web App and Exit Internet Explorer =?Utf-8?B?U2FuZHk=?= Microsoft ASP .NET 6 4th Aug 2005 09:28 PM
Outlook.exe posts an error on exit that it needs to exit. How do. =?Utf-8?B?SmFjayBIYWVzbHk=?= Microsoft Outlook Installation 0 4th Mar 2005 04:17 AM
The exit X is greyed out. I cannot exit Excel...in 2000 and now i. =?Utf-8?B?S2l0dHlv?= Microsoft Excel Misc 1 28th Sep 2004 06:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:29 AM.