- Feature Articles
-
CodeSOD
- Most Recent Articles
- Irritants Make Perls
- Crossly Joined
- My Identification
- Mr Number
- intint
- Empty Reasoning
- Zero Competence
- One Month
-
Error'd
- Most Recent Articles
- Not Impossible
- Monkeys
- Killing Time
- Hypersensitive
- Infallabella
- Doubled Daniel
- It Figures
- Three Little Nyms
- 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
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.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)
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: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).
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.
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
Admin
Another set of mistakes: commas here. "One of the long-tenured developers, Douglas at Patrick's company left, ..." needs one after Douglas. "...having Patrick doing his job, and also picking up the work that Douglas used to do was bad." needs one after do, or the existing one to go. And half spaced hyphens are not dashes. They make you look like a teenager.
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 :-)
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:Admin
Now that's an epic WTF of a manglement.
Admin
The command "domainIds.toArray(new String[0])" is very ineffective. It is creating passing an empty array which is not used and garbage-collected, because a bigger array is needed that is created inside the toArray() method. A quicker running is "domainIds.toArray(new String(domainIds.size()))". Here the passed array is used to fill in the data and then returned. I see this type of coding by a lot of inexperienced programmers who never cared to look up why the toArray() method needs such a parameter
Admin
The reason why
toArray()
needs such a parameter is because otherwise the desired type cannot be determined, and the result will be anObject[]
, notString[]
. That's exactly whattoArray()
without arguments returns. The overloadedtoArray
could have beentoArray(Class<T> elementType)
but that too would require reflection to create the array.domainIds.toArray(new String[domainIds.size()])
will indeed use the passed array, as long as the collection doesn't grow in the meantime (if shared between multiple threads). I think that since Java 11 the preferred way should bedomainIds.toArray(String[]::new)
, so the collection can allocate its own array based on its current size. It saddens me to say that the current implementation uses 0 as argument (and I've seen no override that does it differently), but that's something that can be improved internally.Admin
The truly astounding WTF here is that
.toArray()
can't determine the type on its own, which was specified when it was constructed:The default, in this case, should be the underlying type,
String[]
rather thanObject[]
, imho, had this been better thought out.Admin
That's because generics didn't exist until Java 5.0, in 2004. Until that time there was just
Collection
andArrayList
. When generics were introduced Sun (yes, that long ago) had to deal with a lot of existing code that should continue to work without having to recompile it. The solution was type erasure - generics only exist during compile time, and the compiler effectively removes it or replaces it with casts. During runtime we're back atCollection
andArrayList
, the generic typeString
no longer exists. The JVM can therefore not know that we wantString[]
and notObject[]
.Other languages like C# use reified generics that effectively creates new types on the fly. That means the generic type is built into the new type and therefore remains during runtime.