- cross-posted to:
- fediverse@lemmy.ml
- cross-posted to:
- fediverse@lemmy.ml
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.
Browsing the code makes me angry at how bloated Java projects are:
package com.sublinks.sublinksapi.community.repositories; import com.sublinks.sublinksapi.community.dto.Community; import com.sublinks.sublinksapi.community.models.CommunitySearchCriteria; import com.sublinks.sublinksapi.post.dto.Post; import com.sublinks.sublinksapi.post.models.PostSearchCriteria; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import java.util.List; public interface CommunitySearchRepository { List<Community> allCommunitiesBySearchCriteria(CommunitySearchCriteria communitySearchCriteria); }
Every file is 8 directories deep, has 20 imports, and one SQL statement embedded in a string literal. 😭
Yup. Welcome to the world of Java where such things are not only silly but encouraged.
Most IDEs will handle the imports for you and auto collapse them
Ignoring the problem doesn’t make it better
How would you do “better” imports then?
*Vaguely wave arms towards the few dozens languages that do imports right*
I don’t mind Java personally, but let’s not pretend that its import syntax and semantics is at the better side of the spectrum here.
Just look at… Go, Haskell, TypeScript, Rust, even D has a better module system.
Isn’t Go just the equivalent of only doing asterisk-imports in Java, just without (and fair enough, Java has 0 need to do that 😂) repeating the
import
-keyword?There are multiple things in Go that make it better.
But just for giving a few thoughts about Java itself;
These are like “module 101” things. Like, you’re right that the IDEs nowadays do most of that, but IDEs also get it wrong (“oh you meant a THAT package instead of that other one”) and reading the code without an IDE is still a thing (code reviews for example) which means the longer the import section (both vertically and horizontally) the harder it is to work with. And if you don’t look at all imports carefully you may miss a bug or a vulnerability.
Also, Java is the only language I know of that has such a span on the horizontal. The memes about needing a widescreen monitor for Java is actually not a joke; I never had to scroll horizontally in any other language. To me that’s just insanity.
Also, if you’re gonna make it the whole universe as the root of your package structure, we already have DNS and URI/URLs for that. Let me use that!
And don’t get me started as only-files-as-packages while simultaneously having maybe-you-have-multiple-root for your code… makes discovery of related files to the one you’re working with very hard. Then of course the over reliance on generated code generating imports that might or might not exist yet because you just cloned your project…
deleted
And what’s bad about that? As in, how is the verbosity a negative thing exactly? More so because virtually any tool can be configured to default-collapse these things if for your specific workflow you don’t require the information.
At the same time, since everything is verbose, you can get very explicit information if you need it.
Here’s an example:
https://github.com/sublinks/sublinks-api/blob/main/src/main/java/com/sublinks/sublinksapi/community/listeners/CommunityLinkPersonCommunityCreatedListener.java
IMO that’s a lot of code (and a whole dedicated file) just to (magically) hook a global event and increase the subscriber count when a link object is added.
The worst part is that it’s all copy/pasted into a neighbouring file which does the reverse:
https://github.com/sublinks/sublinks-api/blob/main/src/main/java/com/sublinks/sublinksapi/community/listeners/CommunityLinkPersonCommunityDeletedListener.java
It’s not the end of the world or anything, I just think good code should surprise you with its simplicity. This surprises me with its complexity.
I find that Java is overly Verbose, but it’s much better than the alternative of underly verbose.
Java really follows the single class for single functionality principle, so in theory it makes sense to have these located in different classes. It should probably be abstracted to a shared method, but it shouldn’t be in the same file.
At least to me this looks like simplicity, but I’ve been writing Java in some capacity since 2012.
It’s not just the visible complexity in this one file. The point of it is to keep a subscriber count in sync, but you have that code I referenced above, plus:
LinkPersonCommunityCreatedEvent
LinkPersonCommunityDeletedEvent
LinkPersonCommunityCreatedPublisher
LinkPersonCommunityDeletedPublisher
And then there are things like
LinkPersonCommunityUpdated[Event/Publisher]
which don’t even seem to be used.This is all boilerplate IMO.
And all of that only (currently) serves keeping that subscriber count up to date.
And then there’s the hidden complexity of how things get wired up with spring.
And after all that it’s still fragile because that event is not tied to object creation:
@Transactional public void addLink(Person person, Community community, LinkPersonCommunityType type) { final LinkPersonCommunity newLink = LinkPersonCommunity.builder().community(community) .person(person).linkType(type).build(); person.getLinkPersonCommunity().add(newLink); community.getLinkPersonCommunity().add(newLink); linkPersonCommunityRepository.save(newLink); linkPersonCommunityCreatedPublisher.publish(newLink); }
And there’s some code here:
https://github.com/sublinks/sublinks-api/blob/main/src/main/java/com/sublinks/sublinksapi/api/lemmy/v3/community/controllers/CommunityOwnerController.java#L138C31-L138C50
final Set<LinkPersonCommunity> linkPersonCommunities = new LinkedHashSet<>(); linkPersonCommunities.add(LinkPersonCommunity.builder().community(community).person(person) .linkType(LinkPersonCommunityType.owner).build()); linkPersonCommunities.add(LinkPersonCommunity.builder().community(community).person(person) .linkType(LinkPersonCommunityType.follower).build()); communityService.createCommunity(community); linkPersonCommunityRepository.saveAllAndFlush(linkPersonCommunities);
that is able to bypass the community link service and create links in the repository directly, which would presumably not trigger than event.
Maybe there’s a good reason for that, but it sure looks fragile to me.
Yeah but that’s more on the coder. Like you indirectly say, they could just as well have had a single listener for community update events, since they all share the data structure for such events (I would assume, haven’t looked around too much).
And to me as a professional java coder, I will say it’s just not complex. The scaffolding you mentally discard after a week of working with Java unless you’re looking for something related to do the scaffolding - and then it’s cool that it’s all explicitly there, this has helped me countless times in my career and is one of the big strengths of such verbose languages - and beyond that and some design choices I would not have made that way related to naming and organization, there’s not much code there to begin with. In modern Java you might just do this in a lambda supplied in the place where you hook the events, anyways.
Fun fact, studies have found that the number of bugs in a program is proportional to the number of lines of codes, across languages. More lines of codes, more bugs, even for the same math and edge cases. So a more verbose language tends to have more bugs.
Interesting, but did this include web code and code only one person really ever works on?
Because on the pure backend level, I have observed the reverse over my career. The shorter, the “smarter”, the “cooler” the code, the more buggy it is. Not based on the code itself, but based on the developer inevitably leaving the company at some point. Meaning that what matters all of is a sudden is how verbose, how documented and how explicit the code is, because the bugs come from someone else messing with it as they get to take it over. It’s a legacy problem in a lot of ways.
Hence me saying that if solo projects are included, they probably tilt this massively as they’ll never really run into this problem. Like say, you just scan github or something. Of course most projects in there are solo, of course the more lines the more room for bugs.
But in an environment where you’re not solo coding, the issue is not getting the code to run and having it have no programmed bugs, but to be able to have someone else understand what you did and wanted to do in a meaningful amount of time, especially as they have to mutate your code over years and decades. Or maybe it’s just that the bugs no longer are what “matters”, as fixing code bugs is cheap compared to the endless hours wasted from “clever” code being unmaintainable. 🤷
I have a similar experience, in that anytime I heard someone hating on the verbosity of Java it was never the good devs the ones who can write a code that’s readable a few months later.
Its still better than any python project lol
Really? I find python imports to work very similar to cpp in practice.
But you really dont see what the function wants or requires or returns ( except with typehints, but they dont work most of the time and then its not enforced in any way )
Larger, modern python projects always use type hints, for this specific reason.
In the past you had PyDoc, which also scratched that itch.
Barring that, contributing to a python project is very difficult without an IDE that performs type checks for you (which is unreliable).
Correct! As i already contributing to a big ass python project at work. We will rewrite a Big Project from python to c# in under 1 month.
Just you wait until your developers learn about the
var
keyword - it’s going to be Python 2.7 PTSD incidents all over again 😂Isnt that already default on all variables? Its like a var(in js)?
Nope, was added to dot Net after the fact. Normally you declare each type by hand, e.g.
ArrayList<int> myCoolList = new ArrayList<int>();
vs
var myCoolList = new ArrayList<int>();
The second example is why the keyword was added, but now imagine you have a function call returning an unknown type, and then things will start to get super funky.
E.g.
var myCoolBook = BuildBookData(input);
…one step forward and then the same step back 😂 (disclaimer: I do actually like C#, though)