News for hackers. Stuff that matters.
Re:Selecting a programming language can be a form of prematu... by mumblemumble on Monday November 15, 2021 @10:41AM (#29228658)
A case study from my own personal experience:

We generally do everything in Java. Python is avoided because of all the usual complaints - dynamically typed, not fast enough, GIL, etc.

So, as a PoC, I decided to try rewriting one of our services in Python. And, compared to the Java one, it is:

Cheaper to write and maintain. About 1/10 as many SLOC. About 1/20 as many person-hours.

Has better static type checks. For example, Java's type checker cannot statically verify for null safety. The Python one I chose does do that. Note that, since I did choose to put type hints on everything, the productivity boost in question cannot be attributed to dynamic typing.

(That said, not all 3rd-party libraries have type hints, so, if you want to type check everything, you may have some yak shaving to do.)

Uses less RAM. About 1/2 as much.

Is faster. Admittedly I'm leaning on numpy, Cython, and friends for this. It may well be much slower for projects where that is not possible. But still, I think that the point about premature operation stands in this case.

(Disclaimer: I also have a colleague at $FAMOUS_COMPANY who tells me they are moving off of Python because they found the opposite of the above in many cases. Though I personally suspect that a mitigating factor is that their profit margins and scale are large enough that all the coefficients in their cost/benefit formula are wildly different from the norm.)

Re:Selecting a programming language can be a form of prematu... by mixedCase on Monday November 15, 2021 @10:51AM (#29228780)
Not that I'm a Java proponent, but have you tried doing the same rewrite effort on modern Java and with modern tools?

With newer tools and most important of all: your rewrite being a second iteration with the benefit of hindsight, you may notice that the language choice makes less of an impact than expected. IME language differences tend to shine the most when writing something the first time around and its expression capabilities shape the design.

Re:Selecting a programming language can be a form of prematu... by mumblemumble on Monday November 15, 2021 @11:07AM (#29228990)
I also rewrote a Python service in Java in an attempt to control for that. The results came out similarly in terms of development & maintenance effort. Unfortunately it wasn't a thing where a performance comparison made sense, though, so I didn't really do one.

My sense was that the big benefit was actually down to the libraries and syntactic sugar. Dataclasses, comprehensions, and generator functions all have a big impact, but so do things like Python just generally having more ergonomic libraries for talking to databases, producting/consuming REST APIs, and reading/writing data files.

Re:Selecting a programming language can be a form of prematu... by twic on Monday November 15, 2021 @11:48AM (#29229421)
You mentioned NumPy. NumPy is great, and there is basically no Java equivalent. So if you are doing the kind of work which NumPy can help with, it's going to be very hard work to beat it with Java, either in development speed or execution speed.

To put it another way, perhaps what you tested here was having a great array library vs not having a great array library! It would be interesting to test Python vs Java on a more generic task, where there isn't a huge advantage from a particular library or set of libraries.

It would also be great if someone would write a NumPy-style library for Java. My team would use that a lot!

Re:Selecting a programming language can be a form of prematu... by mumblemumble on Monday November 15, 2021 @01:08PM (#29230445)
Numpy was not a factor in the Python->Java PoC; it was a service that did no number crunching whatsoever. There, the specific library advantages were the psycopg, flask, requests, json, and csv packages. All of the (mainstream) Java equivalents are just kind of verbose and fiddly to work with compared to those.

I don't think this is entirely down to the language itself. A lot of it is cultural. The Java community tends to favor a more "enterprisey" style of programming and API design that, at least to my tastes, tends to be rather overwrought. That could change, in principle. I'm just not optimistic about it happening any time soon.

Re:Selecting a programming language can be a form of prematu... by twic on Monday November 15, 2021 @02:01PM (#29231168)
There is a lot of enterprisey crap, but there are also many decent libraries. In particular, i am pretty happy with the Java options for the things you mention - the PostgreSQL JDBC driver, JDBC itself, and HikariCP for pooling, the JDK HttpServer (no templating though, don't know if you need that here), the JDK HttpClient, Jakarta JSON with Joy as an implementation, and SimpleFlatMapper for CSV. None of them are perfect, but i am pretty happy churning out generic business service type things with them.

I wish there was some way i could take a sabbatical from my job and come and try to rewrite your service in what i consider effective modern Java. I am pretty optimistic it would be competitive with Python in effort and maintainability, but i have no way to prove it!

Re:Selecting a programming language can be a form of prematu... by BiteCode_dev on Monday November 15, 2021 @01:06PM (#29230406)
There is also no Django for Java, no Fast API for Java, no Typer for Java, etc.

Why ? Because it's easier to write great frameworks in Python.

It compounds.

Re:Selecting a programming language can be a form of prematu... by twic on Monday November 15, 2021 @01:48PM (#29230995)
Equally, there's no Hibernate for Python, no Spring for Python. And FastAPI looks basically like JAX-RS to me.

I don't think the lack of any of these things is because they're impossible, or even particularly hard. It's because the community did or did not have the need and energy to build them.

Remember that Java had Java EE forced on it quite early. That defined web development for a long time. Eventually Spring overthrew it, and became the new tyrant. Neither of those are quite like Django, or Rails, but they occupied the space that would have had to have been empty for a Django or Rails to emerge. I think this is rather unfortunate; it would be really useful to have a vibrant Django- or Rails-like framework in Java.

There's no Typer because there isn't much of a culture of building command-line tools in Java. There are some good command-line parsing libraries, but nothing with all the bells and whistles and extensions that Typer has, as far as i know.

Re:Selecting a programming language can be a form of prematu... by BiteCode_dev on Monday November 15, 2021 @02:24PM (#29231477)
> Equally, there's no Hibernate for Python, no Spring for Python. And FastAPI looks basically like JAX-RS to me.

Of course there is. In fact, SQLA is easier to use than Hibernate, and the ecosystem around is as rich. As for Spring, crossbar fits the bill.

> I don't think the lack of any of these things is because they're impossible, or even particularly hard. It's because the community did or did not have the need and energy to build them.

I've never say they are impossible. I've said they require less energy to build them in python :)

Re:Selecting a programming language can be a form of prematu... by slotrans on Monday November 15, 2021 @03:34PM (#29232330)
No one who writes Python has ever said to themselves, "I wish I had Spring." It's not necessary, and it wouldn't even be beneficial. The kind of dynamism that Spring brings to Java, Python already has.

Also Hibernate is terrible and should be erased from the Earth.

Re:Selecting a programming language can be a form of prematu... by kwhitefoot on Monday November 15, 2021 @01:45PM (#29230949)
> You mentioned NumPy. NumPy is great, and there is basically no Java equivalent.

Things like this are the greatest disappointment to me after fifty years of writing software. It should not be necessary that Java equivalent exists. It really ought to be possible to connect to libraries from any language by now.

Re:Selecting a programming language can be a form of prematu... by twic on Monday November 15, 2021 @01:49PM (#29231004)
After fifty years of writing software, i would have thought you would understand why that wasn't possible.
Re:Selecting a programming language can be a form of prematu... by kwhitefoot on Tuesday November 16, 2021 @08:35AM (#29240369)
Perhaps you could enlighten me as to why it isn't possible? I do understand that it would be difficult.
Re:Selecting a programming language can be a form of prematu... by mumblemumble on Monday November 15, 2021 @10:48PM (#29236549)
WinRT is an interesting thing on that front, though I am not aware of a non-Windows equivalent, and so it's never really going to see much open source adoption, let alone support on other platforms.
Re:Selecting a programming language can be a form of prematu... by twic on Tuesday November 16, 2021 @11:46AM (#29242742)
Back in the '90s, when we still remembered how to dream, we had CORBA.

CORBA got a lot of bad press, but towards the end, it was actually pretty decent. Certainly the best way to do the thing it was trying to do!

Re:Selecting a programming language can be a form of prematu... by Mikeb85 on Monday November 15, 2021 @02:11PM (#29231304)
What do you mean? JBlas exists. Colt exists. There's probably others but I'm not super up to date on Java things.

Numpy is just wrappers for BLAS and LAPACK.

Re:Selecting a programming language can be a form of prematu... by plafl on Monday November 15, 2021 @02:58PM (#29231903)
> Numpy is just wrappers for BLAS and LAPACK.

Certainly not. I encourage you to have a look at the API of BLAS/LAPACK to perform basic operationa. And that just scratches the surface.

Re:Selecting a programming language can be a form of prematu... by Mikeb85 on Monday November 15, 2021 @04:13PM (#29232857)
https://numpy.org/doc/stable/user/building.html

It is. Maybe it's a very fancy wrapper.

Re:Selecting a programming language can be a form of prematu... by mumblemumble on Monday November 15, 2021 @04:27PM (#29232999)
I would argue that numpy is a fancy wrapper for BLAS in the same way that a Spitfire is a fancy wrapper for a Rolls-Royce Merlin 61.
Re:Selecting a programming language can be a form of prematu... by xapata on Monday November 15, 2021 @11:17AM (#29229098)
I suspect there's some algorithmic choice hidden in a library in this comparison, where the Python library has the right choice for the job and the Java library has the wrong one.
Re:Selecting a programming language can be a form of prematu... by tmp_anon_22 on Monday November 15, 2021 @12:39PM (#29230053)
Are you as familiar with Java as you are Python?
Re:Selecting a programming language can be a form of prematu... by mumblemumble on Monday November 15, 2021 @12:49PM (#29230173)
I'm much more familiar with Java. It's mostly what I get paid to write. Python's relegated to times when I can get away with it, which are relatively uncommon.
Re:Selecting a programming language can be a form of prematu... by killingtime74 on Monday November 15, 2021 @02:03PM (#29231208)
Kotlin Scala does all these and interface with Java to boot
Re:Selecting a programming language can be a form of prematu... by TheDudeMan on Monday November 15, 2021 @10:45AM (#29228698)
> Java's type checker cannot statically verify for null safety.

Check out NullAway.

> Uses less RAM. About 1/2 as much.

Were you on a modern JVM?

Re:Selecting a programming language can be a form of prematu... by mumblemumble on Monday November 15, 2021 @10:55AM (#29228826)
It's not just null. For example, Java much more frequently leaves you in a situation where you need to cast to/from `Object`. In Python, `Any` type hints can usually be avoided, because of union types.
Re:Selecting a programming language can be a form of prematu... by taeric on Monday November 15, 2021 @11:53AM (#29229489)
First, I want to make it clear that if you have a programming style that works for you, please don't take this as an attack against it. Keep using what works.

If you find yourself commonly having to cast to/from Object in Java, that is almost certainly something of your own doing.

Re:Selecting a programming language can be a form of prematu... by ubercore on Monday November 15, 2021 @12:04PM (#29229606)
What's the "Java way" of accomplishing something like you'd represent with Union or Intersection types?
Re:Selecting a programming language can be a form of prematu... by avita1 on Monday November 15, 2021 @03:41PM (#29232422)
Other comments have linked newer language features that make it easy. But for years, the Java Way of handling discriminated unions was to use the visitor pattern [1]. It's very verbose, and is an insane amount of typing unless your IDE is doing the typing for you, but it has the compile time guarantees that forces each caller to handle every type without instanceof/Object.

[1] https://dzone.com/articles/design-patterns-visitor

Re:Selecting a programming language can be a form of prematu... by taeric on Tuesday November 16, 2021 @10:09AM (#29241347)
Apologies for forgetting I had posted this... :(

My gut would be a visitor for the fully general case. Though, I would also accept that you are using the wrong language for the abstractions your are writing? My guess is it will depend on why you are needing/wanting one of those types.

For a lot of plumbing code, these aren't as necessary as stipulated. Yes, there could be some duplication of code. But, especially in a microservice landscape, most of that duplication will be each service doing their version of whatever you are doing.

Re:Selecting a programming language can be a form of prematu... by programmer_dude on Monday November 15, 2021 @12:28PM (#29229930)
There is none, without up or down casting. This is just some "guideline" that gets paraded from time to time (in OOP circles). On second thought, I am sure there's a "design pattern" for sum types out there. Why take the easy way out, am I right?
Re:Selecting a programming language can be a form of prematu... by dljsjr on Monday November 15, 2021 @12:25PM (#29229872)
A common interface with all of the required functionality exposed as interface methods. A Java person would argue that if you have a function that takes an object that can be two totally disparate things with no shared functionality then that’s a code smell.
Re:Selecting a programming language can be a form of prematu... by tomtheelder on Monday November 15, 2021 @12:36PM (#29230021)
Probably with a composite class I guess. So a union type of types A and B has members of types A and B, and a non-nullable flag to indicate which one this instance is. Intersection type is just the same thing without the flag.

I don't write Java, though, just guessing.

Re:Selecting a programming language can be a form of prematu... by mumblemumble on Monday November 15, 2021 @01:00PM (#29230324)
It's one option. It has the advantage of structurally limiting the types that can be used in that spot. It has the disadvantage (compared to true union types) of offering no static help beyond that.

If you add a case, for example, you're 100% on your own to make sure that all code that interacts with the type is updated to handle the new case. Slip-ups will produce run-time errors rather than static ones.

It's also rather tricky (and awkward) to set things up such that consumers are forced to check the case value before attempting to coerce it.

All of this can be worked around with custom linter rules, but then that becomes its own maintenance burden.

My preference is to try and invert things such that you can rely on dynamic dispatch and "tell, don't ask." That eliminates the need to coerce things at run-time in the first place.

Re:Selecting a programming language can be a form of prematu... by Someone on Monday November 15, 2021 @12:59PM (#29230297)
You can also implement an union type as a sealed interface/abstract class with two classes that implement/extend it (https://openjdk.java.net/jeps/409, https://www.baeldung.com/java-sealed-classes-interfaces)
Re:Selecting a programming language can be a form of prematu... by ar_lan on Monday November 15, 2021 @11:46AM (#29229395)
> So, as a PoC, I decided to try rewriting one of our services in Python.

Not to your main point - but this line threw me off. I really thought you were discussing this as a "person of color", and I was so confused as the relationship of persons of color and Python. ^_^

Re:Selecting a programming language can be a form of prematu... by xrobledo84 on Monday November 15, 2021 @12:24PM (#29229860)
So what does PoC mean here then?
Re:Selecting a programming language can be a form of prematu... by twobitshifter on Monday November 15, 2021 @12:27PM (#29229910)
Proof of concept
Re:Selecting a programming language can be a form of prematu... by overtomanu on Monday November 15, 2021 @12:46PM (#29230132)
you have to also consider backward compatibility. If at all there is another version change in python like the one that happened from 2.0 to 3.0, maintenance effort would be more.
Re:Selecting a programming language can be a form of prematu... by BiteCode_dev on Monday November 15, 2021 @01:04PM (#29230375)
Java, Perl and Ruby all had the same.

In fact, in 2018, Java 8 was still massively used:

https://jaxenter.com/java-8-still-strong-java-10-142642.html

Then you have libs and frameworks, and they break compat too. The JS ecosystem basically broke everyone code for 10 years every 6 month.

It's not a Python thing.

Re:Selecting a programming language can be a form of prematu... by mumblemumble on Monday November 15, 2021 @01:23PM (#29230662)
According to the latest jrebel report, about 70% of Java shops are still on Java 8 as of earlier this year. The changes with Java 9 certainly created plenty of work for me. Java's famous claim to backward compatibility mostly concerns the JVM itself. They have done a killer job there. But the virtual machine is not the same thing as the platform that rests atop it.

This is not by way of complaining about Java 9. The changes are good and needed to happen. And the reasons why most people are still on Java 8 are also legitimate. Everyone's handling the situation in a fairly mature way, from what I've seen. The level of histrionics about Python 3, though... I've got a lot less sympathy for that.