Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Efficient use of If Then

Status
Not open for further replies.

ahmun

IS-IT--Management
Jan 7, 2002
432
US
Hi All...

I've been wondering about how ASP works in regards to using If Then Else Statement

Take the two code examples:

** only two lines of code... looks neat for a developer!
Code:
strName = Request.Form("Name")
If isNull(strName) Then strName = ""

** 5 lines of code... yuck!
Code:
If isNull(Request.Form("Name") Then
  strName = ""
Else
  strName = Request.Form("Name")
End if

Which of the two are more efficient use of the server's processing?

Earnie Eng
 
how bout your don't do either.

how bout just
strName = Request("Name").Item

If there isn't any value in "Name" then it is the same as saying

strName = "";

By using .Item, you take the value of what is in the Request object.
By not using .Item you are taking the Request Object itself.

Think of it this way.

.Item says - tell me what is on that piece of paper. If there is nothing on it then the answer is "nothing"

no .Item says - give me that piece of paper and I will look at it every time I want to see what is on it. If there is nothing on it, the answer is "a piece of paper with nothing on it." which is not the same as "nothing"



"Every day is like a precious gift, you have to make it count" James Birrell 1993-2001
 
The problem comes when I use the
Replace(strName, " ", " ")
it errors out when strName is null...

sorry for omitting this detail.

a revised display of the two code blocks... The reason for this is that I may be looping through 100s of records, and repeating this if/then structure across multiple fields in each record...

** only threelines of code... looks neat for a developer!

Code:
strName = Request.Form("Name")
If isNull(strName) Then strName = ""
[blue]strName = replace(strName, " ", " "[/blue]

** 6 lines of code... yuck!

Code:
If isNull(Request.Form("Name") Then
  strName = ""
Else
  strName = Request.Form("Name")
End if
[blue]strName = replace(strName, " ", " "[/blue]


A practical reson I would want to make this replacement is so that I force, for example, a full name, to have non breaking spaces in html display so that the name doesn't jump to a second line in a variable-width table cell.

Or... if you look at thread333-985135, I had asked everyone for help on a solution to truncating a large description field... I also used a replace statement in that case so that the description text doesn't jump to a second line...

Earnie Eng
 
What about:

strName = replace(Request.Form("Name"), " ", " ")

but make sure you use the final closing parenthesis, which you missed in both examples in your sample code.

Lee
 
yes.. I did miss the closing paren
that was my original code... to assign strName to the result of the replace function...

and that's when the server gave me the error:
Invalid Use if Null: Replace (paraphrased)


Earnie Eng
 
In my opinion the longer method for the If/Then is probably more efficient. Basically it is a comparison between one conditional and one assignment vs one conditional and two assignments.

Additionally, you should never get a Null out of Request.Form unless you somehow managed to put it there yourself. If you ask for a value that does not exist then it gives you back an empty reference. And on top of that you should not be getting a Null error on your replace (though some of the other string functions will give it to you on an empty field).

Here is an example I just whipped up to back up all of my above points:
Code:
<%
Option Explicit

'Is it null?
If IsNull(Request.Form("NonExistantField1")) Then
	Response.Write "Field qualifies as Null<br>"
Else
	Response.Write "Field is not Null<br>"
End If

'Is it Empty?
If IsEmpty(Request.Form("NonExistantField2")) Then
	Response.Write "Field qualifies as Empty<br>"
Else
	Response.Write "Field is not Empty<br>"
End If

'string Length?
If Len(Request.Form("NonExistantField3")) > 0 Then
	Response.Write "Field has a length > 0<br>"
Else
	Response.Write "Field qualifies as 0 length string<br>"
End If

'is it numeric?
If isNumeric(Request.Form("NonExistantField4")) Then
	Response.Write "Field qualifies as a Number<br>"
Else
	Response.Write "Field is not a Number<br>"
End If

'what's it's VarType?
Response.Write "Field's varType qualifies as: " & VarType(Request.Form("NonExistantField5")) 
Response.Write " (0=Empty, 1=Null)<br>"

'and the much argued Replace()
On Error Resume Next
Response.Write Replace(Request.Form("NonExistantField6")," ","&nbsp;")
If err.Number <> 0 Then
	Response.Write "The Replace Failed!<br>"
Else
	Response.Write "The Replace Succeeded! - Is it still empty? " & IsEmpty(Request.Form("NonExistantField6")) & "<br>"
End If
On Error Goto 0

%>

The output I get is:
Field is not Null
Field qualifies as Empty
Field qualifies as 0 length string
Field qualifies as a Number
Field's varType qualifies as: 0 (0=Empty, 1=Null)
The Replace Succeeded! - Is it still empty? True

The fact that this disagrees with what you have posted makes me wonder if your error is not in fact occurring elsewhere.

-T

barcode_1.gif
 
Tarwin, thanks for clarifying the double-assignment case in my first example, vs a single assignment using the longer method.

hm... in my tunnel vision of trying to make my code efficient... I overlooked one very important detail as I typed out my code example above (not the actual code I was using): I was using a recordset, as opposed to a request.form... which does return a null value, if the field in the DB was null.

This would explain why I would return an error while a request.form wouldn't be the problem... yep you are right, too... the error was occurring for a different reason.

I ended up writing the code in long form.


Earnie Eng
 
I am just as guilty as everyone else of trying to write 5 lines of code in 2, and I occasionally justify it as "the 3ms I didn't save will never be noticed". However in the long run your better off because the code is more readable and easier for others to maintain. As an example read any post where I supplied a 30 line example and then later suplied a one line solution. The one liner is cool (and sometimes faster) but would be hell to change (even for me) down the road, whereas the 30 liner is easier to understand.
Course, commenting and a common variable naming scheme helps too :p

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top