• Ginny [they/she]@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    162
    ·
    13 days ago

    I also think Java is shit, but if you manage to get a NullPointerException while writing a hello world program, maybe anon is just not cut out for computers?

    • Scott@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      144
      ·
      13 days ago

      I’ve worked on a corporate project with multiple Java services, anon isn’t really exaggerating. Java can be a hell scape at times

      • taladar@sh.itjust.works
        link
        fedilink
        arrow-up
        52
        ·
        13 days ago

        They forgot to mention that production Java applications apparently need to log a certain minimum number of completely meaningless stacktraces per hour to work properly. Or at least I assume that is the case from the fact that all of them do that.

        • HackerJoe@sh.itjust.works
          link
          fedilink
          arrow-up
          30
          ·
          13 days ago

          Best with an old and vulnerable log4j on a Windows log server.

          We don’t know what’ll happen if we update. And we don’t know if the dude who coded it will answer our calls. YOLO!

        • Scott@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          9
          ·
          13 days ago

          You would be surprised, errors right out of the box on a freshly initialized project aren’t uncommon

          • babybus@sh.itjust.works
            link
            fedilink
            English
            arrow-up
            14
            ·
            13 days ago

            As I’ve been working with Java professionally for years, you’re right, I would be surprised, because that would be really uncommon.

    • kitnaht@lemmy.world
      link
      fedilink
      arrow-up
      82
      ·
      edit-2
      13 days ago

      I’m pretty sure Java doesn’t have pointers, so writing a hello world application isn’t gonna fuck up nearly that hard.

      The one thing he forgot though is that your source file is probably in the folder

      com/companyname/net/classes/factory/factoryfactory/worker/lib/bin/refresh/jdk/model/ui/closebutton/press.java

      And spread out among a bunch of other directories, and the java file is like…3 lines. But there are 10k files spread all around directories like this that are all 3 lines a piece with a class definition.

      • perviouslyiner@lemmy.world
        link
        fedilink
        arrow-up
        19
        ·
        13 days ago

        wait, so when .io gets deregistered, are a load of companies going to have to rename their root directories and rewrite all of their include statements?

        • kitnaht@lemmy.world
          link
          fedilink
          arrow-up
          43
          ·
          edit-2
          13 days ago

          Thankfully, despite naming them like that, it doesn’t actually seem to have any real purpose. Apparently they just wanted to make sure that different companies making different libraries didn’t accidentally use the same name for their project…

          • Hawke@lemmy.world
            link
            fedilink
            arrow-up
            11
            ·
            13 days ago

            That’s exactly the reason. And also no company is going through the bother to refactor that shit, so everything is named based on some other company 5 mergers and acquisitions ago.

      • Valmond@lemmy.world
        link
        fedilink
        arrow-up
        7
        ·
        13 days ago

        They have sort of pointers, like references, that can be null…

        You just “new” stuff to it and let the “garbage” collector deal with freeing stuff up. When it feels like it.

    • blue lion@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      50
      ·
      13 days ago
      • a hello world doesn’t need libraries in Java
      • installing JDK takes at most 5 steps, depending on the OS
      • a nullpointerexception is more likely the developper’s fault (unassigned value, calling a function on a null object)
      • IntelliJ is easy to install and modern (granted, other IDEs are very ancient)
      • developping GUI apps is a PITA, no matter the ecosystem (generally)

      The rest is more or less spot on (no idea about concurrency issues though)

      • Blue_Morpho@lemmy.world
        link
        fedilink
        arrow-up
        32
        ·
        13 days ago

        nullpointerexception is more likely the developper’s fault

        Of course it was the developer’s fault. But it’s absurd a language without pointers throws an error about pointers.

          • Blue_Morpho@lemmy.world
            link
            fedilink
            arrow-up
            9
            ·
            13 days ago

            Having error messages that match the language is actually helpful. A reference and a pointer aren’t exactly the same.

            Like if Rust output “invalid word size” on a type mismatch.

      • tyler@programming.dev
        link
        fedilink
        arrow-up
        8
        ·
        13 days ago

        The date of the post is from this week so it’s not accurate at all. Java does support main outside of a class now, and it doesn’t need to be static, or take args. You wouldn’t use JavaFX in this day and age either. Installing the jdk is absolutely nothing especially if you’re using IntelliJ as it will install it for you and manage everything. No library is even close to 3gb.

        This entire post sounds like it was written by someone that last touched Java in 2010.

        Source: am a Kotlin dev. Java sucks. None of these are the reasons why.

        • Ziglin@lemmy.world
          link
          fedilink
          English
          arrow-up
          2
          ·
          13 days ago

          Depends on the version of Java you have to use and most places still say to put it in a class because they’re outdated too. (Is anything about Java modern?)

    • chunkystyles@sopuli.xyz
      link
      fedilink
      English
      arrow-up
      35
      ·
      13 days ago

      I’ve been programming in Java professionally for 11 years. It’s not just embellishment, it’s outright lying.

      Threads giving you race conditions? All concurrent programming will do that if you’re shit at it.

      Java has come a long way. I will admit that UI in Java is terrible. I would never do that.

      • extremeboredom@lemmy.world
        link
        fedilink
        arrow-up
        16
        ·
        edit-2
        13 days ago

        It’s not accurate to accuse Anon of “lying,” when both their story and yours would point to the race conditions from threads being a symptom of someone who’s just learning the language.

        It’s not that serious though; because it’s a greentext, it is both artificial AND homosexual.

        • Maalus@lemmy.world
          link
          fedilink
          arrow-up
          4
          ·
          13 days ago

          It’s not someone who is learning, because “how to avoid race conditions” is like java concurrency 101. The entire thing is made to prevent that shit from happening. And it is incredibly easy to setup a “happens before” relationship.

      • crusa187@lemmy.ml
        link
        fedilink
        arrow-up
        11
        ·
        13 days ago

        Been coding Java for about 15 years now. Pretty much agree - anon’s primary mistake was using javaFX. From a junior dev perspective I can see why they’d do that, but Java isn’t really meant for building desktop applications, it’s meant to power web apps.

        What they should have done instead is create a backend restful web service and wire up a frontend rest client with something suited to web app ui dev such as angular or react. Java has some awesome frameworks built for it over the years, something like spring boot would make building that backend service trivial if you know how to use it. JAX-RS/Jersey or even servlets could be utilized for this instead, if you wanted to.

        Spring boot has some nice tooling for thread management, but Java also has pretty good options for this built in as well. As chunky mentioned, if you aren’t already versed in concurrency patterns, don’t try to perform concurrent operations or you’re gonna have a bad time. But do learn how to do this, because exploiting concurrency is one of the golden rules of good computing.

        • pivot_root@lemmy.world
          link
          fedilink
          arrow-up
          4
          ·
          edit-2
          13 days ago

          What they should have done instead is create a backend restful web service and wire up a frontend rest client with something suited to web app ui dev such as angular or react.

          If anon’s program was designed to work as a client for some external server or if Java had absolutely no GUI frameworks, that would be fine. But if anon’s goal was to create a simple desktop application, doing this would be the programming equivalent of a Rube Goldberg machine.

          With that approach, you also need to be really careful about how you do it. Fuck that part up and you can end up with the locally-hosted REST server listening on the device’s public network interface or vulnerable to CSRF from a web page opened in the user’s browser.

      • SorteKanin@feddit.dk
        link
        fedilink
        arrow-up
        3
        ·
        13 days ago

        Threads giving you race conditions? All concurrent programming will do that if you’re shit at it.

        Well, if you write Rust then there won’t be race conditions.

        • chunkystyles@sopuli.xyz
          link
          fedilink
          English
          arrow-up
          2
          ·
          12 days ago

          Rust is great, and I truly mean that. But saying you can’t get race conditions in Rust is just not true.

          And also, not everything is going to be written in Rust. When the company you’re working for needs a web application quickly, that’s better done in other languages.

          And also also, Spring has a lot of niceties when it comes to concurrency that make this much less of a point.

          • SorteKanin@feddit.dk
            link
            fedilink
            arrow-up
            2
            ·
            12 days ago

            You can’t get data races at least, and in practice it’s very difficult to get actual race conditions.

        • _stranger_@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          12 days ago

          Only in the context of data within the rust code. If you’re writing threaded rust that interfaces with external IO, for example, you can still get bit by a race condition. A person can absolutely be shit enough to hit a race condition in rust.

          • SorteKanin@feddit.dk
            link
            fedilink
            arrow-up
            2
            ·
            12 days ago

            interfaces with external IO

            How would you run into a race condition like this with safe Rust? You can’t share mutable file handles between threads for example. I’m not sure you’re correct in saying its still possible. Even if it is, it doesn’t sound easy.

    • Pencilnoob@lemmy.world
      link
      fedilink
      English
      arrow-up
      30
      ·
      13 days ago

      It’s much better today, but in 2010 that was 100% accurate.

      That being said, using Java as a first time programming language is like a 15 year old trying to fly an airliner to get a few blocks away to pick up some after school snacks. Obviously it’s way overkill. Sure you could get across town with it, but it’s probably 1000x more complicated than just a simple bicycle or even walking.

      Java is industrial strength for professionals. There’s absolutely no consideration made for educational usage.

        • Schtefanz@feddit.org
          link
          fedilink
          English
          arrow-up
          1
          ·
          edit-2
          12 days ago

          Which other eco system has more stable libraries, which are easy to install and redistribute and deploy?

          I would choose always choose the most boring language for the task. That the good thing about java that it is very boring and most likely won’t run into a obscure problem if you stay away from reflection and unsafe.

          • taladar@sh.itjust.works
            link
            fedilink
            arrow-up
            1
            ·
            edit-2
            12 days ago

            Pretty much any compiled language using native binaries is easier to install and distribute. Java applications are an absolute pain to get to run to the point that most need specialized wrapper scripts (usually in shell on unix platforms) and need to tinker with memory management parameters on almost all of them.

            • Schtefanz@feddit.org
              link
              fedilink
              arrow-up
              1
              ·
              edit-2
              12 days ago

              Do you mean something like c or c++ where you have to deal with big little edian, libc, utf8, platform specific threads, various compilers feature sets and a lot of undefined behaviour. Also if you want to distribute a library it is much harder because you a have to package it yourself or what for a distro to package your library or build a header only library which has to be manually updated every time a new version comes out. With maven you just have to upload it and you can use it in another project

              How often do thinker with the memory management in Java? Just use the g1 garbage collector, which will be fine most of the time if you have a problem just use another one.

              You can also compile the program with GraalVM to a native binary, also most Java programs are servers where it make sense to have a systemd service to configure the server

              • taladar@sh.itjust.works
                link
                fedilink
                arrow-up
                1
                ·
                12 days ago

                How often do thinker with the memory management in Java?

                As a sysadmin unfortunately every single one of those shitty Java projects forces me into dealing with that sooner or later.

      • SorteKanin@feddit.dk
        link
        fedilink
        arrow-up
        1
        ·
        13 days ago

        Java is industrial strength for professionals.

        Disagree. It’s an outdated tool today. Professionals would not choose it for new projects.

        • Schtefanz@feddit.org
          link
          fedilink
          English
          arrow-up
          2
          ·
          12 days ago

          Java isn’t outdated it is stable, today you don’t need JakartaEE you can use something like Spring Boot, Quarkus, or a microframework.

          Try to run an npm install on a 3 year old Angular or React project and most likely it won’t work

    • Lysergid@lemmy.ml
      link
      fedilink
      arrow-up
      29
      ·
      edit-2
      13 days ago

      95% exaggeration. Here is reality:

      • yeah you need main class coz it’s OO-language. Though, not required anymore, which should’ve been done once Java got scrip language capabilities (jshell) back in JDK 9. But as of today not required anymore
      • imports exist in most if not all languages. Gotta be insane writing them manually in 2010 let alone 2024
      • installing Java runtime (JRE) is as simple as installing any app. Though for installing JDK you need 5 mins for setting PATH. Think about JDK as like TSC or Webpack and JRE as a Browser. I’d argue installing and configuring JDK is simpler than TSC or Webpack
      • Unless you doing some non-trivial multi-threading your stack trace will tell you exactly where is your NPE. You gotta be as blind as my teammates to spend more than 1 minutes to find where it is coz it literally tells you file and line numer where Exception occurred
      • I mean, yeah if you use IDE from 2000 it will look like it. IntelliJ looks modern, though I don’t like the fact latest versions look like VSCode
      • I hardly reach 3G of deps from all 10 projects I have on my workstation.
      • IDK what anon means by ecosystem here, Java ecosystem is quite standard across the board. JDK(std lib), Maven/Gradle(deps, build, publishing), Spring Framework (all sorts of blueprints and solutions to standard app level problems), Hibernate/JPA (ORM), JUnit+Mockito (testing). These are tools and libs used in 90% of projects I worked on. Of course there will be more depending on project needs. Layers? It’s not like language imposes any layers whatsoever. It’s just common practice to have 3-4 layers and some glue in-between.
      • don’t do GUI in Java it sucks and will suck until Java gets string interpolation. Hopefully soon
      • concurrency is actually the only thing which is really bloated in Java. Which will change with next LTS version if I remember correctly. And it’s not that hard if you actually read the f manual and not just “try and hope”. Again it will become much more efficient and easier to follow soon. As of now - yeah, not trivial. But people mostly prematurely optimize, so karma
      • Java is kinda have 20 ways to do same thing but actually no. Java built with idea of providing simple building blocks. Then it provides more specific API built on top of those building blocks. It allows to have API which solves typical problems and provides capability to solve custom problems with those building blocks. People often confuse this as many ways to do one thing but it’s like saying “I can have byte array why I need string data type”. Those are different levels of abstraction

      Edit: typos

      • rumba@lemmy.zip
        link
        fedilink
        English
        arrow-up
        19
        ·
        13 days ago

        95% exaggeration if he is a real programmer.

        If he just tried to walk into Java knowing nothing or maybe PHP, and refused to RTFA, he might experience about 30% to 40% of that I just trying to do everything wrong.

    • schteph@lemmy.world
      link
      fedilink
      arrow-up
      29
      ·
      13 days ago

      Java is religiously backwards compatible. Modern java projects are not as enterprisey and boilerplatey, but, as jdk21 is backwards compatible with jdk1.3, you can still happily write code as if it’s 2003.

      Additionally, the java space is huge, so just wildly googling will probably not help you that much.

      • taladar@sh.itjust.works
        link
        fedilink
        arrow-up
        6
        ·
        13 days ago

        On the other hand back in Java 1.2, 1.3 and 1.4 days you frequently had companies install a specific patch level version because their applications worked only with that patch level. That was back when Java was actually popular for some reason.

      • Hawke@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        13 days ago

        Is that why every single application will only work with some ancient version of Java?(usually 8, sometimes 1.6 or 11)

        I can’t think of many cases where Java 21 is a drop in replacement, and I don’t think anyone actually used 17.

        • pivot_root@lemmy.world
          link
          fedilink
          arrow-up
          4
          ·
          edit-2
          13 days ago

          That’s entirely the fault of applications (or more likely their libraries) using internal APIs or JNI. As long as it isn’t doing anything screwy with reflection and class loading or using the sun.* packages that aren’t part of the language specification, Java bytecode compiled for ancient Java versions will still work on modern versions.

    • darthelmet@lemmy.world
      link
      fedilink
      arrow-up
      26
      ·
      13 days ago

      Only have a beginner perspective, but in school I did really well in intro CS class that used Python. 2nd class was in Java and it almost broke me I was so confused.

      • naeap@sopuli.xyz
        link
        fedilink
        arrow-up
        6
        ·
        13 days ago

        Hated Java in school

        But before Java I learned a bit of Pascal and C/C++, and I’m so fucking happy that I’m currently working with C++ in my job and not with fucking Java

        I still hate it with everything I’ve got …

    • Aganim@lemmy.world
      link
      fedilink
      arrow-up
      18
      ·
      13 days ago

      Anon pretty much sums up my experience with Java when I had to learn it in college 20-ish years ago. I’ll never get rid of my distaste for the language I’m afraid.

    • Valmond@lemmy.world
      link
      fedilink
      arrow-up
      17
      ·
      13 days ago

      I have developed in java and C/C++ (many years) and Anon is maybe exaggerating a bit but not lying, we all have been there more or less.

      Personally I hate how java forces you into bad architectural choices. Where is the unsigned int? Why isn’t an int a class BTW? Why the pass by copy for some, by reference for others? Where is multi inheritance? Lots of things are dumbed down or you have no choice in the matter.

      Sure didn’t help it was a power hungry beast moving at snail speed back in the day too.

      • tyler@programming.dev
        link
        fedilink
        arrow-up
        5
        ·
        13 days ago

        I mean they are lying because Java does support main as a top level function now. If this was written last year, sure. Also IntelliJ will literally install the jdk and manage it for you. Using asdf I haven’t had to touch the PATH for Java in probably over a decade (used sdkman before that).

        This entire post sounds like someone that last touched Java in 2010 and wanted to complain about it again. Java is shit, but for none of the reasons in the post.

        • anyhow2503@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          12 days ago

          It’s easy to accuse a noob of making the wrong choices when you have the experience necessary to make the right ones. There are a ton of outdated guides on the internet for every programming language. I’m almost certain there is some school kid downloading an old Borland C++ version right now, because the youtube video from 2010, regurgitating a tutorial from 2004 said so.

    • zod000@lemmy.ml
      link
      fedilink
      arrow-up
      13
      ·
      13 days ago

      Anon is absolutely exaggerating for comic effect. That doesn’t mean Java doesn’t have all of those problems though, it just isn’t as ridiculous as they made it out unless you get dragged into enterprise bullshit (then you’re in for it).

    • dohpaz42@lemmy.world
      link
      fedilink
      English
      arrow-up
      10
      ·
      13 days ago

      For the “simple” apps anon is talking about, they are exaggerating a lot. Though, when you get beyond the obligatory “Hello World” and “a window with a button”, the complexity does start to increase exponentially.

      Throwing in frameworks like Spring or JavaFX does make things more complicated. But it’s still relatively manageable once you get used to it and know what goes where and when.

      Now the whole configuration thing, that’s an art form best left to those hooded “seniors”. It’s good to learn eventually, but not when you first start out; lest you have a fetish for pain.

      Java has come a long way. It’s not the languages fault that people create monstrosities like AbstractJavaFinalSerializedFactory or whatever. But if you do want to be “good” at Java, you will want to learn about design patterns. It also doesn’t hurt to have a thick skin too, because you’ll be both criticized and made fun of for your choices. 😊

      • Aganim@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        12 days ago

        also doesn’t hurt to have a thick skin too, because you’ll be both criticized and made fun of for your choices.

        As somebody who started out with PHP I can fully relate to this. 😋

    • SpaceNoodle@lemmy.world
      link
      fedilink
      arrow-up
      6
      ·
      edit-2
      13 days ago

      Yes. Not an exaggeration.

      Edit: I say the same thing as a bunch of other commenters, yet I’m downvoted? Who’s got a stick up their ass today?

    • theneverfox@pawb.social
      link
      fedilink
      English
      arrow-up
      3
      ·
      13 days ago

      Concurrency isn’t bad, and package management (while maven is absolutely terrible to work generally), the dependency chains aren’t exceptionally bad. Getting it installed is easier than python on platforms it’s not already there on, not because it’s more portable, but because the installers do more for you. Portability is hard, they haven’t done it well but they’ve paved the default use case pretty well (although that works against you when you get to harder cases)

      But the rest is pretty close.

      The worst is the scaffolding, it’s literally superstition for years to gain the understanding as to why you’re doing it. I took two years of Java in high school before getting a degree - it was 4 years and halfway through a degree before I understood why I was making a class with a method main(string[] args). It works like that because your entry class calls the main method with a list of string arguments… I didn’t understand at all, because even though it’s simple it’s a special case, and I’d never seen anyone name the string array anything different, so I just copied and pasted it, never understanding it because I’d been told “you just have to have that” for do long

      Builds are arcane too - there’s still companies that only use netbeans in their build pipeline, Android still requires a specific an old Java version in conjunction with the IDE or a gradle build, at best a project uses maven (the package manager), which is xml based and full of arcane details that are best treated as a magic incantation to be copied exactly from elsewhere

    • tiredofsametab@fedia.io
      link
      fedilink
      arrow-up
      2
      ·
      13 days ago

      If you’ve never used Eclipse, which I assume it was referencing, it does feel old, clunky, and ugly out-of-the-box (to me at least). I tried to use IntelliJ wherever possible, but a previous company had a project that really didn’t like to run from it but would with a very specific setup in Eclipse (I don’t recall any details now more than 10 years later).

      There is a fair bit of boilerplate and bloat.

      I don’t remember UI stuff being so bad, but most of what I worked with was old Swing/AWT stuff. I did have to use JavaFX (I think it was?) once and remember something about it being frustrating, but it was for some existing thing I had to modify.

      I don’t mind Java so much, but it’s certainly not my favorite language.

      I don’t know how one gets a nullPointer when doing a hello world, though. I’m guessing this is embellishment or mashing together something later with their initial printing of hello since I think you’d come up with some other error in that process to getting something to print before nullPointer.

    • Zaphod@discuss.tchncs.de
      link
      fedilink
      arrow-up
      1
      ·
      13 days ago

      After working with somewhat modern Java using IntelliJ for 4 years, it’s alright. Null is the most annoying thing imo; just use Optionals instead

    • Serinus@lemmy.world
      link
      fedilink
      arrow-up
      54
      ·
      13 days ago

      C# is nearly the same, but much, much better.

      • It doesn’t (usually) come with the Java culture 8 layers of abstraction. This isn’t in the Java language. This isn’t in OO. Yet nearly every Java programmer makes things way more complicated than it needs to be.
      • It’s a prettier language. Similar syntax with less bullshit.
      • It’s open source
      • It’s still multiplatform. Modern dotnet / C# works on anything.
      • Both Visual Studio and Visual Studio code are great IDEs that blow Eclipse out of the water
      • It’s one of the most common business languages.
      • It’s going to be supported forever.

      If I could restrict the world of programming to two languages, it’d be C# and Rust. C# for most things and Rust for a lower level language.

      • PlusMinus@lemmy.world
        link
        fedilink
        arrow-up
        14
        ·
        13 days ago

        Nah, C# suffers from a lot of the same shit Java does. Needing everything to be a class is just no longer a good design choice (if it ever was). AOT support is still lacking. I don’t get, why it does not have typdefs. I think the solution / project structure is unnecessary and I could probably think of more stuff I dislike about C#. But imho, it still beats Java.

        Golang is my choice over C# any time. I strongly prefer how interfaces are handled and I actually like the error handling.

        • Serinus@lemmy.world
          link
          fedilink
          arrow-up
          16
          ·
          13 days ago

          Needing everything to be a class

          In 2015 they added scripting. If you’re making a real project, you should absolutely use classes. (It’s not that hard. Don’t do the Java shit.) But you can absolutely write one off scripts just fine.

          AOT support is still lacking.

          Publishing your app as Native AOT produces an app that’s self-contained and that has been ahead-of-time (AOT) compiled to native code. Source.

          • PlusMinus@lemmy.world
            link
            fedilink
            arrow-up
            5
            ·
            13 days ago

            I think you misunderstood my post. I am quite proficient with C#. I just think other languages do it better.

            AOT is not where it should be yet, because not all libraries have full stripping support.

        • pivot_root@lemmy.world
          link
          fedilink
          arrow-up
          3
          ·
          edit-2
          13 days ago

          I strongly prefer how interfaces are handled

          It’s better than Java, but they still chose to walk headfirst into the same trap that bites Java developers in the ass: associating interface implementations with the struct/class rather than the interface itself.

          When you have two interfaces that each require you to implement a function with the same name but a different signature, you’re in for a bad time featuring an abomination of wrapper types.

          Edit: Clarity.

          • Willem@kutsuya.dev
            link
            fedilink
            arrow-up
            8
            ·
            13 days ago

            On that last note, can’t you use the explicit interface implementation in C#?

            e.g.

            public class SampleClass : IControl, ISurface
            {
                void IControl.Paint()
                {
                    System.Console.WriteLine("IControl.Paint");
                }
                void ISurface.Paint()
                {
                    System.Console.WriteLine("ISurface.Paint");
                }
            }
            
            • pivot_root@lemmy.world
              link
              fedilink
              arrow-up
              2
              ·
              edit-2
              13 days ago

              Edit: I misread your comment as “like in C#” and wrote this as an answer to the non-existent question of “can’t you use explicit interfaces like in C#”

              I haven’t kept up with recent Java developments, but with Go, you’re out of luck. Interface implementations are completely implicit. You don’t even have an implements keyword.

              Edit: For Java, a cursory search suggests that they haven’t yet added explicit interfaces: https://stackoverflow.com/questions/19111090/does-java-support-explicit-interface-implementation-like-c

              • ඞmir@lemmy.ml
                link
                fedilink
                arrow-up
                2
                ·
                13 days ago

                He mentioned C#, which does let you explicitly choose to implement same-name functions of two interfaces with different code

                • pivot_root@lemmy.world
                  link
                  fedilink
                  arrow-up
                  3
                  ·
                  edit-2
                  13 days ago

                  For some reason, my brain inserted a “like” before “in C#”, and answered the question of “can’t you use explicit interfaces like in C#.”

          • TunaCowboy@lemmy.world
            link
            fedilink
            arrow-up
            3
            ·
            13 days ago

            just one more oop bro I swear

            Pure oopium. All oop ‘design patterns’ exist solely to overcome the inherent flaws of oop.

            • pivot_root@lemmy.world
              link
              fedilink
              arrow-up
              9
              ·
              edit-2
              13 days ago

              just one more oop bro I swear

              Didn’t understand my criticisms of Go and Java’s interfaces, or do you just enjoy LARPing as a senior programmer while living in a small world where the term “interface” strictly means object-oriented programming and not the broader idea of being a specification describing how systems can interact with each other?

      • spongebue@lemmy.world
        link
        fedilink
        arrow-up
        6
        ·
        13 days ago

        I only had one job that used C#, and it was the worst job I ever had. Even with the worst possible way to be introduced to the language, I still love it.

    • chunkystyles@sopuli.xyz
      link
      fedilink
      English
      arrow-up
      27
      ·
      13 days ago

      No. Every language has its haters. There’s a reason Java is so widely used. If you like it, keep at it.

      • tempest@lemmy.ca
        link
        fedilink
        arrow-up
        22
        ·
        13 days ago

        Yes and the reason is because millions of lines of production code were written and it isn’t worth rewriting them.

        Plenty of languages around now that don’t have 30 years of baggage and the specter of Oracle hanging over it.

        Now a days many businesses choose Go.

        • porous_grey_matter@lemmy.ml
          link
          fedilink
          arrow-up
          6
          ·
          13 days ago

          I don’t really like Go either, but it’s better than Java, and it’s pretty good for Big Software ™. In the end, every language has some problems. Java just has all of them.

        • AusatKeyboardPremi@lemmy.world
          link
          fedilink
          arrow-up
          5
          ·
          13 days ago

          Now a days many businesses choose Go.

          Many companies may choose something other than Java, but Java is still the behemoth.

          Such a decision is taken when the company is completely new or if it is a green field project.

          Even in the case of the latter, companies just choose to stick with their existing tech (read: expertise and experience of their tech teams)…

        • dukatos@lemm.ee
          link
          fedilink
          English
          arrow-up
          1
          ·
          13 days ago

          The only reason not to choose Go is legacy systems with SOAP. That shit will never die.

    • Sentient Loom@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      27
      ·
      13 days ago

      I like how straight-forward the syntax is. And it also seems orderly to have everything be a class. There’s a system to it.

      I’m using C++ for a project now and I like it in a similar way, but there’s more freedom (everything doesn’t HAVE to be a class). So with C++ I’ll never go back to Java (unless it’s for a job).

    • dev_null@lemmy.ml
      link
      fedilink
      arrow-up
      12
      ·
      edit-2
      13 days ago

      I thought I like Java until I tried Kotlin. It’s everything I liked about Java, but with everything wrong with it fixed.

    • Ginny [they/she]@lemmy.blahaj.zone
      link
      fedilink
      arrow-up
      9
      ·
      13 days ago

      I am a certified Java hater, but you’re allowed to like it. If simple and objected oriented is what you want, I can see the attraction, and it has a good and mature ecosystem.

      • bitchkat@lemmy.world
        link
        fedilink
        English
        arrow-up
        4
        ·
        13 days ago

        The ecosystem is java’s biggest asset. C# is actually a pretty decent language to develop in but the ecosystem just pales. Zookeeper for example doesn’t have an official client. But one guy ported the Java client but it hasn’t been updated in years. Maybe it’s recently because I moved on from that job.

    • SorteKanin@feddit.dk
      link
      fedilink
      arrow-up
      8
      ·
      13 days ago

      Honestly I would consider that a bit weird. At the very least, old-fashioned. If you like Java, it makes me think you haven’t tried a better more modern language to compare it with.

      • Malfeasant@lemm.ee
        link
        fedilink
        arrow-up
        1
        ·
        12 days ago

        Which would you suggest? (And if you say python, I will attack you with a pointed stick)

      • KoalaUnknown@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        12 days ago

        The first language I learned was Python and I hated it. I have not tried Rust yet but I have tried all the Cs.

        Edit: added missing not

        • SorteKanin@feddit.dk
          link
          fedilink
          arrow-up
          1
          ·
          12 days ago

          (I assume you left out a “not” there)

          You should definitely give Rust a shot. It is only conceptually similar to C++ but otherwise very different.

    • lurch (he/him)@sh.itjust.works
      link
      fedilink
      arrow-up
      5
      ·
      13 days ago

      No, Java has lots of merits. For example, once you know layout managers, you can have a resizable GUI app in no time. It’s the exact opposite of arranging things pixel by pixel. You just define “I want a grid of these buttons south and a big text field in the center” and Java will do the rest. I whip up apps like this for the silliest things, like noting which dungeon has what rotating boss this week in a game, so it’s more convinient than noting it in a text file.

  • AusatKeyboardPremi@lemmy.world
    link
    fedilink
    arrow-up
    41
    ·
    13 days ago

    I might have agreed a decade or two ago, when I knew no better. But today, I find the tribalism surrounding programming languages comical.

    I don’t particularly like Java, but I use it because it pays the bills. Similarly, I use C++ (which I prefer) when my work requires it.

    • SorteKanin@feddit.dk
      link
      fedilink
      arrow-up
      33
      ·
      13 days ago

      I don’t particularly like Java, but I use it because it pays the bills. Similarly, I use C++ (which I prefer) when my work requires it.

      I mean, anon is not arguing against that. They’re saying the language is shit regardless of how much it is used in business. I don’t think they are entirely wrong.

    • frayedpickles@lemmy.cafe
      link
      fedilink
      English
      arrow-up
      3
      ·
      12 days ago

      Tell us more ancient one, your heroic tale of “giving up against the endless weight of capitalism” is fascinating.

      • lurklurk@lemmy.world
        link
        fedilink
        arrow-up
        8
        ·
        12 days ago

        “giving up against the endless weight of capitalism”

        We just call it “having a job” nowadays

      • AusatKeyboardPremi@lemmy.world
        link
        fedilink
        arrow-up
        5
        ·
        12 days ago

        Love the dramatics.

        This ancient one has learned the art of pragmatism. A little time in the trenches of enterprise development can do that – turn passionate ideals into practical choices.

        Some days it’s C++, some days it’s Java, Python and so on. In the end, the code compiles, and the ancient one get paid.

        • Malfeasant@lemm.ee
          link
          fedilink
          arrow-up
          5
          ·
          12 days ago

          Some of us try to understand what we’re doing, rather than just copy/paste. It’s easy to discount how difficult learning the basics of something is when you’re already past it.

        • sugar_in_your_tea@sh.itjust.works
          link
          fedilink
          arrow-up
          4
          ·
          edit-2
          12 days ago

          And most IDEs will autogenerate it for you.

          That said, I think it highlights everything I hate about Java:

          public class MyClass {

          Why does it need to be a class? I’m not constructing anything?

          public static void main(String[] args) {

          Why is this a method? It should be a top-level function. Also, in most cases, I don’t care about the arguments, so those should be left out.

          System.out.println(“Hello world!”);

          Excuse me, what? Where did System come from, and why does it have an “out” static member? Also, how would I format it if I felt so inclined? So many questions.

          And here are examples from languages I prefer:

          C:

          #include “stdio.h”

          Ok, makes sense, I start with nothing.

          int main() {

          Makes sense that we’d have an entrypoint.

          printf(“Hello world”);

          Again, pretty simple.

          Python:

          print(“Hello world”)

          Ok, Python cheats.

          Rust:

          fn main() {

          Ooh, entrypoint.

          println!(“Hello world”);

          I have to understand macros enough to realize this is special, but that’s it.

          In C, Python, and Rust, complexity starts later, whereas Java shoves it down your throat.

      • babybus@sh.itjust.works
        link
        fedilink
        English
        arrow-up
        4
        ·
        12 days ago

        Thank you. If you bothered to read a 5 minutes tutorial instead of posting to 4chan, you could also reach this level of knowledge.

        • pinkystew@reddthat.com
          link
          fedilink
          English
          arrow-up
          3
          ·
          edit-2
          12 days ago

          Don’t be mad, you’re the one that commented lol. It’s like you’re choosing to be upset

          • babybus@sh.itjust.works
            link
            fedilink
            English
            arrow-up
            3
            ·
            12 days ago

            I thanked you for your reply and suggested reading a tutorial. How does that make me mad and upset? You’re acting weird.

  • Orbituary@lemmy.world
    link
    fedilink
    English
    arrow-up
    37
    ·
    13 days ago

    My old boss is one of the 3 initial creators of Java. He ran our department the same way this greentext reads.

    He was also a paedo. You can figure out the rest if you dig.

    • rekabis@lemmy.ca
      link
      fedilink
      arrow-up
      6
      ·
      13 days ago

      C# has had string interpolation for, what - nearly a decade, now? It arrived with C# v6, which was released in 2015.

      Meanwhile Java just pulled their implementation out of the latest beta earlier this year because they couldn’t get it to work right.

      Don’t know about you, but I think that Java is largely resting on its laurels as of late. That the only real reason to go for it is it’s third-party library system, and not much more.

    • drake@lemmy.sdf.org
      link
      fedilink
      arrow-up
      5
      ·
      13 days ago

      C# is pretty good generally - I know it far better than any other and it pays my bills! - but it certainly has its weak points. Particularly around the newer features, a lot of them feel really rushed and just kind of shitty.

      The one I hate the most is the whole “nullable” pattern. It’s a total mess. Having to mark up files as #nullable enable, having to mark methods with a bunch of attributes, and the way that it works differently if it’s a value type or a reference type, it’s just so half-baked.

      If you spend some time with a more modern language like Rust or Swift then you’ll quickly start to notice C#’s weaknesses.

      • KubeRoot@discuss.tchncs.de
        link
        fedilink
        English
        arrow-up
        1
        ·
        12 days ago

        I feel like you’re doing something wrong with the nullables… I’m pretty sure you don’t need to mark up files, you can just enable it on the whole project? I’m not sure about the attributes, you might have a point there, but it just makes sense for value vs reference types IMO, since value types are already implicitly different in terms of nullability.

        But yeah, I can imagine it’s half-baked, since nullable reference types (that’s the name, previously reference types were just nullable by default with no extra features) are a more recent addition to the language, one that wasn’t built with them in mind.

        • drake@lemmy.sdf.org
          link
          fedilink
          arrow-up
          1
          ·
          12 days ago

          If you create a new project from scratch, yes, you can enable it project-wide. If you have a project which has a bunch of code predates nullable reference types, and you enable it project wide, you’ll have a billion warnings about it. Also, they’re warnings and not errors by default, which just encourages developers to either ignore or suppress them.

          So the reality is that you need to remember when you’re making new classes to add the attribute, and then deal with external stuff - which isn’t always clearly marked whether it’s nullable or not unless it’s using attributes, by the way… just such a total mess.

          They should have just gone with something more like Rust’s “Option” type. Would have been clearer for codebases that have to deal with a mix. They also could have clearly and decisively deprecated non-nullable reference types and just told people they were going to remove support in some future version so we could all migrate to them properly like we’ve done for .NET Core/.NET 5+.

    • EnderMB@lemmy.world
      link
      fedilink
      arrow-up
      5
      ·
      12 days ago

      After close to two decades of programming, C# is still the best language I’ve used. While some of the newer features seem a bit weird, I’d say it’s one of the few languages that has never got in the way and has just let me write code that made sense. Even with all the improvements Java has made over the years it’s still nowhere near as good as what C# was like maybe 15 years ago.

      The same goes for everyone’s other “fav” language, Python. Ruby has been a better beginner scripting language than Python for many years, and while Rails is definitely a ghetto, as a language Ruby is great at teaching great programming fundamentals.

    • sugar_in_your_tea@sh.itjust.works
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      12 days ago

      C# is nicer Java, but I think it’s still fundamentally a poor language.

      Rust master race:

      fn main() {
          println!("Hello world!");
      }
      

      Unfortunately, the time you save typing you’ll spend compiling, so there’s that…

  • lurklurk@lemmy.world
    link
    fedilink
    arrow-up
    30
    ·
    12 days ago

    Hello World

    30 minutes of boilerplate

    writing imports

    $ cat <<EOF > Hello.java
    public class Hello {
      public static void main(String args[]) {
        System.out.println("Hello world!");
      }
    }
    EOF
    $ java Hello.java
    Hello world!
    

    ok

    • MooseTheDog@lemmy.world
      link
      fedilink
      arrow-up
      26
      ·
      12 days ago

      Welcome to java, we have a couple unconventional ways of doing things, but overall I’m like every other mainstream oo language.

      People: AHH! Scary!

      Welcome to python. your knowledge of me wont help you elsewhere as my syntax is purposefully obtuse and unique. Forget about semicolons, one missed space and your code is as worthless as you after learning this language.

      People: Hello based department

      • Classy@sh.itjust.works
        link
        fedilink
        arrow-up
        9
        ·
        12 days ago

        Oh my god I got fucked by a python script once because of a single space. It took forever to figure out what went wrong

        • TonyOstrich@lemmy.world
          link
          fedilink
          arrow-up
          3
          ·
          edit-2
          12 days ago

          I refuse to code in Python without a really good IDE and linting like PyCharm. When using PyCharm it’s very rare I have issues like this, because it catches them in one way or another, but I notice it catches those kinds of issues a lot when I’m coding soooooooo…

          I have also setup the IDE to specifically color code comments like

          ’ # End If and ’ # Next

          in the same style as their beginning statements as I find it much easier to visually scam through code when they are present.

        • greenkarmic@lemmy.ca
          link
          fedilink
          arrow-up
          2
          ·
          11 days ago

          You can’t use Python without a linter. I have everything setup in vscode to use tabs yet copilot autocomplete insists on inserting random spaces everywhere creating indentation errors. The linter is essential to quickly see and fix them.

      • lurklurk@lemmy.world
        link
        fedilink
        arrow-up
        3
        ·
        12 days ago

        Python has its drawbacks but it also has a pretty useful standard library so as a language for small scripts, one can do much worse

      • JackbyDev@programming.dev
        link
        fedilink
        English
        arrow-up
        3
        ·
        edit-2
        12 days ago

        **kwargs

        “No, I don’t use type annotations because they don’t actually do anything. In fact I purposefully give this parameter different types for different behaviors. How is that confusing?”

    • JackbyDev@programming.dev
      link
      fedilink
      English
      arrow-up
      7
      ·
      12 days ago

      This is getting a little better nowadays.

      > cat Hello.java
      void main() {
          System.out.println("Hello, World!");
      }
      > java --enable-preview Hello.java
      Hello, World!
      

      Things to notice:

      1. No compilation step.
      2. No class declaration.
      3. Main method is not public static
      4. No String[] args.

      This still uses preview features though. However, like you demonstrated already, compilation is no longer a required step for simplistic programs like this.

      • cashew@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        12 days ago

        Microsoft Java is a one-liner these days.

        > cat program.cs
        Console.WriteLine("Hello, World!");
        > dotnet run
        Hello, World!
        
      • Valmond@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        12 days ago

        System.base.stuff.output.out.printfunctions.println

        Or so it felt every time you wanted to dump something into the console…

      • مهما طال الليل@lemm.ee
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        12 days ago

        Main method is not public static

        It must be somewhere under the hood. Otherwise, it wont be callable and it would require an instance of an object to call. Unless the object here is the Java environment?

        No String[] args

        They are just optional I’m sure, like C and C++. You still need them to read command line arguments.

        All in all, these syntax improvements are welcome. I already moved on to Kotlin for Android development though.

        • JackbyDev@programming.dev
          link
          fedilink
          English
          arrow-up
          2
          ·
          edit-2
          12 days ago

          Main method is not public static

          It must be somewhere under the hood. Otherwise, it wont be callable and it would require an instance of an object to call. Unless the object here is the Java environment?

          No. From JEP-445:

          If an unnamed class has an instance main method rather than a static main method then launching it is equivalent to the following, which employs the existing anonymous class declaration construct:

          new Object() {
              // the unnamed class's body
          }.main();
          

          No String[] args

          They are just optional I’m sure, like C and C++. You still need them to read command line arguments.

          Without the preview feature enabled, it is not an optional part of the method signature. It specifically looks for a main(String[]) signature.

          • مهما طال الليل@lemm.ee
            link
            fedilink
            arrow-up
            2
            ·
            12 days ago

            I am not in the mood to read a technical document, but I don’t think the resulting binary/byte code should be different between the two “hello world” programs. But then again, why not?

            Without the preview feature enabled, it is not an optional part of the method signature. It specifically looks for a main(String[]) signature.

            Ah ha! So that’s what’s going on here. They almost got it right. They had the potential to make a lot of the boilerplate optional or implicit under relevant circumstances, but instead the language has two explicit switchable modes.

            Can I write a Java application in “preview feature”?

            • JackbyDev@programming.dev
              link
              fedilink
              English
              arrow-up
              7
              ·
              edit-2
              12 days ago

              I mentioned this uses preview features twice in the first comment regarding this, so I don’t know why you’re "ah ha"ing. Also you don’t need to read the technical document, I’ve quoted the entirety of the relevant text. I provided it as a citation.

              You seem confused about preview features. It’s not a switchable mode to reduce boiler plate. I find the name very clear, but here is more information. From JEP-12

              A preview feature is a new feature of the Java language, Java Virtual Machine, or Java SE API that is fully specified, fully implemented, and yet impermanent. It is available in a JDK feature release to provoke developer feedback based on real world use; this may lead to it becoming permanent in a future Java SE Platform.

              As an example, JDK 17 added pattern matching for switch statements as a preview, and by JDK 21 it was added as a full fledged feature that doesn’t require usage of the enable preview flag. Presumably in some future release of Java this feature will not require the usage of a flag.

              • مهما طال الليل@lemm.ee
                link
                fedilink
                arrow-up
                1
                ·
                edit-2
                11 days ago

                It is pretty late for me. Sorry. And thank you for your patience. Repeating it three times helped.

                It will be interesting to find out if the resulting binary is the same or not and what’s possible once it matured.

                • JackbyDev@programming.dev
                  link
                  fedilink
                  English
                  arrow-up
                  3
                  ·
                  12 days ago

                  Yes, because it’s genuinely not a static method. It’s an instance method. Also the signature is different. It’s not some sort of mere syntactic trick that translates void main() to public static void main(String[] args).

    • meowMix2525@lemm.ee
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      12 days ago

      I got the impression they skipped the hello world cause it was too easy and they wanted to get right to writing their app, so they moved on to more advanced stuff without having a real grasp of the basics

    • unalivejoy@lemm.ee
      link
      fedilink
      English
      arrow-up
      34
      ·
      edit-2
      13 days ago

      They only had to deal with LWJGL. The corporate java world has to use Spring.

      Edit: They also had to deal with all the fans saying they should’ve written it in C#.

      • dev_null@lemmy.ml
        link
        fedilink
        arrow-up
        6
        ·
        13 days ago

        And much of the confusion and frustration at “Java” is actually because of Spring, or the “enterprise” nonsense making everything unnecessarily complex. You can just… write Java without any of that.

        You shouldn’t though, because Kotlin exists, which fixes everything that’s wrong with Java while still being 100% compatible, so even in legacy projects you can mix and match and write new code in Kotlin without needing to rewrite any of the existing Java.

        • Lysergid@lemmy.ml
          link
          fedilink
          arrow-up
          1
          ·
          12 days ago

          It’s not corporate world, it’s web. Spring de facto is the only modern way to build web services and integrations in Java and Spring comes with DI because it’s the way to build efficient extendable framework.

          • dev_null@lemmy.ml
            link
            fedilink
            arrow-up
            1
            ·
            edit-2
            12 days ago

            Micronaut and Vert.X also work, and with Kotlin you unlock that ecosystem as well, for example Ktor. One could argue whether Spring is still a modern framework. It works very well, but there is a lot of “magic” and hard to understand annotations with Spring that make it harder to learn and debug than it could be.

            Of course the reality in enterprise environments is that change is often very difficult and such changes are a hard sell when you already have millions of lines of Spring code.

            But if you are not locked to Spring, there are better options. DI being build in is another negative to me. Spring does everything, and any project using it becomes a “Spring project”. Which robs you of any choice. If you use Ktor for example, it’s only a library, not a framework, and only does the web component. You choose your own DI library that works for you, you choose your own serialization, you choose your own persistence/database solutions, and you can replace Ktor with something else 3 years down the line, if needed, without touching any of the other parts if the project.

  • Batman@lemmy.world
    link
    fedilink
    arrow-up
    30
    ·
    12 days ago

    My inner mathematician respects Java. The first step in any problem is defining your universe

    • Mr_Blott@feddit.uk
      link
      fedilink
      arrow-up
      6
      ·
      13 days ago

      Really want to go to La Scala one day but I looked it up and the tickets are like 500 euros. An eclipse is much cheaper

  • WormFood@lemmy.world
    link
    fedilink
    arrow-up
    21
    ·
    13 days ago

    object orientated programming is the wrong idiom for almost all problems, and even in the few cases where it makes sense, you have to be very careful or it’ll hurt you

    • 31337@sh.itjust.works
      link
      fedilink
      arrow-up
      10
      ·
      13 days ago

      Idk. Maybe it’s because I learned OOP first that it makes more sense to me; but OOP is a good way to break down complex problems and encapsulate them into easily understable modules. Languages like Java almost force everyone on the project to use similar paradigms and styles, so it’s easier for everyone to understand the code base. Whenever I’ve worked on large non-OOP projects, it was a hard-to-maintain mess. I’ve never worked on projects such as the Linux kernel, and I’m hoping it’s not an unmaintainable mess, so I’m pretty sure it’s possible to not use OOP on large projects and still be maintainable. I am curious if they still use OOP concepts, even though they are not using strictly OOP.

      I also like procedural python for quick small scripts. And although Rust isn’t strictly OOP, it obviously borrows heavily from it. Haskell is neat, but I haven’t used it enough to be proficient or develop good sense of application architecture.

      I’ve done production work in C, but still used largely OOP concepts; and the code looks much different than code I’ve seen that was written before C++ was popular.

      • LH0ezVT@sh.itjust.works
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        12 days ago

        The Linux kernel actually uses quite a bit of OOP ideas. You have modules that are supposed to have a clear interface with the rest of the world, and they (ab)use structs to basically work like objects. If you try hard enough, you can even do “inheritance” with them, like with their struct kobject. It is actually somewhat well-thought-out, imo. No need to go full OOP, just pick some of the good parts, and avoid the MappingModelFactoryServiceImpl hell or the madness that is C++.

    • sum_yung_gai@lemm.ee
      link
      fedilink
      arrow-up
      8
      ·
      13 days ago

      I have been trying to be more functional but I still use classes for things like loading/modeling configs. What are some common situations where using an object is a good solution?

      I use python if that helps at all.

      • pivot_root@lemmy.world
        link
        fedilink
        arrow-up
        12
        ·
        edit-2
        13 days ago

        What are some common situations where using an object is a good solution?

        It depends on what you mean by “object”

        • Some kind of structured data?
        • Some named type which fulfills an interface?

        When you have some kind of structured data, having a class to represent it is fine. If you’re able to give it type annotations, that’s much better than passing around random dictionaries.

        When you need polymorphism and have an interface where some method on an object needs to exist (e.g. car.honk()), that’s also fine as long as you avoid creating subclasses and using inheritance. If you need some car that can honk like a truck and drive like a racecar, use composition.

        What I would consider a good use of classes (more specifically, nominal types) is dependent types. The idea is that you use the type system to enforce invariants for data.

        For example, suppose you have a string for a user email. It might be a valid email string, or it might be garbage like “z#%@(”=))??". You have a function for updating the user email in a database, and it requires the email string to be valid.

        One approach is to validate the email string after receiving it from the user. That works, but what if your coworker creates a new form and forgets to validate the email string there? Bad data gets passed downstream to functions that expect well-formed data.

        Another approach is to validate the email string at the top of every function that expects well-formed data. That also works, but now you’re validating the same string multiple times and pasting validate_email(email) everywhere.

        With a dependent type, you have a ValidatedEmail type and a constructor for it. The constructor will return an instance of the ValidatedEmail if and only if the email string is valid. Any function that expects a valid email will only accept a ValidatedEmail, and not a string. If your coworker creates a new form and forgets to validate the email, the type system will complain about a string being passed instead of a ValidatedEmail. You also shift the responsibility of validating the email to wherever there is a boundary between validated and unvalidated data, avoiding unnecessary validation since you know a ValidatedEmail is already valid.

        It’s an extremely useful paradigm for avoiding logic errors, but it’s unfortunately not as common as it should be.

        • anyhow2503@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          12 days ago

          That’s good advice but I would add that Java really sucks at using “the type system to enforce invariants for data” and that this approach doesn’t have much to do with what most (especially Java programmers) would consider OOP. I die inside a little bit every time I need to use code generators or runtime reflection to solve a problem that really should not require it.

      • bstix@feddit.dk
        link
        fedilink
        English
        arrow-up
        8
        ·
        12 days ago

        Objects are great for game programming.

        Stuff like enemies, bullets, loot, items, etc. are constantly changing and affecting each other. This makes it difficult to program in fixed functions that run through and crosscheck a bunch of constantly changing arrays. It makes logical sense to have objects with their own functions stored in dynamic lists instead of arrays. A lot of the properties are used by several different objecs, like coordinates or health points, so it also makes sense to describe them in classes.

        So, I’d say that it’s a useful way to classify data that is very dynamic, or when different functions need to run depending on that data.

        I don’t like putting all code into classes, because a lot of code doesn’t need that kind of flexibility.