Live data from Hacker News

Java 21 makes me like Java again

wscp.dev

691–700 of 777 posts

Re: Java 21 makes me like Java again

#691
post #665

Earlier quoted context omitted.

100% agree. You can create a HammerFactoryFactory to churn out HammerFactories in any language. But the ecosystem in Java (and C# is similar imo) promotes and encourages this type of problem solving. The one thing Java really does need is free standing (or namespaced) functions though. Sometimes I don’t want a class, what’s wrong with a function in a module or namespace in that case?

A class is a module/namespace. You can make a class just to have functions in it.

It’s not though. In lots of languages a namespace can span multiple files, whereas a class must be declared in a single file[0][1]. Modules can usually contain a collection of functions and classes. And namespaces can also contain multiple classes/structure/functions and sometimes modules depending on the language[2].

[0]: https://www.typescriptlang.org/docs/handbook/namespaces-and-...

[1]: https://learn.microsoft.com/en-us/cpp/cpp/namespaces-cpp?vie...

[2]: https://learn.microsoft.com/en-us/dotnet/fsharp/language-ref...

Re: Java 21 makes me like Java again

#692
post #488
post #202

Earlier quoted context omitted.

> Also the null thing hasn’t been an issue for over 10 years. I don't think I've ever seen a Java codebase that doesn't experience NPEs in prod, and that includes modern greenfield ones that make use of all the tools to combat it. > Just use null annotations and both your static checker and your IDE will catch every NPE. Yeah, or just catch it in code review, or just xyz... Or, just switch to a compiler that doesn't…

> I don't think I've ever seen a Java codebase that doesn't experience NPEs in prod Frankly, the last NPE I saw in production was many many years ago.

same, Kotlin doesn't compile if you try to deference null pointers

Re: Java 21 makes me like Java again

#693

Earlier quoted context omitted.

Agreed. Java injection frameworks are opaque and accomplish what they need to with overly powerful mechanisms because of the nature of the language. You don't see that sort of nonsense in python.

Nothing to do with the nature of the language, but with the nature of the program. If you're writing a few line script, you don't need a DI container. Once your program gets large, it becomes extremely messy without one. It's no surprise projects like [1] exist. [1] https://github.com/ets-labs/python-dependency-injector

That DI library is not pythonic at all.

There's nothing wrong with this at all, say:

    class MyClass:

        def __init__(self, my_dep1=None):
             if my_dep1 is None:
                 self.my_dep1 = get_my_dep1()  # Or potentially raise
             else:
                 self.my_dep1 = my_dep1

             [...]

Then to test:

    def test_my_class(): 
        mocked_dep1 = MagicMock()
        my_class = MyClass(my_dep1=mocked_dep1)
        [...]

Re: Java 21 makes me like Java again

#694
post #654

Earlier quoted context omitted.

Go is just a dumb subset hyped up as simple, but it is useless and slowly it will have to introduce all the remaining pieces in some ugly way as they didn’t plan with them ahead of time - see generics.

That is exactly what I used to think when I started with it. But I changed my opinion after 6 months with Go and eco-system. It's not ugly, it's different and much more concise.

It is more verbose than java by all objective counts.

Re: Java 21 makes me like Java again

#695

Earlier quoted context omitted.

Gradle is steaming pile of garbage. But since the zoomers are allergic to XML, Maven (actually fast) is slowly dying.

We are in the process of switching to Gradle at work and I'm not sure I like it. I used Gradle when building some OSS projects, but my experience wasn't that great.

Just make sure that whoever writes the majority of the build file actually understands gradle, at least its fundamentals. It really is not hard, and afterwards it is a really great tool that can significantly improve compile/CI times.

Re: Java 21 makes me like Java again

#696

Earlier quoted context omitted.

In django you're importing a default cache, a default storage etc, and write your code to the interface. In settings.py you wire it all up. It's basically the same, for testing you would have to mock the import or provide some implementation.

technically that's not dependency injection, but a global service locator.

Yeah I agree on that here.

The user doesn't write DI code for Django Class Based views. E.g. the view doesn't accept a Database upon instantiation.

Re: Java 21 makes me like Java again

#697
post #349

Earlier quoted context omitted.

No, there's a lot wrong with inheritance. It leads to all sorts of issues and while theoretically one can keep the tree 1 level deep, in practice it's too tempting to expand the hierarchy. This is one of Java's big issues. The other is reference equality as a default, pretty horrible. Records help but records are also limited in when they can be used.

> No, there's a lot wrong with inheritance. No, there is a lot wrong with bad code . No need to blame the language or any programming concept. Note that I say that for all programming languages, not just Java.

[..] a programming language designer should be responsible for the mistakes that are made by the programmers using the language.

It's very easy to persuade the customers of your language that everything that goes wrong is their fault and not yours. I rejected that...

- Tony Hoare

Re: Java 21 makes me like Java again

#698

Earlier quoted context omitted.

I used the word "and" to indicate that, yes.

I think a comma would make it less ambiguous. Starting that single though with "The" indicates to me that you don't think those are two different ideas. Compare: the idea that inheritance is really good idea and belongs everywhere. With: the idea that inheritance is really good idea. and belongs everywhere.

I think that would actually be an erroneous comma?

Re: Java 21 makes me like Java again

#699
post #485

The "Sealed classes" feature, as described here, just feels all wrong to me. They are saying that if you have a (normal) interface, anyone can create a new class implementing it. So if you do if (x instanceof Foo) { ... } else if (x instanceof Bar) { ... } ... then your code will break at runtime if someone adds a new class, as that code won't expect it. So the article is saying the solution is to use the new "sealed…

It doesn't always make sense to allow extending -- String is `final` for a reason (and one might even argue that final should be the default, and one should explicitly mark with `open` classes that can be subclassed). The stereotypical FP example for sum types are a List -- there you only have an Element (T head, List tail) and a Nil(). There is no point extending it, it would, in fact, result in incorrect code in co…

In a world of untrusted code and SecurityManagers, it was critical that strings be immutable so a data race couldn’t bypass a policy decision. The JVM doesn’t have immutable arrays, so string methods carefully protected the embedded array from tampering, and couldn’t be overridden.

Most classes don’t have this problem. The original authors of a class don’t know what I’m trying to do, and they don’t bear consequences if I (a consenting adult) get it wrong.

Re: Java 21 makes me like Java again

#700
post #427

Earlier quoted context omitted.

Do give async/await in C# a try, it has all the structured concurrency features other languages have to invent APIs and special syntax for :) (if you want to take a look at good structured concurrency, you might be interested in Swift implementation)

And don't forget to bookmark David Fowler guidelines to avoid all the gotchas that everyone falls into while using async/await.

This one? https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/b...
Post reply on HN