More on string comparison
-
Straight out of a C# Unified Communications code sample from MS:
if (string.IsNullOrEmpty(customMessage.Trim()))
...Looks like somewhen Trim() returned null when a string contained spaces only. Or was it meant for Oracle compatibility (with Oracle, an empty string means null)?
-
Straight out of a C# Unified Communications code sample from MS:
if (string.IsNullOrEmpty(customMessage.Trim()))
... -
Straight out of a C# Unified Communications code sample from MS:
if (string.IsNullOrEmpty(customMessage.Trim()))
... -
What is wrong with that? I imagine this was before .Net 4.0 in which you can now use string.IsNullOrWhitespace()...
if(string.IsNullOrWhitespace(customMessage))
...Illogical thoughts make me ill
musefan wrote:
What is wrong with that?
As _Erik_ said, what happens with this code?
string customMessage = null;
if (string.IsNullOrEmpty(customMessage.Trim()))
... -
musefan wrote:
What is wrong with that?
As _Erik_ said, what happens with this code?
string customMessage = null;
if (string.IsNullOrEmpty(customMessage.Trim()))
...now that's shameful coding, but the OP never put that, there is no evidence that it is unsafe to assume that customMessage will never be null. What if the following was the case...
string customMessage = GetCustomMessage();
if (string.IsNullOrEmpty(customMessage.Trim()))
...string GetCustomMessage(){
string result = GetSomeDataFromSomewhere();
return result ?? "";//or in times of old... return result == null ? "" : result;
}Illogical thoughts make me ill
-
now that's shameful coding, but the OP never put that, there is no evidence that it is unsafe to assume that customMessage will never be null. What if the following was the case...
string customMessage = GetCustomMessage();
if (string.IsNullOrEmpty(customMessage.Trim()))
...string GetCustomMessage(){
string result = GetSomeDataFromSomewhere();
return result ?? "";//or in times of old... return result == null ? "" : result;
}Illogical thoughts make me ill
musefan wrote:
there is no evidence that it is unsafe to assume that customMessage will never be null
If that was the case then the call to
string.IsNullOrEmpty
would not be required, as you could safely access theLength
property and test for zero. Eitherway you look at it, the OP code is unnecessary ;) -
musefan wrote:
there is no evidence that it is unsafe to assume that customMessage will never be null
If that was the case then the call to
string.IsNullOrEmpty
would not be required, as you could safely access theLength
property and test for zero. Eitherway you look at it, the OP code is unnecessary ;)But the whole point in the trim (IMO) is to test that a complete whitespace string is not confused with meaningful data. Imagine if the string was fetched using my sample method, and the call to get the data in there returns a tab separated list - this function may always end in a tab even when no data has been added. so the resulting value could be "\t" in which case it should be ignored just like "" would be. Your length test doesn't work in this case because a string of data and whitespace would equal the same. Consider the following possible string values and what you want to test for invalid...
string s = "";//empty and want to handle (length is 0 so fine)
string s = "\t";//as good as empty and want to handle (length is 1 so problem here)
string s = @"data 1\tdata 2\t";//some useful data and this will not be 'caught' by the trim testIllogical thoughts make me ill
-
But the whole point in the trim (IMO) is to test that a complete whitespace string is not confused with meaningful data. Imagine if the string was fetched using my sample method, and the call to get the data in there returns a tab separated list - this function may always end in a tab even when no data has been added. so the resulting value could be "\t" in which case it should be ignored just like "" would be. Your length test doesn't work in this case because a string of data and whitespace would equal the same. Consider the following possible string values and what you want to test for invalid...
string s = "";//empty and want to handle (length is 0 so fine)
string s = "\t";//as good as empty and want to handle (length is 1 so problem here)
string s = @"data 1\tdata 2\t";//some useful data and this will not be 'caught' by the trim testIllogical thoughts make me ill
Sorry, I don't think I phrased it well. The length test was a replacement for the
string.IsNullOrEmpty
call - you can still callTrim
though. Back to the original code:// This is the same as the original code - if customMessage
// is null then they'll be an exception
if (customMessage.Trim().Length == 0)
... -
Sorry, I don't think I phrased it well. The length test was a replacement for the
string.IsNullOrEmpty
call - you can still callTrim
though. Back to the original code:// This is the same as the original code - if customMessage
// is null then they'll be an exception
if (customMessage.Trim().Length == 0)
...I see, yep that's OK with me :) My initial post was really to find out why it was such a bad line of code when we cannot see any other code to judge. People seemed to be going for the null exception but there was nothing to indicate that wouldcould happen
Illogical thoughts make me ill
-
I see, yep that's OK with me :) My initial post was really to find out why it was such a bad line of code when we cannot see any other code to judge. People seemed to be going for the null exception but there was nothing to indicate that wouldcould happen
Illogical thoughts make me ill
-
I didn't say it was unlikely, I said it was possible that it could never happen. That does not mean sometimes it could happen, it means other code could decide that it will never happen.
oggenok wrote:
things that CAN happen eventually WILL happen
...and that is not certain if I wrote...
string s = "hello ";
s = s.Trim();...would you correct with...
string s= "hello ";
if(!string.IsNullOrEmpty(s))
s = s.Trim();EDIT: Not understanding my message is not justification to vote it down.
Illogical thoughts make me ill
modified on Tuesday, March 1, 2011 10:22 AM