|
-
Mar 16th, 2016, 04:35 AM
#1
Thread Starter
Addicted Member
[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
-
Mar 16th, 2016, 04:52 AM
#2
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
-
Mar 16th, 2016, 04:53 AM
#3
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)
 Originally Posted by riechan
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.
-
Mar 16th, 2016, 09:05 AM
#4
Thread Starter
Addicted Member
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
-
Mar 16th, 2016, 09:42 AM
#5
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
 
-
Mar 16th, 2016, 09:49 AM
#6
Re: Efficiency of If variations
 Originally Posted by riechan
...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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|