this post was submitted on 23 Jan 2024
363 points (96.7% liked)

Fediverse

28750 readers
15 users here now

A community to talk about the Fediverse and all it's related services using ActivityPub (Mastodon, Lemmy, KBin, etc).

If you wanted to get help with moderating your own community then head over to [email protected]!

Rules

Learn more at these websites: Join The Fediverse Wiki, Fediverse.info, Wikipedia Page, The Federation Info (Stats), FediDB (Stats), Sub Rehab (Reddit Migration), Search Lemmy

founded 2 years ago
MODERATORS
 

Seems like an interesting effort. A developer is building an alternative Java-based backend to Lemmy's Rust-based one, with the goal of building in a handful of different features. The dev is looking at using this compatibility to migrate their instance over to the new platform, while allowing the community to use their apps of choice.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 65 points 11 months ago (3 children)

Why Java though ? Like really ? It's... Better than any other compiled language ?

[–] [email protected] 68 points 11 months ago* (last edited 11 months ago) (2 children)

Because modern Java is an OK language with a great ecosystem to quickly build web backends. And there are lots of java devs which means more potential contributors.

[–] [email protected] 27 points 11 months ago (2 children)

Exactly. It's also using Spring Boot, Hibernate, and Lombok. It looks just like projects at work. It might be the first fediverse project I contribute regularly to.

[–] [email protected] 16 points 11 months ago

+1 same

I tried to contribute to Lemmy, spent a few hours really confused by rust and gave up. I can meaningfully contribute to a Java/Spring project, not a rust one.

[–] [email protected] -1 points 11 months ago (1 children)

Spring Boot, Hibernate, and Lombok

Ah, yes. How about he kitchen sink and another 5000 dependencies to make Java bareable to code in? Actually lets skip Java cos it's an over-engineered cluster-fuck that considers verbosity a virtue.

[–] [email protected] 3 points 11 months ago (1 children)

If you don't want to contribute, don't. Do you think being a hater is helpful?

[–] [email protected] 1 points 11 months ago

It sooooooooothes my soul. 🙂

[–] [email protected] -4 points 11 months ago (2 children)

Hello world in Java = 500 lines of code.

Hello world in Rust = 3 lines of code.

Java is over-engineered corporate bullshit used by banks and Android development. Nobody programs Java for the fun of it.

[–] [email protected] 6 points 11 months ago (1 children)

Hello World is < 10 lines in Java. Just say you don’t know the language and go away.

Java runs the majority of corporate software out there, and it is very good at what it’s built for.

I’ll take Java over Python/Rust any day of the week

[–] [email protected] 2 points 11 months ago (1 children)
[–] [email protected] -1 points 11 months ago (1 children)

Java devs are the reason humanity will never have FTL drives.

[–] [email protected] 1 points 11 months ago

Oh right, that's a much less moronic take. Well done genius

[–] [email protected] 15 points 11 months ago (1 children)

Probably because everyone knows it and its more predictable

[–] [email protected] 16 points 11 months ago (1 children)
[–] [email protected] 5 points 11 months ago (1 children)

If you say the function should only recieve one argument and returns always boolean. It is predictable to only allow the wanted args and forces you to return a boolean.

For example in a less predictable programming language e.g. Python: I can do all above but python does not stop anyone to put more or less arguments to a function, or a developer not adding typehints or not complying to them and return a string instead of a boolean.

But i had it wrong rust is similar to java on that part.

But still it is a lot more popular and easier to start with. So there will be a lot more contributor to sublinks than lemmy ever had.

[–] [email protected] 12 points 11 months ago (1 children)

Well in that sense Rust is even more predictable than Java. In Java you can always get back exception that bubbled up the stack. Rust function would in that case return Result that you need to handle somehow before getting the value.

[–] [email protected] -1 points 11 months ago (1 children)

That i dont understand? How can it be a result that i need to handle? If its not correct than java will throw an error. ( As expected, shit in shit out )

[–] [email protected] 4 points 11 months ago* (last edited 11 months ago) (1 children)

It's a great and probably the best error system I've seen, instead of just throwing errors and having bulky try catch statements and such there's just a result type.

Say you have a function that returns a boolean in which something could error, the function would return a Result<bool, Error> and that's it. Calling the function you can choose to do anything you want with that possible Error, including ignoring it or logging or anything you could want.

It's extremely simple.

[–] [email protected] 1 points 11 months ago (2 children)

If I except a boolean, there is an error and get a Result, is Result an object? How do I know if I get a bool or error?

[–] [email protected] 2 points 11 months ago (2 children)

You always get a Result. On that result you can call result.unwrap() (give me the bool or crash) or result.unwrap_or_default() (give me bool or false if there was error) or any other way you can think of. The point is that Rust won't let you get value out of that Result until you somehow didn't handle possible failure. If function does not return Result and returns just value directly, you (as a function caller) are guaranteed to always get a value, you can rely on there not being a failure that the function didn't handle internally.

[–] [email protected] 2 points 11 months ago (1 children)

If function does not return Result and returns just value directly, you (as a function caller) are guaranteed to always get a value, you can rely on there not being a failure that the function didn’t handle internally.

The difference being where you handle the error?

