• Wirlocke@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    4
    ·
    1 day ago

    Weird opinion: I like square brackets, angle brackets are neat, curly braces range from neat to ok.

    But I just don’t like the look of parentheses compared to the others. They especially look ugly and clunky when you need a bunch of them in a row, in a way that’s not true for the others.

    I can’t think of a reason why I feel this way apart from the fact they tend to be very clunky in math too. They’re also the ugliest one when handwritten despite being the arguably simplest shape.

    All that said it annoys me how parentheses are the most used bracket type in programming.

  • onlinepersona@programming.dev
    link
    fedilink
    English
    arrow-up
    49
    arrow-down
    1
    ·
    3 days ago

    cpp and their operator overloads are also insane. It makes understanding cpp code impossible. A “+” could actually do something completely different or have insane side-effects. Truly superior language for spreading confusion.

    • MellowSnow@lemmy.world
      link
      fedilink
      arrow-up
      6
      ·
      2 days ago

      When I was an intern, I was tasked with writing a file converter for 3D geometry files. There was an official cpp library to read the source files, so I was using that to read the data, restructure and transform it as needed, then write it to the output file format. Near the end, I was having a problem where I was SURE that my math was correct for the transform coordinates, but the resulting file would load in the viewer with all the correct looking pieces but all in the wrong locations. The senior guy there helped me debug it since I wasn’t super familiar with cpp at the time, and we found that the library I was using had overloaded the multiply (*) operator specifically for the objects I was using it on, and it was actually multiplying and saving the result directly to the first variable used in the multiplication. I assume it was done for memory efficiency or something, but I had no idea it would behave that way, so I was accidentally changing the values of variables I didn’t intend to.

      Example: I was doing something a = b * c, which caused the value of b to be changed (to b * c). Which screwed things up when I needed the original value of b later.

      • onlinepersona@programming.dev
        link
        fedilink
        arrow-up
        5
        ·
        1 day ago

        This is the type of shit that haunts me. Imagine overloading “>>” for piping and hiding threading behind that. The idea was to create an async pipe, but nowhere are you aware of async or threading or whatever was chosen. You look at the code and think “of it’s like piping to stdout” or whatever, only to find out that the method immediately returns.

        Tracking down what the hell is happening with code that has such hidden side effects is a nightmare.

        Are you still in the C++ world?

        • MellowSnow@lemmy.world
          link
          fedilink
          arrow-up
          4
          ·
          1 day ago

          That is WILD lol. No, after that, I did a bunch of backend server apps for a few years mostly in C#, and then I got put onto some web app stuff after that. For better or worse. I do enjoy it and appreciate the abstraction, and I have found that I do love frontend dev a lot, but I do sometimes miss the lower level languages haha.

          • onlinepersona@programming.dev
            link
            fedilink
            arrow-up
            3
            ·
            edit-2
            3 hours ago

            I talked to a frontender recently who said they had less pleasure writing frontend, because AI could make it look as good as they wanted in a fraction of the time. Has AI changed anything for you?

            I was “encouraged” to use AI at work for frontend and while it output functional stuff, lifting the hood on the how, terrified me. 😅

            • MellowSnow@lemmy.world
              link
              fedilink
              arrow-up
              3
              ·
              15 hours ago

              Yeah, in my experience, it’s very hit or miss with frontend code architecture (I primarily work with React). And I am very particular about keeping code clean and maintainable, so that can be a challenge sometimes for sure. It’s not bad for smaller one-off tasks, but I still do a majority of my larger tasks by hand because I typically already know what I need and how to do it correctly.

    • red_tomato@lemmy.world
      link
      fedilink
      arrow-up
      31
      arrow-down
      2
      ·
      3 days ago

      Operator overloads is something that sounds like a good idea until you experience them firsthand. Just properly name your functions for fuck’s sake.

      • glibg10b@lemmy.zip
        link
        fedilink
        arrow-up
        2
        ·
        1 day ago

        Like all of the footguns in C++, it’s only a problem if the codebase uses it wrong. When used correctly (i.e. only when behaviours mimic built-in types, and without side effects), it helps makes code more readable

        Don’t do what numpy does, which is to define * for two arrays. Without learning the library, you wouldn’t know whether this performs element-wise multiplication, matrix multiplication, dot product or cross product. Worse yet, it behaves differently to Python’s built-in * for lists (which is also bad), which concatenates N copies of the list

      • xigoi@lemmy.sdf.org
        link
        fedilink
        arrow-up
        3
        ·
        2 days ago

        Clearly you’ve never had to read code like

        entity.position = vector_algebra::vector2_add(entity.position, vector_algebra::vector2_scale(entity.velocity, dt));
        • red_tomato@lemmy.world
          link
          fedilink
          arrow-up
          3
          ·
          2 days ago

          I prefer Java style:

          entity.position = entity.position.add(entity.velocity.scale(dt));
          

          Not hard to read.

      • zerofk@lemmy.zip
        link
        fedilink
        arrow-up
        6
        ·
        2 days ago

        Every time I use a language that doesn’t have operator overloading I wish I was using C++. It’s absolutely maddening to have to read and write things like “end.subtract(start)”.

      • balsoft@lemmy.ml
        link
        fedilink
        arrow-up
        16
        ·
        3 days ago

        Operator overloading is awesome in a functional language. Haskell allows for a lot of creative stuff with operators, but since they can’t have side effects, must conform to predictable types, and have specific requirements they are somewhat understandable. Idris is also very ergonomic when it comes to operators.

        C++ allowing a library to override the + operator with system("rm -rf /") is a lot less awesome.

      • Chrobin@discuss.tchncs.de
        link
        fedilink
        arrow-up
        7
        ·
        3 days ago

        As a computational physicist, I appreciate operator overloading IF used properly.
        Example of a good use: + for matrix addition
        Example of a bad use: left shift operator << as a pipe
        I still don’t understand who’s in the C++ committee that they would choose this garbage over a function

        • tyo_ukko@sopuli.xyz
          link
          fedilink
          arrow-up
          2
          ·
          14 hours ago

          My theory is that they came up with the concept of operator overloading (or copied it elsewhere and were exited) and decided the best place to demonstrate and sell the concept is in the most used console input and output standard functions.

          Edit: I realize my contempt for this decision is not evident here, so let me add that it was a terrible choice in their part indeed.

      • unknownuserunknownlocation@kbin.earth
        link
        fedilink
        arrow-up
        8
        arrow-down
        1
        ·
        3 days ago

        Honestly, I’m glad to have them, it can make a number of things simpler. For instance, a program I wrote had a number of duration fields that had to be stored in XML files and updated every once in a while. It was something that had to occur very often in the code: add a value to the duration, set that value in the XML data, and save the XML file. So I created a class that stored the date/time value and a pointer the XML element (it could be assumed that nothing else would be changing the value in the XML file and if it did, then that change should be overwritten) and overloaded the += operator (among others), so then all I needed was to create the DurationField object, and then I could go durationFieldX += time2 - time1 for instance. I could abstract all the logic away and make it very clear and easy to write. Could I have made it so that I have to to durationFieldX.add(time2 - time1)? Of course I could have. But IMO that variant is less neat and less readable.

        • MangoCats@feddit.it
          link
          fedilink
          arrow-up
          8
          ·
          3 days ago

          I’ll jump on your unpopular side. With a decent IDE you should be able to point/click and decode what any operator is doing in any context. If I have two vectors, or quaternions, and I want to operate on them, why should I be forced to pass them to a standard function instead of clearly seeing what I’m doing. If you can’t be bothered to know the types of your variables in a strongly typed language… IDK.

    • boonhet@lemmy.zip
      link
      fedilink
      arrow-up
      9
      ·
      2 days ago

      Omg I have the best idea but I’m not writing any C++ so someone else will have to do it.

      Overload + to work as - in either some super rare condition or randomly with a very low chance

      Ship it

    • Valmond@lemmy.dbzer0.com
      link
      fedilink
      arrow-up
      11
      arrow-down
      2
      ·
      3 days ago

      Yeah it’s like a knife, wildly superior to cut stuff up in the kitchen, but you can also like stick it in your eye (but why?).

    • Sonnenblume@discuss.tchncs.de
      link
      fedilink
      arrow-up
      7
      ·
      1 day ago

      There are lots of ways to write C++ code that looks perfectly reasonable but introduces horrible undefined behaviour. You need to be aware of lots of edge cases all the time and you will still make mistakes that can introduce difficult to trace bugs. One of my personal favourites is object slicing.

      class A {
         int foo;
      };
      
      class B : public A {
         int bar;
      };
      
      B b;
      A a = b;
      

      Looks OK? Well, a now only contains the A part of the object. The field bar is lost in the assignment. There are reasons for this behaviour but its a sharp edge nonetheless.

      These kinds of problems are numerous and you have to keep them in mind while programming C++. Some people don’t mind it but many do. Static analysis tools that can catch many of these errors exist, but I understand people who say they don’t want to deal with that and rather use a language where these kind of errors are not possible. Personally, I would also discourage people from starting new C++ projects today if they can avoid it.

    • Kwdg@discuss.tchncs.deOP
      link
      fedilink
      arrow-up
      2
      ·
      1 day ago

      I don’t really know myself, probably the many footguns and complexities. Like taking a reference to a value in a vector and then resizing the vector, boom undefined behaviour. There a re a lot of things like this. Maybe also the verbosity and multie ways to do things. Look at the linked code, there is a lot of overhead to the function definitions, sure there is a point to all of it and I put it there for a reason, but to people less familiar to the language it is probably quite offputting.

      Personally I like the language, second only to rust for me

  • calcopiritus@lemmy.world
    link
    fedilink
    arrow-up
    24
    ·
    3 days ago

    Funny that you put rust as the “uses parenthesis for function calls”. When in rust you can use any bracket for macro invocations:

    let v1 = vec!(1,2,3);
    let v2 = vec![1,2,3];
    let v3 = vec!{1,2,3};
    

    These are valid and have the exact same meaning.

    • Flipper@feddit.org
      link
      fedilink
      English
      arrow-up
      9
      ·
      3 days ago

      To be fair, with Rust macros anything goes. There was an example where someone demoed descriping rust structs and enums nested as xml with a macro to parse it.

      • Ephera@lemmy.ml
        link
        fedilink
        arrow-up
        5
        ·
        2 days ago

        I always enjoy this one: https://crates.io/crates/inline-python

        Apparently, Python’s syntax is almost entirely a subset of Rust’s syntax + a few keywords, so even though you have to pass valid Rust tokens into a macro, that’s good enough to write Python with just a few quirks.

      • calcopiritus@lemmy.world
        link
        fedilink
        arrow-up
        7
        ·
        2 days ago

        That’s inside the macro though. Whatever you parse inside the macro is defined by the creator of the macro. The macro invocation itself is defined by the rust language.

        Rust allows you to use any bracket because it doesn’t know what the macro is supposed to do.

        Some macros work like functions:

        println!("Hello world");
        

        Some macros work like array declarations:

        let v = vec![1,2,3];
        

        And some macros work like a code scope:

        select!{
         a => myfunc(),
         b => my_other_func()
        }
        

        Since rust doesn’t know what your macro does, it just lets the user choose whatever the user sees more fitting for that specific macro invocation.

    • ExperimentalGuy@programming.dev
      link
      fedilink
      arrow-up
      3
      ·
      3 days ago

      That’s always funny to me. In a language that’s so particular about different syntax features, they let macros of all things have a little bit of arbitrariness.

      • Ephera@lemmy.ml
        link
        fedilink
        arrow-up
        1
        ·
        1 day ago

        Yeah, I always wonder, if it was an intentional decision or just some first draft that stayed in.

        Because I do think, it’s kind of smart. Some macros make sense to call with either () or {}, depending on if you pass a code snippet that’s multi-line or not.
        So, you could come up with a complicated syntax to define which bracket types are acceptable, or you could just not bother. 🙃

  • DrakeAlbrecht@lemmy.world
    link
    fedilink
    English
    arrow-up
    18
    ·
    3 days ago

    I have honestly never seen parentheses called “round brackets” before. It totally makes sense, I have no idea why it’s not used more often.

  • Gladaed@feddit.org
    link
    fedilink
    English
    arrow-up
    14
    ·
    3 days ago

    Only one of those is a function, get it right. () Call functions <> Specialize Templates, can be used to do compile time calls to things that behave like functions. [] Accesses elements {} Calls constructors, aka functions that do the initial setup.

    Overloading these to do something else gets you in the naughty corner.

    Edit: the implementation of the {} fuckery is no great. Just save the sum instead of all numbers.

    • Kwdg@discuss.tchncs.deOP
      link
      fedilink
      arrow-up
      5
      ·
      3 days ago

      Good point regarding the {} implementation.

      I disagree slightly regarding your point that these aren’t function calls. Yes technically they are different things but effectively they are functions, I cann pass arguments and get a result based on some operations.

      Of course not all of these can do anything but only the constexpr value is actually pretty limited. The other ones are straight up functions

  • NeatNit@discuss.tchncs.de
    link
    fedilink
    arrow-up
    9
    ·
    3 days ago

    Good meme, but I’m really not sure why you wrapped everything in asserts which kind of ruins the aesthetics. IMO would have worked better as e.g.:

    x = sum(1, 2, 3); // x = 6
    
      • NeatNit@discuss.tchncs.de
        link
        fedilink
        arrow-up
        5
        ·
        3 days ago

        In example code, yes, but in this meme specifically which is all about function call syntax, it ruins the point. E.g. every line in the C++ snippet includes parentheses.

  • trxxruraxvr@lemmy.world
    link
    fedilink
    arrow-up
    8
    ·
    3 days ago

    I haven’t kept up to date with C++ development in the last 16 years, I only recognize the first two of those four.

    • Kwdg@discuss.tchncs.deOP
      link
      fedilink
      arrow-up
      7
      ·
      3 days ago

      You can overload the indexing operazor “[]” (std::vector does this for example), and since c++23 this operator can have more than one argument.

      The last one is a constructor call with an overloaded conversion operator. The {} syntax is possible since c++11 I think. The conversion operator was already available before that

      • xthexder@l.sw0.com
        link
        fedilink
        arrow-up
        2
        ·
        2 days ago

        Ah, that’s why… I didn’t think multiple arguments to operator[] was valid, but my brain is still catching up to C++20 features.

  • palordrolap@fedia.io
    link
    fedilink
    arrow-up
    3
    ·
    3 days ago

    You can do most if not all of this in Perl if you’re twisted enough. Some hoop jumping may be required*, but I can think of ways to do something that looks like each of these.

    *: The braces are especially tricky if warnings are turned on. I’d probably abuse the deprecated multidimensional syntax and tie a hash to the actual function.