views:

124

answers:

5

In my attempt at dissecting a bit of C# I am afraid I do not understand either the goal or the logic of this bit of code:

if (!string.IsNullOrEmpty(str2) && (Strings.UCase(Strings.Left(str2, 1)) != Strings.Left(str2, 1)))
    {
        return false;
    }

I understand the first part is checking if str2 is "not null", however the second part is coming off a bit flaky. So we UCase the first character of str2, and if it does not equal the first character of str2 (which is NOT "UCase"d), then return "false"?

Maybe I am not missing anything and what I described above is in fact what the code is doing. If this is the case, can we reform this into something else that offers the same result,say for example, check if str2 is uppercase or not? I feel like this is the end goal.

You thoughts?

+4  A: 

My bet is that they are really just testing whether the first character is uppercase. The initial "IsNullOrEmpty" test is just there to make sure that the real test doesn't throw an exception.

The big question: if there is no string value (null or empty) this will not return false. Is that the expected outcome?

Mark Brittingham
Yes, this is the expected outcome. Some context: this bit of code is just one of many conditionals of a larger function which checks if a carriage return in a Word document is "valid". So, if there is no string value, the code "doesn't care".
jJack
+3  A: 

Code Objective in English :)

If the non-empty string begins with a lower case character then return false

Shankar Ramachandran
You would do yourself and the next guy who would be dissecting this in future if you put this English sentence before the code block as a comment :)
Shankar Ramachandran
@Xencor: Even more, if he can he should rewrite the code to something clearer (see Guffa's suggestion).
Jason
I would, however Guffa is correct: the code was taken from a dll using .NET Reflector. I am sure the real code has many comments in it, however Reflector, I believe, ignores comments, no?Being a "newb" to this stuff, not so sure I have the confidence to go to Development and say "hey! Comment your code!". I bug their code enough as it is...
jJack
@jJack: It's not Reflector that ignores comments, they are just not included in the compiled code. Actually nothing of the original source code is in the dll, Reflector just recreates code that will give the same result as the original code.
Guffa
+5  A: 

Yes, you understood the code right.

It looks like something translated from VB using a translation tool, as it's using functions from the VisualBasic namespace. I would rather write it with String methods:

if (!String.IsNullOrEmpty(str2) && str2.Substring(0,1).ToUpper() != str2.SubString(0,1)) {
  return false;
}

Or simply getting the first character as a character instead of as a string, and use the IsLower method of the Char class:

if (!string.IsNullOrEmpty(str2) && Char.IsLower(str2[0])) {
  return false;
}
Guffa
You are correct, I used .NET Reflector to dissect a .dll. That said, here is the code in Visual Basic:If (Not String.IsNullOrEmpty(str2) AndAlso (Strings.UCase(Strings.Left(str2, 1)) <> Strings.Left(str2, 1))) Then Return FalseEnd If--My next step is to learn how .NET Reflector really does its magic. Thanks for the help!
jJack
lesson learned: I won't post code in comments anymore.
jJack
A: 

This is the same, but refactored:

if (!string.IsNullOrEmpty(str2)) {
  string s = Strings.Left(str2, 1);
  if (Strings.UCase(s) != s) {
    return false;
  }
}

It is clear that this code tests that the first letter of str2 is or isn't in uppercase when it has any character.

eKek0
This did not improve the readability nor the maintainability of the code.
Jason
A: 

I share the perceptions you have when you say : "I do not understand either the goal or the logic of this bit of code" :) A test that returns only 'false is "fishy" : presumably "something" is waiting for a boolean to be returned, and nothing is returned if the result of this evaluates to 'true.

But if I had to write such a function I'd use the alternative OR logic :

return (! (String.IsNullOrEmpty(testString) || testString.ToUpper()[0] == testString[0]));
BillW