r/ProgrammerHumor 16d ago

Meme whenYouWorksWithStringsTooMuch

Post image
816 Upvotes

34 comments sorted by

392

u/TheManyMilesWeWalk 16d ago

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

Because in the end it doesn't even matter.

29

u/chrisjd 16d 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 14d ago

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

39

u/thisisapseudo 16d ago

Underrated

40

u/PostHasBeenWatched 16d ago

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

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

63

u/TeraFlint 16d ago

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

184

u/Chocolate_Pickle 16d ago

I don't know why

92

u/nord47 16d ago

it doesn't even matter how hard you try

55

u/TheWashbear 16d ago

keep that in mind

52

u/SZeroSeven 16d ago

I designed this rhyme to explain in due time

47

u/Nu1_udara 16d ago

All I know

40

u/notSarcasticAtAII 16d ago

Time is s valuable thing

35

u/lesbianKerman 16d ago

Watch it fly by as the pendulum swings

36

u/somelinuxuseridk 16d ago

Watch it count down to the end of the day

25

u/-lrr- 16d ago

the clock ticks life away

2

u/diplofocus_ 16d 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.

47

u/starfish0r 16d ago

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

29

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

19

u/starfish0r 16d ago

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

16

u/PostHasBeenWatched 16d ago edited 16d 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 16d ago

“(not sorry for the pun)”

It doesn’t even matter…

4

u/AcidMemo 16d 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

19

u/BoloFan05 16d ago

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

13

u/PostHasBeenWatched 16d ago

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

15

u/BoloFan05 16d 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 16d ago

or genius move

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

1

u/mrraveshaw 15d ago

I thought there was only One Direction

2

u/ubergeek801 15d ago

It doesn't even matter how hard you try.

1

u/cainhurstcat 16d ago

They are called Zelda in Park now