这是用户在 2024-8-7 8:01 为 https://rohan.ga/blog/java/ 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
Skipping ads...

rohan ganapavarapu

Java is an amazing language (better than rust)
Java 是一种了不起的语言(比 rust 更好)

DISCLAIMER: Let me preface this by saying my favorite language is Haskell and that I write toy languages for fun. I am a pl nerd so my opinion is valid. Also the title is like 20% bait.
免责声明:让我先说一下我最喜欢的语言是 Haskell,我写玩具语言是为了好玩。我是一个pl书,所以我的意见是有效的。此外,标题就像 20% 的诱饵。

My secret was always that I like coding in java, in some sort of masochistic way. Everything is dead simple. Everything is so verbose you can never forget what anything is. It is like sitting down and playing a relaxing game of stardew valley. It flows so easily. The feeling of mastery is so easy to attain.
我的秘诀一直是我喜欢用 java 编码,以某种受虐狂的方式。一切都非常简单。一切都是如此冗长,你永远无法忘记任何事情是什么。这就像坐下来玩一场轻松的星露谷物语游戏。它很容易流动。掌握的感觉是如此容易获得。

Coding in rust is like playing chess and league at the same time. It is the same mental exertion and the same insufferable people yelling at you on the internet. The semantics are cool. I love how it borrows stuff from functional languages. But can I tell you a secret? Java gets 75% of the way there with 10% of the complexity.
在 rust 中编码就像同时下国际象棋和联赛。这是同样的脑力消耗和同样难以忍受的人在互联网上对你大喊大叫。语义很酷。我喜欢它从函数式语言中借鉴东西的方式。但是我能告诉你一个秘密吗?Java 以 10% 的复杂度完成了 75% 的工作。

Error handling is definitely better in Rust, but it is closer than most think. The way errors get buried and propagated up the call stack can be confusing. In complex programs it can be missed. Pattern matching is better in Rust, but is closer than most think. Java records, java switch statements, Optional<>, streams, they all basically make up for the OOPiness of the language and are all pretty straight forward. Also the concurrent data structures in java are much nicer to use than in rust.
在 Rust 中,错误处理肯定更好,但它比大多数人想象的更接近。错误被埋藏在调用堆栈中并向上传播的方式可能会令人困惑。在复杂的程序中,它可能会被遗漏。Rust 中的模式匹配更好,但比大多数人想象的更接近。Java 记录、java switch 语句、Optional<>、流,它们基本上都弥补了语言的 OOPiness,而且都非常简单。此外,Java 中的并发数据结构比 rust 中的并发数据结构更好用。

Learning rust is hard, it could not be the language AP CS is taught in. The first skill gap is the borrow checker, then lifetimes, then async, and then macros. The smallest, first skill gap, the borrow checker, was about 10 times harder than learning about Java classes for the first time, and about 3.5 times harder than grokking monads. I still can BARELY write complex async rust, and if I see or have to write a macro that is like a hair above the simplest examples I will quit the editor.
学习 rust 很难,它不可能是 AP CS 教授的语言。第一个技能差距是借用检查器,然后是生存期,然后是异步,然后是宏。最小的第一个技能差距,即借用检查器,比第一次学习 Java 类的难度大约 10 倍,比摸索单子的难度大约 3.5 倍。我仍然几乎不能写出复杂的异步生锈,如果我看到或不得不写一个像最简单示例上方的头发一样的巨集,我会退出编辑器。

I would say the most clusterfucked abstract class inheritance system design in Java would not even approach the most complex Rust system design. Then again, I have not worked in a legacy Java environment, but I think this argument is still valid. Rust simply does not scale as well as Java.
我想说的是,Java 中最混乱的抽象类继承系统设计甚至无法接近最复杂的 Rust 系统设计。话又说回来,我没有在遗留的 Java 环境中工作过,但我认为这个论点仍然有效。Rust 的可扩展性不如 Java。

