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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of trim(..)

Status
Not open for further replies.

ice78991

Programmer
Nov 20, 2006
216
I have heard that some browsers add whitespace onto strings sent in URl or Form variables and so you must use trim(..)
For example
Code:
<cfif len(trim(url.str))> ...do sthg

Is it correct to say you do not have to use trim(..) when dealing with variables in all other scopes ?
 
No. I would not make that blanket assumption. There are any number of scenarios that might cause variables to have leading or trailing spaces. For example, querying a database column with type "char".

It is impossible to say whether it is a factor in your application or not. If spaces are significant or critical to the outcome or your process, you may want to trim them explicitly.

----------------------------------
 
But it's always a good coding practice to use trim(). The last thing you want is to have to go through loads of code adding the trim() when you start to realize that whitespace is causing unforeseen issues.

_____________________________
Just Imagine.
 
I think it is better to approach it more generally. What I would say is a good practice is to code in anticipation of likely problems and properly handle them. Whether that means trimming values or some other action. Unless you are dealing with an object that has specific documented rules and a specific contract about the information it returns, it is generally best not to make blanket assumptions.

----------------------------------
 
To clarify, the blanket assumption I was referring to was the comment "Is it correct to say you do not have to use trim(..)". My answer would be no. Not unless there is a contract in the documentation that says the values are always trimmed.



----------------------------------
 
The example I had in mind was as follows

Code:
<cfset myvar = "">
[.code]

Ii decided at one point that I would trim everything.
Throughout a page I then tested the length of this variable. The test used len(trim....
but it seemed so pointless to trim here that I removed all trims. Was this good coding practice?
 
I suppose the best way to ask this question is 'is it best practice to trim for the VARIABLES scope or are you making your code superfluous by doing this'?
 
Whether it is necessary depends on your process. There are any number of things that might require you to trim the values. Such as setting it equal to a value from a database column with type char, which may pad values with spaces.

With so many variables in the equation it is impossible to say "No. You should never need to trim variables". But you only need to test the length or trim when the value has changed. So I do not see this as an issue. The bottom line is if removing trailing and leading spaces are critical to your process, then yes you should trim them.

----------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top