- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Edit Admin
I'm pretty sure this is Java, not C#. Bonus points for the singular method name getDomainID which actually returns multiple domain ids.
Admin
It's nothing a CSV-string can't solve.
Admin
Agreed; passing an empty array to
toArray
is a pretty strong tell. (Arrays in Java are pretty much vestigial, and there are backwards compatibility concerns that make them interoperate poorly with proper collections; in modern code, they're used for varargs and pretty much nothing else.)case
is a reserved word, but that could be an anonymization issue.particularCase.stream().map(i -> i.getDomainId()).collect(toSet())
is the right way to do this.Edit Admin
Eh, I dunno whats going on here but this is not C#:
Also there is no base class Set of HashSet. That would make no sense, because it's an IReadOnlySet or IEnumerable (https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1?view=net-8.0)
Considering that it looks to be really bad code, I bet it's either Java, JavaScript or PHP :p
Addendum 2024-12-11 07:29: A correct C# version would looks like this BTW:
[MethodImpl(MethodImplOptions.AggressiveInlining)] private static HashSet<string> GetDomainId(ICollection<ParticularCaseEntity> particularCases) => particularCases.Select(particularCase => particularCase.GetDomainId()).ToHashSet();
Admin
DDD, or D³ - Douglas the Departing Developer - hehehe ehehehe ...
Admin
reminds me of a perl snippet that i saw scattered all over a codebase in a past job my @ids = grep { ! $seen{$_} ++ } keys %hash;
this is a convoluted way to get a unique set of keys from a hash. a simpler version would just be my @ids = keys %hash;
Admin
And if an array really needs to be returned:
particularCase.stream().map(ParticularCaseEntity::getDomainId).distinct().toArray(String[]::new)
Edit Admin
Most likely the
grep
was originally operating on an array and later changed to a hash (making the grep unnecessary as you have shown.)With an array, the grep is also unnecessary as one can simply use
uniq
fromList::Util
, like so:Edit Admin
Pfffft to all the issues in the code. TRWTF is rehiring dear Douglas (er, well, if they did that in the end rather than just using his CV as kindling for the next company barbeque).
Edit Admin
I'm not sure about arrays being vestigial - all collections use them, and they are the fastest option if you need a fixed length modifiable collection.
Edit Admin
This makes me sad. I see how they got there, but it's still sad.
Admin
Once you've started thinking about functional programming, it's amazing how much OO code is just reinventing map
Edit Admin
Ha, someone ninja fixed the article - now I wonder if it was a typo or if Remy also doesn't get any infos about the language involved :-)
Edit Admin
@**** ref
Yep. And more bonus points for having a parameter of type
Collection<T>
named singularly:particularcase
. If a single instance of a case con contain multiple case entities (which seems likely), then the naming is even worse. The parameter oughth to beparticularCaseEntities
. They got not only the plurality wrong, but what the collection contains.As often said, naming is in fact a hard problem. Or at least there are hard instances of it. Then there are people who give no thought whatever to the names they choose, an/or simply don't understand what they're trying to do.
Addendum 2024-12-11 12:16: Oops. My first line was supposed to read: @Melissa U ref
Sorry Melissa.
Admin
Not to mention that HashSet has had a constructor that takes a
Collection
since Java 1.2, so even if you needed an array you could just: