r/programminghorror • u/MISINFORMEDDNA • Mar 23 '26
C# Whitespace isn't a number?
I just got this in a PR. Not sure what to make of it.
if (string.IsNullOrWhiteSpace(palletNumber))
{
if (!string.IsNullOrEmpty(_palletNumber))
{
_errorMessage = "Pallet # not found.";
}
return;
}
UPDATE:
After multiple attempts to justify his code, we ended up with this, lol:
if (string.IsNullOrWhiteSpace(palletNumber))
{
return;
}
184
Upvotes
85
u/anto2554 Mar 23 '26
I don't know C#. Is _palletNumber equal to palletNumber?
94
u/Infinite_Self_5782 Mar 23 '26
unsure what palletNumber returns, but my god is that confusing naming and calling conventions holy shit
this looks like some code a junior dev would write at 3 am on about 50 grams of caffeine
5
u/FrikkinLazer Mar 24 '26
If you remove the inner if, does the unit tests start failing?
2
u/Rockworldred Mar 23 '26
Don't know the syntax here, bit why is something called a number using string and not an integer?
10
u/Over_Dingo Mar 23 '26
Interesting way of getting "IsWhiteSpace"
4
u/Rogntudjuuuu Mar 23 '26 edited Mar 23 '26
It looks like a check when there's a _palletNumber stored and the incoming call doesn't contain a palletNumber. It could've been cleaner, but I've seen much worse.
IsNullOrEmpty only checks for null and empty strings. IsNullOrWhitespace also accepts strings only consisting of whitespace. That way you don't have to trim the string before the check.
Edit: checked the code again, missed the negation
1
u/NovelStyleCode Mar 23 '26
Please tell me this is your intern's code
2
u/mk061104 Mar 23 '26
Are you working in intralogistics?
2
u/Rot-Orkan Mar 25 '26
This is the type of thing I get excited to see, because it means I get to write a simple unit test with like 10 inline data parameters with values like "", "abc", "123", "-123" null, " " and so on
Then my total number of unit tests goes up and I get a bunch more green checkmarks 🤩
2
u/Quick_Resolution5050 Mar 23 '26
I suspect ESL - they mean no pallet number found in request.
-4
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Mar 23 '26
That's a custom function, right? .NET doesn't provide a
IsNullOrWhitespace(), does it?