It sounds to me like Java works in kinda the same way. You either use throws Exception and require the caller to handle the exception when it occurs, or you handle it yourself and return whatever makes sense when that happens (or whatever you want to do before you do a return). The main difference being how the error is delivered.

Java has class similar to Result called Optional.

[–] [email protected] 1 points 11 months ago

Yeah I suppose ignoring unchecked exceptions, it's pretty similar situation, although the guarantees are a bit stronger in Rust IMO as the fallibility is always in the function signature.

Ergonomically I personally like Result more than exceptions. You can work with it like with any other enum including things like result.ok() which gives you Option. (similar to java Optional I think) There is some syntactic sugar like the ? operator, that will just let you bubble the error up the stack (assuming the return type of the function is also Result) - ie: maybe_do_something()?. But it really is just Enum, so you can do Enum-y things with it:

// similar to try-catch, but you can do this with any Enum
if let Ok(value) = maybe_do_something() {
  println!("Value is {}", value)
}

// Call closure on Ok variant, return 0 if any of the two function fails
let some_number = maybe_number()
  .and_then(|number| process_number_perhaps(number)) // this can also fail
  .unwrap_or(0);

In that sense it's very similar to java's Optional if it could also carry the Exception value and if it was mandatory for any fallible function.

Also (this is besides the point) Result in Rust is just compile-time "zero cost" abstraction. It does not actually compile to any code in the binary. I'm not familiar with Java, but I think at least the unchecked exceptions introduce runtime cost?

[–] [email protected] -2 points 11 months ago* (last edited 11 months ago) (1 children)

That's a kinda terrible way to do it compared to letting it bubble up to the global error handler.

You can also use optional in java if you want a similar pattern but that only makes sense for stuff where it's not guaranteed that you get back the data you want such as db or web fetch

[–] [email protected] 2 points 11 months ago* (last edited 11 months ago) (1 children)

You can bubble up the Error with ?operator. It just has to be explicit (function that wants to use ? must return Result) so that the code up the stack is aware that it will receive Result which might be Err. The function also has defined Error type, so you know exactly which errors you might receive. (So you're not surprised by unexpected exception type from somewhere deep in the call stack. Not sure about Java, but in Python that is quite a pain)

Edit: To provide an example for the mentioned db fetch. Typically your query function would return Result(Option). (So Err if there was error, Ok(None) if there was no error, but query returned no results and Ok(Some(results)) if there were results) This is pretty nice to work with, because you can distinguish between "error" and "no resurts" if you want, but you can also decide to handle these same way with:

query()
  .unwrap_or(None)
  .iter().map(|item| do_thing(item))

So I have the option to handle the error if it's something I can handle and then the error handling isn't standing in my way. There are no try-catch blocks, I just declare what to (not) do with the error. Or I can decide it's better handled up the stack:

query()?
  .iter().map(|item| do_thing(item))

This would be similar to exception bubbling up, but my function has to explicitly return Result and you can see in the code where the "exception" is bubbled up rather than bubbling up due to absence of any handler. In terms predictability I personally find this more predictable.

[–] [email protected] 1 points 11 months ago (1 children)

But like, what kind of error are you gonna handle that's coming from the DB, if it's something like a connection error because the DB is down, then you are shit out of luck you can't handle that anyway, and you probably shouldn't, not from the layer you are calling your DB from, that's a higher level logic, so bubbling Errors there make sense.

and if it's an "error" like findById doesn't always return something, that's what the Optional pattern is for.

what you have described to me seems like a worse version of the checked/unchecked exception system.

[–] [email protected] 1 points 11 months ago

But like, what kind of error are you gonna handle that's coming from the DB, if it's something like a connection error because the DB is down

I could return 500 (getting Error) instead of 404 (getting None) or 200 (getting Some(results)) from my web app.

Or DB just timed out. The code that did the query is very likely the only code that can reasonably decide to retry for example.

[–] [email protected] 1 points 11 months ago* (last edited 11 months ago) (1 children)

Here's some examples written on my phone:

match result {
    Ok(bool_name) => whatever,
    Err(error_type) => whatever,
}

if let Ok(bool_name) = result {
    whatever
}

if result.is_ok() {
    whatever
}

let whatever = result.unwrap_or_default();
let whatever = result?;

And there's many other awesome ways to use a Result including turning it into an Option or unwrapping it unsafely. I recommend you just search "Rust book" on your search engine and browse it. Here's the docs to the Result enum.

[–] [email protected] 1 points 11 months ago (1 children)

Ah, so it is like a wrapper enum, ok contains the data type you want and err the error object?

[–] [email protected] 1 points 11 months ago

Exactly! The other wrapper enum I named (Option) is the same kind of concept but with Some(value) and None.

[–] [email protected] 4 points 11 months ago (1 children)

It's probably got the best library/tooling ecosystem of any language out there. Certainly dwarfs Rust in that regard. Easier to find devs. Reasonably efficient thou not as much as Rust and typically less memory efficient. It's a perfectly good suggestion for a project like Lemmy. I'd reach for Java or Go before Rust for a project like this but you know, that's just me.

[–] [email protected] 2 points 11 months ago (1 children)

You see, Go would've been a better option than Java.

[–] [email protected] 3 points 11 months ago

It would have been a good option. As is Java. If you want to do it in Go, go ahead.