215
Code analyzer lore (lemmy.world)

This post was brought to you by this PMD rule.

Transcription

Why do we have this stupid code analyzer rule enabled anyway? Nobody writes code like this...

After telling them the lore why it's there:

You have seen such things before?

11 Times, as a matter of fact

you are viewing a single comment's thread
view the rest of the comments
[-] jbk@discuss.tchncs.de 15 points 5 days ago

the 2nd does that already

in the first you can't, as getClass() is not a static method (wouldn't even make sense if it was)

[-] mmddmm@lemm.ee 9 points 5 days ago

The GP is saying that String is a class already, you shouldn't have to call String.class.

Personally, I'm away from it for long enough that I don't remember either.

[-] Ephera@lemmy.ml 14 points 5 days ago

I don't believe there is much deeper of an explanation than "because the Java designers didn't implement support for that".

That feature is called "types as a first-class value" and you need to implement some special casing or an entire system in the language to make it work. Telling devs there's a special static variable .class is conceptually simpler to implement and understand.

[-] tdawg@lemmy.world 2 points 4 days ago

Is it simpler tho? You have to explain to someone that the Type is also an Object with the field .class on it. I feel like just saying it's a Type and you can reference Types directly is simpler. Idk maybe I've been currupted by type theory too much lol

[-] Ephera@lemmy.ml 2 points 4 days ago

Well, I think your idea would be simpler, if we weren't talking about Java.
Pretty much everything is an object in Java. It's only logical that a type would also be an object and have associated fields.

Similarly, what you're thinking of as "reference types directly" doesn't make sense in Java, because it lacks many of the systems to make that actually usable like a type. What you get from .class is a Class object, which you can't stick into a generic type parameter, for example.
It basically uses reflection to give you e.g. the name of that type and you can also instantiate an object of that type, if no parameters need to be passed to the constructor function.

And then, yeah, I think for explaining that you merely get an object which roughly describes the type, the separate .class field is a good idea.

this post was submitted on 27 Mar 2025
215 points (100.0% liked)

Programmer Humor

22131 readers
1875 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS