236
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
[-] koper@feddit.nl 26 points 3 months ago

So what is the reason for doing it that way?

[-] i_stole_ur_taco@lemmy.ca 22 points 3 months ago

I think this is just a picky optimization.

The first one runs the constructor to instantiate a new string, then gets its class (which is presumably a static property anyway). The second doesn’t have to run any constructor and just grabs the static class name from the type.

Maybe there’s more implementation nuance here but it seems like an opinionated rule that has zero effect on performance unless that code is being called thousands of times every second. And even then the compiler probably optimizes them to the same code anyway.

[-] spooky2092@lemmy.blahaj.zone 34 points 3 months ago

Maybe there’s more implementation nuance here but it seems like an opinionated rule that has zero effect on performance unless that code is being called thousands of times every second

It's good practice to get in the habit of coding to only do the things you want/need to do rather than hoping the compiler does it for you.

This particular constructor call may be light, but there may be constructors that have a lot of overhead. Or you might be running alongside 1000 other processes who said the same thing and you start to see performance degradation.

[-] zea_64@lemmy.blahaj.zone 3 points 3 months ago

These things add up if you're doing them all over a 1 million line codebase, by which point it's incredibly painful to claw back performance if you need it.

[-] ulterno@programming.dev 4 points 3 months ago* (last edited 3 months ago)

This seems like one of those cases where you don't want to be waiting until benchmarking.

It makes the code simpler anyway.

load more comments (4 replies)
load more comments (9 replies)
this post was submitted on 27 Mar 2025
236 points (99.6% liked)

Programmer Humor

24709 readers
407 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