Hacker News new | past | comments | ask | show | jobs | submit login

One of the weirdest things about Java's URL API is this:

    equals
    
         Two hosts are considered equivalent if both host names can be resolved into the same IP addresses; else if either host name can't be resolved, the host names must be equal without regard to case; or both host names equal to null.
If you're doing bulk operations on stored URLs, you may just be sending out tons of DNS resolutions without ever calling an obvious networking API (like openConnection). If your DNS is down or slow, equals() will block.

I'm sure most Java programmer here has learned this lesson, but new programmers get surprised by this every time.




So I'm certainly not a Java developer and I barely know very much about urls, but isn't that a semantically surprising comparison as well? You can host wildly different websites on the same server/IP as a temporary or permanent arrangement, but I'm not sure I would expect that to make you group them together


That's an ancient API that is de facto deprecated by best practices. Java 1.0 was far from perfect regarding the design decisions.


From the docs:

> The java.net.URL constructors are deprecated. Developers are encouraged to use java.net.URI to parse or construct a URL.


Except it accepts some inputs that URI balks at.

I don't recall the exact case, but when I was trying to toleratly parse a URI, I fall back a URL and then convert it to a URI.


It is beyond surprising, it is horrifying.

Also, URI not URL has been the preferred choice for years and years and years.

But the footgun persists, for backwards compatibility


java.net.URL is deprecated. It’s a mistake from the 90s.


Learned, but completely forgot about this. What remained was a vague suspicion that perhaps the benefits of clear type separation that would be lost by using a plain string might not necessarily be worth it. (and then you eventually start rolling your own and then one day someone goes wild with the allowed characters liberties within the username:password prefix...)

To sum things up: never ever use java.net.URL, it's a bad http client and it's worse at everything else you might expect it to be.


It doesn't really make sense to use URL today (it does not even support encoding). URI can be used to identify the resource and then more explicit API can be used for I/O instead of URL.openConnection().


It's up there with "java.text.SimpleDateFormat is not threadsafe". SONAR catches that at least.


Just have to convince your teammates to install SonarLint or convince management to pay for SonarQube.

I’ve been successful at neither.


Then just claim the credit for fixing their bugs.


Yeah, a golden rule of Java dev is don't use URL. Use URI instead.

URL is one of those "it seemed like a good idea in the 90s" legacy warts.


That description reads like an xkcd comic.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: