News for hackers. Stuff that matters.
Re:You’re better off using Exceptions by XMPPwocky on Monday February 03, 2020 @01:44PM (#22227447)
For most implementations of maps (hashtables and various tree-ish things), even in the single-threaded case, doing .exists and then .get is about twice as expensive as doing a .get that returns an Option (or nullable reference, default value, whatever)- you have to do the map lookup twice.

(Okay, probably not twice as slow, since the cache will be hot, but still.)

Re:You’re better off using Exceptions by seanmcdirmid on Monday February 03, 2020 @02:43PM (#22228124)
Not twice as slow. But you are forgetting the case where you know the element is already in the map, and you want to call map.get() without wrapping it in an option.
Re:You’re better off using Exceptions by pjc50 on Monday February 03, 2020 @04:35PM (#22229425)
C# does this fairly nicely with an "out" parameter: https://docs.microsoft.com/en-us/dotnet/api/system.collectio...

Or if you "know" it's there call the version that throws an exception if it isn't.

Re:You’re better off using Exceptions by seanmcdirmid on Monday February 03, 2020 @08:23PM (#22231374)
I haven't used that aspect of the Dictionary API at all in the 9 years programming C#, it is just too unwieldy to have to define a separate local variable to hold the result of the get. It also doesn't scope the out assignment (if the get fails, you can still read the out variable bound). I'm rarely in the case where I don't expect something to be in a map anyways, and when I do, I have an extension method with a default value returned if the key doesn't exist (or in some cases populate). Keeping expressions clean is important.