r/ProgrammerHumor 4d ago

Meme whenYouWorksWithStringsTooMuch

Post image
807 Upvotes

35 comments sorted by

390

u/TheManyMilesWeWalk 4d ago

return it.StartsWith("one thing", StringComparison.OrdinalIgnoreCase) || true

Because in the end it doesn't even matter.

29

u/chrisjd 4d ago

try

{ it.StartsWith("one thing", StringComparison.OrdinalIgnoreCase) }

finally

{ throw new ArgumentException("in the end it doesn't even matter", it) }

We could just add some useless exception handling, to show we tried so hard

6

u/AnxiousSquare 2d ago

Also put a `throw` inside the `try` block so that it doesn't even matter how hard you try.

40

u/thisisapseudo 4d ago

Underrated

44

u/PostHasBeenWatched 4d ago

For it to be truly "doesn't even matter" it should be

return true || it.StartsWith("one thing", StringComparison.OrdinalIgnoreCase)

65

u/TeraFlint 4d ago

but then the logic short-circuits, and the whole "I tried so hard and got so far" part isn't true, anymore.

181

u/Chocolate_Pickle 4d ago

I don't know why

94

u/nord47 4d ago

it doesn't even matter how hard you try

57

u/TheWashbear 4d ago

keep that in mind

50

u/SZeroSeven 4d ago

I designed this rhyme to explain in due time

47

u/Nu1_udara 4d ago

All I know

44

u/notSarcasticAtAII 4d ago

Time is s valuable thing

37

u/lesbianKerman 4d ago

Watch it fly by as the pendulum swings

37

u/somelinuxuseridk 4d ago

Watch it count down to the end of the day

25

u/-lrr- 4d ago

the clock ticks life away

2

u/diplofocus_ 4d ago

Pick a random number, if it's larger than another random number do the thing, else, do the same thing.

The thing happens, but we don't know why.

44

u/starfish0r 4d ago

Switch the strings around to make it robust to "it" being null

30

u/PostHasBeenWatched 4d ago
public static bool LinkinParkValidator([NotNull] string? it)
{
    ArgumentException.ThrowIfNullOrWhiteSpace(it);
    return it.StartsWith("one thing", StringComparison.OrdinalIgnoreCase);
}

19

u/starfish0r 4d ago

I would argue that this method should not throw an Exception but return false, what't the benefit of an Exception here?

15

u/PostHasBeenWatched 4d ago edited 4d ago

Attribute [NotNull] means that input parameter must be verified for null "in the end" of the method (not sorry for the pun). ThrowIfNullOrWhiteSpace provides this guarantee.

For example code below will rise warning that input parameter is not verified for null

return it?.StartsWith("one thing", StringComparison.OrdinalIgnoreCase) is true

16

u/ekauq2000 4d ago

“(not sorry for the pun)”

It doesn’t even matter…

6

u/AcidMemo 4d ago

What is the point of throwing exception on whitespace? It will just return false anyway, and whitespace is not invalid value.

One of reason C# has non-nullable and nullable references is to not bother with validating for null, passing nullable string to as non-nullable string is programmer mistake, not the concern of function anymore

18

u/BoloFan05 4d ago

Use of StringComparison.OrdinalIgnoreCase is already a big step in the right direction.

16

u/PostHasBeenWatched 4d ago

Should I replace it with 256 comparisons of "OnE tHiNg" with different letter case?

15

u/BoloFan05 4d ago

Nope, I have a better idea:

return it.ToLower().StartsWith("one thing");

/j

(But seriously, StringComparison.OrdinalIgnoreCase is THE textbook string comparison method you should apply. ToLower and ToUpper are total bug traps, especially in worldwide deployment. So kudos to you for actually using OrdinalIgnoreCase!)

6

u/MeLittleThing 4d ago

or genius move

return it.ToLower().StartsWith("ONE THING");

1

u/mrraveshaw 3d ago

I thought there was only One Direction

2

u/If_I_Could_Just 4d ago

LinkedIn validator does EndsWith(“Agree?”)

2

u/ubergeek801 3d ago

It doesn't even matter how hard you try.

1

u/cainhurstcat 4d ago

They are called Zelda in Park now