 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

November 1st, 2006, 02:47 AM
|
|
Authorized User
|
|
Join Date: Jun 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Optimization
Hi,
which code of the following is more optimized, or is there a better way?
public void SomeFunction()
{
if(true)
{
//do something
return;
}
else
{
return; //the meaning of exit when false
}
}
or this way :
public void SomeFunction()
{
if(!true)
{
return;
}
//do something
return;
}
|
|

November 1st, 2006, 03:04 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
It doesn't matter. The check for true or false still has to be made one time.
But obviously, this may be a little faster:
Code:
public void SomeFunction()
{
//do something
}
Since true is always true, there's no need to check ;)
But I guess that wasn't want you wanted to hear.... ;)
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
|
|

November 1st, 2006, 03:18 AM
|
|
Authorized User
|
|
Join Date: Jun 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
come on ma friend what i mean that is not a real code case,
true means if the conditions become true.
|
|

November 1st, 2006, 03:59 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I know that; that's why I added a smiley.
However, the first part of my reply still holds true: it doesn't matter wether you check for true or false in this case. In both cases, a simple if check has to be made. Either something is true, or false, and your code acts accordingly. In the example you posted, there isn't anything to optimize.
I think it's more a matter of preference; some people like the second example because it doesn't give you lots of nested if statements.
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

November 1st, 2006, 04:51 AM
|
|
Authorized User
|
|
Join Date: Jun 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thnx ma friend,
you know what i like the second style of the code too, i have many questions like this on code styles and optimizations, such example like the SqlConnection Close() issue, many sites talk about this, some of them
recommend to use "using {... }" block for critical code, some close it in "Finally{... }" section ,
i know i talk toooo much :( but in short what do u suggest me to read or to navigate on the internet for
programming performance tips??
|
|

November 1st, 2006, 05:27 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
In that particualr instance, using versus finally, there is no difference. The compiler replaces the using block by the appropriate finally block, and adds the call to Dipose as well. That's why the using part will not compile if the class you try to instantiate does not implement IDisposable. The MSIL generated at the end is the same.
As a matter of style the using is less verbose and quicker so I always prefer it.
--
Joe ( Microsoft MVP - XML)
|
|
 |