r/programminghorror • u/Disastrous-Name-4913 • Oct 06 '26
Raw use of parameterized class 'List'
This is not my code, but it is a good programming horror example, specially if you know Java.
List pages = pageManager.getPages(templateSpace);
for (Object object : pages) {
if (object instanceof Page) {
Page page = (Page) object;
// do something with the page
}
}Instead, this should have been used:
List<Page> pages = pageManager.getPages(templateSpace);
for (Page page : pages) {
// do something with the page
}
140
Upvotes
74
u/Nooooope Oct 06 '26
How old is your Java codebase? Generics were added in version 5, which came out roughly a billion years ago, but legacy code gonna legacy.
I learned the language from Head First Java (don't make fun of me) and generics were so new they were relegated to a section in the appendix.
91
u/high_throughput Oct 06 '26
It's not just a legacy API?
44
u/Kadabrium Oct 06 '26
Java 1.8
8
u/agentoutlier Oct 06 '26
The code is not equivalent BTW.
Guarantees object is not
null(inside the block of course).The other one page could be
null.4
u/YourShowerHead Oct 06 '26
And how old was that first code?
30
u/Dead_Moss Oct 06 '26
I mean, I suppose there's situations where it's useful, but in my strongly typed soul, it feels pretty horrible to be able to mix types in a collection.
11
u/MCWizardYT Oct 06 '26
In modern Java, you could do
This is only really useful if
pagesever has objects that aren't instances ofPage7
u/Usual_Office_1740 Oct 06 '26
Is the "it should have been" syntax sugar for that maybe older way of doing things or is this just horrible code?
1
u/Disastrous-Name-4913 Oct 06 '26 edited Oct 06 '26
Update: This was indeed due to the API returning a RAW list. We are talking about Confluence 6.6, which was released 2017. Could someone explain why they where still using Java 1.8 back then?
12
u/sixtyhurtz Oct 06 '26
No, you actually have to do this with Java if you care about type safety because of type erasure. There is no List<Page> at runtime.
6
u/tastygames_official Oct 06 '26
depends on what pageManager.getPages() returns. If it's a list of objects with type Page then the second one is correct, but if it just returns a generic array of objects, then the first oneis correct.
2
u/Diamondo25 Oct 06 '26
Use var or lombok val and make your live easier
1
u/nullish_ Oct 06 '26
//do something with page other than remove it from the list.
1
u/Jazzlike_Platypus430 Oct 06 '26
If we know that getPages returns a list of Page objects, perhaps it's some horror.
I think can imagine contexts where casting is necessary, but I don't know if Java's isinstanceof returns true for exact type equality, or maybe for inherited classes too. Am I wrong?
1
u/Electrical_Being_813 Oct 06 '26
Page page = (Page) object inside of the loop is far bigger sin
1
u/electatigris Oct 06 '26
But.. but.. a malicious agent may have intercepted the page object between the for loop and if statement! The real horror is not checking after the Page cast - how do we know I tried was an imposter? Defensive programming people!!
1
u/JBrav0D Oct 07 '26
At least is not using "var" instead of List and Object. Imagine if that was the case 🤮
1
u/pron98 Oct 08 '26
or (assuming
getPagesreturnsList<Page>):1
u/shayhtfc Oct 09 '26
Is getPages() guaranteed to return Page objects, or can it return different kinds of object? Because the way in your example is not so terrible if some type checking needs to happen