• xorium (unregistered)

    It's a pity he did not fix the atrocious curly brace style while he was at it. This is java unworthy.

  • Rob (unregistered)

    Does Java not have some form of the TryParseInt function?

  • Richard Brantley (unregistered)

    This is the kind of method I re-read multiple times just to try to back into the thought process that produced it.

  • (nodebb)

    "only to find 39 copies of this function, with minor variations." If only there were a way to reuse code in other parts of the project...

  • (nodebb) in reply to Rob

    No, Java doesn't have a tryParse built-in. There's just the parseInt used here that throws an exception (that Java forces a catch block for): https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Integer.html#parseInt(java.lang.String)

    But it's a common thing to exist in utility libraries that many projects would be including anyway, like Apache Commons Lang: https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/math/NumberUtils.html#toInt-java.lang.String-

    Of course, it might be implemented by just catching the exception of the built-in parseInt, so you're playing the Java exception performance penalty anyway for cases where it isn't a number: https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/math/NumberUtils.java#L1528

  • pedagog (unregistered)
    Comment held for moderation.
  • Rafael (unregistered)
    Comment held for moderation.
  • Duke of New York (unregistered)

    If trying you are, doing you are not.

  • (nodebb)

    With that information, Mark went and did a search for makeInteger in the code, only to find 39 copies of this function, with minor variations.

    TRWTF is that nobody will ever appreciate how the developer fully understood how the Beauty-of-Java™ is that all code is reusable (but only if less than 40 times) and also remembered something something methods can be overridden [so why not extend this to functions too?] AND managed to give "i" something to do besides be a counter in a for loop that got copypasta'd from .

    Personally, I'm kind of impressed that we don't just check if Bad Use Of Stringly Data might be an integer - we MAKE it an integer no matte what it might represent. That, my colleagues, is Brillant!!

    Addendum 2025-05-22 14:53: @Override "for loop that got copypasta'd from Stack Overflow"

    Maybe that should be @Overflow...?

  • (nodebb)

    I think this was done out of pure malice

Leave a comment on “A Trying Block”

Log In or post as a guest

Replying to comment #680484:

« Return to Article