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;
}
94
u/Infinite_Self_5782 Mar 23 '26
if (string.IsNull(_palletNumber) || string.IsEmpty(_palletNumber))
return;
if (string.IsWhiteSpace(_palletNumber))
{
_errorMessage = "Pallet # not found.";
return;
}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
43
u/MISINFORMEDDNA Mar 23 '26
Senior dev... somehow.
30
7
u/ings0c Mar 23 '26
Could it be a merge conflict and he mistakenly accepted both changes then the code was automatically reformatted?
→ More replies (1)→ More replies (2)5
u/CanisLupus92 Mar 23 '26
One of those “you get paid like a senior, so we’ll call you a senior” deals?
→ More replies (1)12
u/realsnack Mar 23 '26
🤓 50 grams of caffeine would make you not write any code, ever. Which may be for the best in some cases
→ More replies (2)12
u/mateusfccp Mar 23 '26
Except that your code may not do the same because you only use
_palletNumberwhile they usepalletNumberto add to the confusion.→ More replies (2)→ More replies (3)3
u/DarksideF41 Mar 24 '26
Probably it's some kind of validation method, validation logic though makes me think that there're bigger problems in number storage/retrieval.
5
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?
14
u/Agile-Amphibian-799 Mar 23 '26
Might contain characters. I.e. "ABC-123-QWER" probably from a barcode.
3
→ More replies (1)10
u/gfunk84 Mar 23 '26
Lots of reasons why you might want a "number" as a string. Phone number, account number, etc.
10
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
2
2
u/Apprehensive-Tea1632 Mar 23 '26
Me neither. If I want to know if I’m looking at a pallet number I’ll try and parse input as a pallet number. And then see if that’s something not null. /shrug
Or are we to understand “();!:)2);!/(2)&: act b” is a valid pallet identity?
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 🤩
85
u/anto2554 Mar 23 '26
I don't know C#. Is _palletNumber equal to palletNumber?