• onlinepersona@programming.dev
    link
    fedilink
    English
    arrow-up
    0
    ·
    11 months ago

    I agree, typing is not a bottleneck, repeating yourself is. All that boilerplate code in the example, shouldn’t have to be written. It’s wasted time.

    • philm@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      11 months ago

      The curse of OOP (java style…).

      I mean why do you need to write getter and setter methods. I have wondered at the beginning of university 10 years ago, and am still wondering why you would need something like that…

      • Dunstabzugshaubitze@feddit.de
        link
        fedilink
        arrow-up
        0
        ·
        11 months ago

        You let your ide generate simple getters and setters or utilize something that generates them during a compilation process.

        Who ever writes them per hand needs to utilize their tooling better or needs better tooling.

        • philm@programming.dev
          link
          fedilink
          arrow-up
          0
          ·
          11 months ago

          Yeah but why do I have to use an IDE to generate getters and setters in the first place? It just adds up to more mental overhead, because my brain has to process this boilerplate somehow, even if my IDE can generate it (I know it’s simple code, but it’s even simpler to not have that boilerplate code at all).

          • Dunstabzugshaubitze@feddit.de
            link
            fedilink
            arrow-up
            0
            ·
            11 months ago

            Access control and offering a sound interface.

            You don’t need getters and setters if every attribute is public, but you might want to make sure attributes are accessed in a specific way or a change to an object has to trigger something, or the change has to wait until the object is done with something. Java just has tools to enforce a user of your objects to access its attributes through the methods you designed for that. It’s a safeguard against unintended side effects, to only open up inner workings of a class as littles as necessary.

            In a language without something like private attributes you’d have to account for far more ways someone might mutate the state of objects created by your code, it opens you up to far more possible mistakes.

            • philm@programming.dev
              link
              fedilink
              arrow-up
              0
              ·
              11 months ago

              I’m totally aware of the benefits of encapsulation, but the way java does it seems so unnecessarily boilerplatey (C# is better, functional programming makes encapsulation even simpler, but that’s a different paradigm…)

              I like how Rust approaches this via the module system and crates (you have pub for the public interface, pub(crate) for crate/lib wide access and no modifier for being only allowed to access in the current module and submodules of that module)

              • Dunstabzugshaubitze@feddit.de
                link
                fedilink
                arrow-up
                0
                ·
                11 months ago

                Not to aware of how c# works, or interested in defending java, especially ancient java versions, but what does it do better in that regard?

                Only records for more or less pure data objects come to mind, but those are also in modern Java.

  • vettnerk@lemmy.ml
    link
    fedilink
    arrow-up
    0
    ·
    11 months ago

    Typing speed matters in programming the same way hammer hits per second matters when building a house. There’s a little bit more to it.

  • Kissaki@feddit.de
    link
    fedilink
    English
    arrow-up
    0
    ·
    11 months ago

    Are people, their critics, really bothered with the writing aspect of verbose code?

    Verbose code - and their example shows it quite well - is less concise. To me, their example and reasoning looks more overly verbose, over-engineered rather than well or thoroughly designed and safe.

    What makes more sense depends on the context. Where is and will it be used? What is it being exposed to?

    A simple data holding class for one “internal” layer to the next can have a very simple form, so it’s easily understood. It may not be safe against misuse or mistakes, but the simplicity and clear use-case and intention much outweighs those costly safeguards to me. For maintainability, for readability, for clarity.

    It’s always a balancing act between simplicity and easy understanding vs safeguards and costs. If there’s less risk you can make it much simpler - which reduces other, less obvious risks and costs.


    The example made me immediately spot how C# quite a while ago introduced language constructs for what would otherwise be a lot of boilerplate programming like they do in their example.

    public record Reservation(DateTimeOffset Date, string Email, string Name, int Quantity, bool IsAccepted);
    

    no need for 78 lines of code with 9 methods.

    C# also has the with keyword for copy-adjusting immutable types.

    And required init syntax provides another alternative for an effectively immutable type.

    public class Reservation
    {
        public required DateTimeOffset Date { get; init; }
        public required string Email { get; init; }
        public required string Name { get; init; }
        public required int Quantity { get; init; }
        public required bool IsAccepted { get; init; }
    }