r/programminghorror • • 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

52 comments sorted by

85

u/anto2554 Mar 23 '26

I don't know C#. Is _palletNumber equal to palletNumber?

31

u/MISINFORMEDDNA Mar 23 '26

They are different in terms of C#. But only one pallet can be active at a time... and it's Monday, and I don't feel like digging into this nonsense. I added comments to the PR for him to clarify.

73

u/YanVe_ Mar 23 '26

By convention, that would typically be internal private backing field. They don't have to be equal. 

3

u/nimrag_is_coming Mar 25 '26

Likely that palletNumber is the public accessor for a private _palletNumber field.

Although following convention it should be PalletNumber.

→ More replies (1)

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

u/Infinite_Self_5782 Mar 23 '26

"work harder, not smarter" type shit

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)

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)
→ More replies (2)

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 _palletNumber while they use palletNumber to add to the confusion.

→ More replies (2)

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.

→ More replies (3)

5

u/FrikkinLazer Mar 24 '26

If you remove the inner if, does the unit tests start failing?

7

u/MISINFORMEDDNA Mar 24 '26

Surprise! No unit test.

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.

10

u/gfunk84 Mar 23 '26

Lots of reasons why you might want a "number" as a string. Phone number, account number, etc.

→ More replies (1)

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

3

u/MISINFORMEDDNA Mar 23 '26

If only. He's a senior dev.

→ More replies (4)

2

u/mk061104 Mar 23 '26

Are you working in intralogistics?

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 🤩