Results 1 to 6 of 6

Thread: [RESOLVED] Efficiency of If variations

  1. #1

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Resolved [RESOLVED] Efficiency of If variations

    So, I'm trying to develop an application on .NET 4.5. Just a quick question, in the following implementations, which are the most efficient (and flexible in terms of compatibility) implementation of the following cases:

    Code:
        
    	If String.IsNullOrWhiteSpace(txtPXFName.Text) _
    		Or String.IsNullOrWhiteSpace(txtPXMName.Text) _
    		...
    	End If
    	
    	If Trim(txtPXFName.text = "") _
    		Or Trim(txtPXFName.text = "") _
    		...
    	End If
    Code:
            blnAbFHR = If(rdoT4AbFHR1.Checked = True, True, False)
    
            If rdoT4AbFHR1.Checked = True Then
                blnAbFHR = True
            ElseIf rdoT4AbFHR2.Checked = True Then
                blnAbFHR = False
            End If
    Also, are the following codes essentially the same?
    Code:
    If xx.Checked Then
    If xx.Checked = true Then
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,745

    Re: Efficiency of If variations

    Code:
    blnAbFHR = If(rdoT4AbFHR1.Checked = True, True, False)
    
    If rdoT4AbFHR1.Checked = True Then
       blnAbFHR = True
    ElseIf rdoT4AbFHR2.Checked = True Then
       blnAbFHR = False
    End If
    
    ' Just write
    blnAbFHR = rdoT4AbFHR1.Checked
    Code:
    ' These are the same
    If xx.Checked Then
    If xx.Checked = true Then

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Efficiency of If variations

    IsNullOrWhiteSpace does more than just Trim (it also deals with there being no string at all), so it is safer to use that.

    Both parts of your second code block are essentially the same, but can be made even simpler/faster:
    Code:
    blnAbFHR = rdoT4AbFHR1.Checked
    edit: I just noticed that the ElseIf refers to rdoT4AbFHR2 , was that a typo? (my comments were based on it being rdoT4AbFHR1)

    Quote Originally Posted by riechan View Post
    Also, are the following codes essentially the same?
    Code:
    If xx.Checked Then
    If xx.Checked = true Then
    Yes, and they probably compile to exactly the same thing (so the difference is the amount of typing/reading)
    Last edited by si_the_geek; Mar 16th, 2016 at 04:56 AM.

  4. #4

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Efficiency of If variations

    Code:
    blnAbFHR = rdoT4AbFHR1.Checked
    Yes, it's the second radio button for that set. I should omit that. So this basically gets the status of this rdoT4AbFHR1 (since it can only be True/False) right? I'm trying to make my code much more debug-able, you see (and much more easier to type, I guess).
    Last edited by riechan; Mar 16th, 2016 at 09:11 AM.
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Efficiency of If variations

    If you are talking about efficiency, then OrElse will almost always be more efficient than Or, so there's that.

    Also, at one point, checked = True actually compiled to more code than just .Checked, because there was an extra statement generated for comparing True to True. I seem to remember looking a few years ago and that was no longer the case.
    My usual boring signature: Nothing

  6. #6
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Efficiency of If variations

    Quote Originally Posted by riechan View Post
    ...I'm trying to make my code much more debug-able, you see (and much more easier to type, I guess).
    Sometimes, these are mutually exclusive: less typing can mean more debugging. In general (YMMV) more explicit, lengthy, code will mean less debugging.

    The number of questions trying to solve null reference exceptions are numerous; guaranteed that the poster tried every shortcut in the book to write 'shorter' code. More explicit code, using local variables, will easily avoid the problem, or make it easier to debug, and write robust code.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width