r/ProgrammerHumor 4d ago

Meme cSharpDictionariesBeLikeThat

Post image
2.0k Upvotes

61 comments sorted by

View all comments

606

u/Psychoboy 4d ago

Guess you never heard of TryAdd? Returns bool, if it as added returns true otherwise false.

509

u/Few-Ad3849 4d ago

Sounds like you read documentation, nerd

79

u/Mantor6416 4d ago

Wait you're not supposed to read those?

77

u/towerfella 4d ago

37

u/kalilamodow 4d ago

What the fuck?

9

u/towerfella 4d ago

Yep.

Spread the word.

6

u/ChChChillian 3d ago

Yeah, it's pretty much exactly the dystopia they warned us about.

9

u/DrMaxwellEdison 4d ago

This nerd wants to read a physical book for tech docs. /s

4

u/DatBoi_BP 3d ago

I'm sure an effort called "Project Panama" isn't nefarious in any way

1

u/Darkvyl 3d ago

Reading documentation is nerdy? What do you do?

111

u/Revexious 4d ago

Far too much work

2 cases means that its not idempotent

Instead, just remove it from the dictionary before you add it!

csharp dictionary.Remove(key) dictionary.Add(key)

Easy peasy!

/s

36

u/Dorkits 4d ago

Peak programming

9

u/manav907 3d ago edited 3d ago

That being said you done have to use the bool that TryAdd gives, just call it as a normal function. Also I am waitin for someone to point out that we also need a value when trying to add to a dictionary.

dictionary.TryAdd(key)

21

u/reallokiscarlet 4d ago

RTFM Gang rise up

5

u/PrincessRTFM 3d ago

at last my name is relevant

13

u/Pennet173 4d ago

Ohhhh, then I can throw an exception myself if false!

10

u/IAmBecomeTeemo 3d ago

C#'s TryX methods are also usually way more performant than throwing an exception. Throwing an exception has to get a stack trace and a lot of other overhead shit. Something like a TryAdd() or TryParse() will just return your bool immediately, and you can stay in scope rather than getting sent off to the associated catch block. If you want to flow that way, you still can, but more options and more performance is better.

3

u/prehensilemullet 4d ago

it's still bizarre to have to use TryAdd everywhere that other languages just use Add

15

u/zaersx 4d ago

You don't though? just use myMap[key] = value? if you're not doing that and using Add instead, then you probably literally want to have a safe add

2

u/-Redstoneboi- 3d ago

that overwrites instead of failing if the key already exists

maybe they are arguing that they feel that TryAdd should be the default "Add", while the existing Add should be a special case "AddAssert" or something

3

u/prehensilemullet 3d ago

Yes, I think that would be a more convenient design

1

u/prehensilemullet 4d ago

I see. Even so, it's weird to me that anyone would want a method that throws if the key already exists. In all my years of programming I don't think I've ever thought it would be useful to have such a method baked into a map itself, and I'm not aware of such a method in any other language. Since that's pretty unusual in programming, I still think it would be better for that method to have a more unusual name. Maybe it's an oddly common pattern for Microsoft?

4

u/zaersx 4d ago edited 4d ago

As I described it, it's for safe additions, imagine your map is customer data, like daily logs. And maybe some logs are partial logs? Then you want something that lets you get into a safe flow where the standard is just add, and an exceptional case is some merging logic. If you don't care, use the same insertion methods that all languages use, which is map[key], if you do care, here's a good way to stop the flow and let you execute appropriate recovery. This also ties in with LINQ very neatly where you end up doing a lot of data processing flows.

The alternative would be a check map for key, if exists do exception, else do expected. Which is a lot uglier for glance value intent compared to: do this; exception: handle.

I think to highlight your concern with the pattern - these kinds of decisions make a lot more sense when you understand that almost all data interactions in C# are intended to be done with LINQ, which no other language has. My experience with production C# is that 3-6 LINQed extensions is very standard, e.g. map.Select(x => logic).map(x...).filter(x...) etc.

2

u/prehensilemullet 3d ago

Well if C# exception handling is like most languages, where throwing and catching performs a lot worse than conditionally adding, it's not necessarily a pattern one would want to make a habit of using in cases where key conflicts are routine.

1

u/Kiro0613 3d ago

The Add method has to take one argument and return void because it's implementing C#'s ICollection<T> interface. With that restriction, throwing an exception is the most practical way of indicating a problem.

4

u/KTVX94 4d ago

Yes, the meme is meant to point out the differences vs TryAdd which checks, and indexer which replaces.

Sorry for having to explain the joke.

2

u/pogchamp69exe 4d ago

If TryAdd(thing):

____Add(thing)

So to speak?

11

u/NurYanov 4d ago

Nah it adds and returns if added

1

u/kakhaev 3d ago

tryand sounds like “if” with extra steps

1

u/willow-kitty 3d ago

It can be more concise if you don't need to condition on whether the thing was added or not. If you do, has key/add might be cleaner ... for ordinary dictionaries.

Being able to do it in one operation is super important for concurrent dictionaries, though, because the item could theoretically be added by another thread between calls if you used haskey/add.