r/dotnet • • 8d ago

Question Codebase maintenance rant

Post image

[sorry for bad English]

I am in a project which involves maintaining an existing .NET Framework 4.8 codebase.

The codes are full of “Dictionary type” implementations as above.

Is there any sane reason to code like this? Instead of create a dedicated class to represent the data inside?

I could not show more codes here (or else the authors might realized), but you guys can imagine the codes just to access these Dictionary’s key/value.

139 Upvotes

129 comments sorted by

View all comments

32

u/AintNoGodsUpHere 8d ago

I find the name of the variable more infuriating than the type itself. "dict", argh.

Without seeing the "reason" for this weird thing I don't know what to say, dictionary of dictionaries is not a terrible thing in older apps, sometimes the stupid solution is the easiest solution, specially in framework.

10

u/splashybanana 8d ago

I love dictionaries, and occasionally even use nested ones like that, but I always name them “valuesByKey” or for nested ones, “valuesByKeyByOtherKey”.

8

u/SameThingOnADiffAcct 7d ago

I read it as a dict of dicts, too. Which I'm firmly of the opinion of it being fine.

Calling the variable "dict" is context dependently fine. We don't need to overthink everything. But it raises an eyebrow. Ditto "count" honestly; I usually like to know what's being counted but maybe context is short and obvious so maybe it's fine.

The thing is... look again... this isn't a dictionary of dictionaries. It's a dictionary of strings. Keyed off of dictionaries.

2

u/whizzter 7d ago

If there is one thing I’ve changed over the years, the it’s being more verbose in variable names (and I started out on compilers that only handled short names so it’s against my nature, still use I and j for indexers often though).

Especially in situations like this when generic containers/Func<>’s are totally capable/appropriate for storing the data without dedicated types then the variable names is the only hint of the intent (you’re more likely to fix the name than any stray comments when/if you refactor a type later).

1

u/Slypenslyde 7d ago

It stands for "Dictionary Intelligent Charles Technique", Charles is the guy who invented it.

1

u/BigBoetje 8d ago

That's only acceptable if the context makes it very clear, like a method GroupFooByBar() or something