[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
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
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
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) :)
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).
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.
Re: Efficiency of If variations
Quote:
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.