I also hate the skill issue argument. Like you know what the ultimate skill issue is? You not knowing C well enough to make memory safe code. Forget C, you not going complete rollercoaster tycoon and writing some plain x86 assembly.
我也讨厌技能问题的论点。就像你知道最终的技能问题是什么吗?你对 C 的了解不够,无法编写内存安全代码。忘了 C,你不会像过山车一样大亨,写一些普通的 x86 汇编。

The whole point of a language is to be easy. Basically the Go philosophy (which I do like more than Java these days, although it does not have the functional stuff I want). Like the whole point of high level languages is to never encounter such skill issues. It is to write working code fast and in a maintainable way. Naturally you would want this to be easy.
一门语言的全部意义在于简单。基本上是 Go 哲学(现在我更喜欢 Java 哲学,尽管它没有我想要的功能性东西)。就像高级语言的全部意义在于永远不要遇到这样的技能问题。它是以可维护的方式快速编写工作代码。当然,你会希望这很容易。

It is so easy to write good java with a few guidelines. Duplicate 1-2x before abstraction. No inheritance unless you are sure, especially no more than one layer deep. Use Optional<> over null. Do not get cute with your abstractions. Java is so easy people feel the need to make it hard doing OOP gymnastics to build “elegant” systems.
只需一些准则即可轻松编写出好的 java。在抽象之前复制 1-2 倍。除非你确定,否则没有继承,尤其是不超过一层的深度。使用 Optional<> 而不是 null。不要对你的抽象概念感到可爱。Java 是如此简单,人们觉得有必要让 OOP 体操变得困难,以构建“优雅”的系统。

Now here are some cheap, easy arguments. More people know Java and more jobs need Java. Tooling is better for Java and the ecosystem is more developed. I absolutely love JetBrains IDEA and no amount of loser lua neovim (don’t come after me I use neovim for C, zig, and other low level languages) config for Rust will match IDEA’s support for Java. (also if the primeagen is reading this, lua sucks, don’t @ me, it is never enjoyable to write, imagine liking the language I used to code roblox hacks in when I was 9 years old).

I am going to admit, maybe I just do not know rust enough. But I promise I have coded more rust than most of you have, and I probably know it better than the average self proclaimed rust enthusiast. And yet, if I had to write a big grug “business logic” project, you would see me reaching for Java before rust.

The final thing about rust is that it is not even particularly elegant. Haskell is nice to look at, APL (BQN, UIUA) is nice to look at. When I want a puzzle I write in those languages. Because I admire the mathematical abstraction. Rust is marred with the compiler, marred with low level BS that makes it ugly. That does not make it satisfying in this way. If you love language design, or beauty in programming languages, Rust is not even that good. It is not beautiful, it is not as useful as people proclaim, and it occupies such a small use case that the language is mid.

Even when I do systems programming, like low level stuff, I just want to manage my own memory. I do not want to beg the compiler to let me write code, I just want to allocate stuff and put stuff into there. C is fine for this. I’ll try zig later.

And I am not saying rust is always bad. It has a small but limited number of use cases. For example I wrote a bot that helped buy things rather quickly in 2020-2021 (provided I was awake to do some captchas). I needed it to be performant, robust, and the scope was just simple enough (even though it was async) where I knew I would not need a lot of code (but a mediocre amount). It occupied the space just before the complexity curve went asymptotic. The other use case is for writing toy languages, it does that super well. At its best it feels like using a cnc mill to machine a precise gear. But most times you just need an injection mold and some plastic.

P.S. check out this fizzbuzz impl. I did in Java, I have not found a similar solution in Java ANYWHERE on the internet. It also shows the power of modern Java. (please do not actually use this in an interview) https://github.com/Ocean-Moist/FizzBuzz/blob/master/src/Main.java

P.S. plz write your applications in Go instead of Java, otherwise you might get brainwashed by RxJava. if this happens, no one has found a cure.