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

  • i_stole_ur_taco@lemmy.ca
    link
    fedilink
    arrow-up
    25
    arrow-down
    7
    ·
    4 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.

    • Ephera@lemmy.ml
      link
      fedilink
      English
      arrow-up
      16
      ·
      4 months ago

      Well, it also avoids running instantiation code, which could be doing all kinds of things. In theory, it could have a side-effect which modifies some of your application state or issues a log statement or whatever.

      Even if it doesn’t do anything wild right now, someone could change that in the future, so avoiding running such code when it’s not needed is generally a good idea.

    • Michal@programming.dev
      link
      fedilink
      arrow-up
      8
      arrow-down
      1
      ·
      4 months ago

      It’s like saying list.isEmpty() over list.getLength() == 0 is a picky optimisation.

      There’s a developer out there who coded this and they obviously don’t know what they’re doing. One day they’re gonna iterate all rows in the database to check if it’s empty. You have to flag these issues early and teach the newbies.