Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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
 
Old November 1st, 2006, 02:47 AM
Authorized User
 
Join Date: Jun 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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;
}

 
Old November 1st, 2006, 03:04 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old November 1st, 2006, 03:18 AM
Authorized User
 
Join Date: Jun 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

come on ma friend what i mean that is not a real code case,
true means if the conditions become true.


 
Old November 1st, 2006, 03:59 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old November 1st, 2006, 04:51 AM
Authorized User
 
Join Date: Jun 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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??

 
Old November 1st, 2006, 05:27 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Python code optimization icopec Python 2 December 13th, 2011 05:13 AM
Search Engine Optimization radiohalo5 ASP.NET 1.x and 2.0 Application Design 4 November 27th, 2010 09:44 PM
Crystal Reports optimization Raghavendra_Mudugal Crystal Reports 0 June 13th, 2006 12:08 AM
Pass-Through Query Optimization redrobot5050 Access VBA 3 June 18th, 2004 01:25 AM
Query optimization SubodhKumar SQL Language 1 October 22nd, 2003 06:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.