I don’t think that casting a range of bits as some other arbitrary type “is a bug nobody sees coming”.

C++ compilers also warn you that this is likely an issue and will fail to compile if configured to do so. But it will let you do it if you really want to.

That’s why I love C++

    • mindbleach@sh.itjust.works
      link
      fedilink
      arrow-up
      34
      ·
      1 month ago

      C is dangerous like your uncle who drinks and smokes. Y’wanna make a weedwhacker-powered skateboard? Bitchin’! Nail that fucker on there good, she’ll be right. Get a bunch of C folks together and they’ll avoid all the stupid easy ways to kill somebody, in service to building something properly dangerous. They’ll raise the stakes from “accident” to “disaster.” Whether or not it works, it’s gonna blow people away.

      C++ is dangerous like a quiet librarian who knows exactly which forbidden tomes you’re looking for. He and his… associates… will gladly share all the dark magic you know how to ask about. They’ll assure you that the power cosmic would never, without sufficient warning, pull someone inside-out. They don’t question why a loving god would allow the powers you crave. They will show you which runes to carve, and then, they will hand you the knife.

  • panda_abyss@lemmy.ca
    link
    fedilink
    arrow-up
    78
    arrow-down
    2
    ·
    1 month ago

    I actually do like that C/C++ let you do this stuff.

    Sometimes it’s nice to acknowledge that I’m writing software for a computer and it’s all just bytes. Sometimes I don’t really want to wrestle with the ivory tower of abstract type theory mixed with vague compiler errors, I just want to allocate a block of memory and apply a minimal set rules on top.

    • jkercher@programming.dev
      link
      fedilink
      English
      arrow-up
      5
      ·
      edit-2
      14 days ago

      100%. In my opinion, the whole “build your program around your model of the world” mantra has caused more harm than good. Lots of “best practices” seem to be accepted without any quantitative measurement to prove it’s actually better. I want to think it’s just the growing pains of a young field.

      • SpaceCowboy@lemmy.ca
        link
        fedilink
        arrow-up
        5
        arrow-down
        1
        ·
        1 month ago

        Even with qualitative measurements they can do stupid things.

        For work I have to write code in C# and Microsoft found that null reference exceptions were a common issue. They actually calculated how much these issues cost the industry (some big number) and put a lot of effort into changing the language so there’s a lot of warnings when something is null.

        But the end result is people just set things to an empty value instead of leaving it as null to avoid the warnings. And sure great, you don’t have null reference exceptions because a value that defaulted to null didn’t get set. But now you have issues where a value is an empty string when it should have been set.

        The exception message would tell you exactly where in the code there’s a mistake, and you’ll immediately know there’s a problem and it’s more likely to be discovered by unit tests or QA. Something that’s an value that’s supposed to be set may not be noticed for a while and is difficult to track down.

        So their research indicated a costly issue (which is ultimately a dev making a mistake) and they fixed it by creating an even more costly issue.

        There’s always going to be things where it’s the responsibility of the developer to deal with, and there’s no fix for it at the language level. Trying to fix it with language changes can just make things worse.

        • HER0@beehaw.org
          link
          fedilink
          arrow-up
          5
          ·
          1 month ago

          For this example, I feel that it is actually fairly ergonomic in languages that have an Option type (like Rust), which can either be Some value or no value (None), and don’t normally have null as a concept. It normalizes explicitly dealing with the None instead of having null or hidden empty strings and such.

          • SpaceCowboy@lemmy.ca
            link
            fedilink
            arrow-up
            2
            arrow-down
            1
            ·
            1 month ago

            I just prefer an exception be thrown if I forget to set something so it’s likely to happen as soon as I test it and will be easy to find where I missed something.

            I don’t think a language is going to prevent someone from making a human error when writing code, but it should make it easy to diagnose and fix it when it happens. If you call it null, “”, empty, None, undefined or anything else, it doesn’t change the fact that sometimes the person writing the code just forgot something.

            Abstracting away from the problem just makes it more fuzzy on where I just forgot a line of code somewhere. Throwing an exception means I know immediately that I missed something, and also the part of the code where I made the mistake. Trying to eliminate the exception doesn’t actually solve the problem, it just hides the problem and makes it more difficult to track down when someone eventually notices something wasn’t populated.

            Sometimes you want the program to fail, and fail fast (while testing) and in a very obvious way. Trying to make the language more “reliable” instead of having the reliability of the software be the responsibility of the developer can mean the software always “works”, but it doesn’t actually do what it’s supposed to do.

            Is the software really working if it never throws an exception but doesn’t actually do what it’s supposed to do?

            • HER0@beehaw.org
              link
              fedilink
              arrow-up
              1
              ·
              1 month ago

              It is fair to have a preference for exceptions. It sounds like there may be a misunderstanding on how Option works.

              Have you used languages that didn’t have null and had Option instead? If we look at Rust, you can’t forget not to check it: it is impossible to get the Some of an Option without dealing with the None. You can’t forget this. You can mess up in a lot of other ways, but you explicitly have to decide how to handle that potential None case.

              If you want it to fail fast and obvious, there are ways to do this. For example you, you can use the unwrap() method to get the contained Some value or panic if it is None, expect() to do the same but with a custom panic message, the ? operator to get the contained Some value or return the function with None, etc. Tangentially, these also work for Result, which can be Ok or Err.

              It is pretty common to use these methods in places where you always want to fail somewhere that you don’t expect should have a None or where you don’t want your code to deal with the consequences of something unexpected. You have decided this and live with the consequences, instead of it implicitly happening/you forgetting to deal with it.

    • Kairos@lemmy.today
      link
      fedilink
      arrow-up
      6
      arrow-down
      29
      ·
      1 month ago

      People just think that applying arbitrary rules somehow makes software magically more secure, like with rust, as if the compiler won’t just “let you” do the exact same fucking thing if you type the unsafe keyword

      • BatmanAoD@programming.dev
        link
        fedilink
        arrow-up
        17
        ·
        1 month ago

        It’s neither arbitrary nor magic; it’s math. And unsafe doesn’t disable the type system, it just lets you dereference raw pointers.

        • Kairos@lemmy.today
          link
          fedilink
          arrow-up
          6
          arrow-down
          1
          ·
          1 month ago

          That’s not what I meant. I understand that rust forces things to be more secure. It’s not not like there’s some guarantee that rust is automatically safe, and C++ is automatically unsafe.

      • panda_abyss@lemmy.ca
        link
        fedilink
        arrow-up
        2
        arrow-down
        1
        ·
        1 month ago

        I don’t know rust, but for example in Swift the type system can make things way more difficult.

        Before they added macros if you wanted to write ORM code on a SQL database it was brutal, and if you need to go into raw buffers it’s generally easier to just write C/objc code and a bridging header. The type system can make it harder to reason about performance too because you lose some visibility in what actually gets compiled.

        The Swift type system has improved, but I’ve spent a lot of time fighting with it. I just try to avoid generics and type erasure now.

        I’ve had similar experiences with Java and Scala.

        That’s what I mean about it being nice to drop out of setting up some type hierarchy and interfaces and just working with a raw buffers or function pointers.

  • magic_lobster_party@fedia.io
    link
    fedilink
    arrow-up
    46
    arrow-down
    1
    ·
    1 month ago

    There are no medals waiting for you by writing overly clever code. Trust me, I’ve tried. There’s no pride. Only pain.

    • Chrobin@discuss.tchncs.de
      link
      fedilink
      arrow-up
      20
      arrow-down
      1
      ·
      1 month ago

      It really depends on your field. I’m doing my master’s thesis in HPC, and there, clever programming is really worth it.

      • magic_lobster_party@fedia.io
        link
        fedilink
        arrow-up
        11
        ·
        1 month ago

        Well as long you know what you’re doing and weigh the risks with the benefits you’re probably ok.

        In my experience in the industry, there’s little benefit in pretending you’re John Carmack writing fast inverse square root. Understanding what you wrote 6 months ago outweighs most else.

      • MonkderVierte@lemmy.zip
        link
        fedilink
        arrow-up
        6
        arrow-down
        1
        ·
        1 month ago

        Clever as in elegantly and readable or clever as in a hack that abuses a bug/feature and you need to understand the intricacies to understand half of it?

        • Chrobin@discuss.tchncs.de
          link
          fedilink
          arrow-up
          11
          ·
          1 month ago

          Honestly, also the latter. If you are using hundreds of thousands of cores for over 100h, every single second counts.

    • merc@sh.itjust.works
      link
      fedilink
      arrow-up
      7
      ·
      1 month ago

      Not only that, but everyone who sees that code later is going to waste so much time trying to understand it. That includes future you.

    • Ajen@sh.itjust.works
      link
      fedilink
      arrow-up
      4
      ·
      1 month ago

      Debugging code is always harder that writing it in the first place. If you make it as clever as you can, you won’t be clever enough to debug it.

  • merc@sh.itjust.works
    link
    fedilink
    arrow-up
    39
    arrow-down
    1
    ·
    1 month ago

    “C++ compilers also warn you…”

    Ok, quick question here for people who work in C++ with other people (not personal projects). How many warnings does the code produce when it’s compiled?

    I’ve written a little bit of C++ decades ago, and since then I’ve worked alongside devs who worked on C++ projects. I’ve never seen a codebase that didn’t produce hundreds if not thousands of lines of warnings when compiling.

    • jkercher@programming.dev
      link
      fedilink
      English
      arrow-up
      16
      ·
      1 month ago

      You shouldn’t have any warnings. They can be totally benign, but when you get used to seeing warnings, you will not see the one that does matter.

    • nroth@lemmy.world
      link
      fedilink
      arrow-up
      13
      ·
      1 month ago

      0 in our case, but we are pretty strict. Same at the first place I worked too. Big tech companies.

    • dejected_warp_core@lemmy.world
      link
      fedilink
      arrow-up
      10
      ·
      1 month ago

      Ideally? Zero. I’m sure some teams require “warnings as errors” as a compiler setting for all work to pass muster.

      In reality, there’s going to be odd corner-cases where some non-type-safe stuff is needed, which will make your compiler unhappy. I’ve seen this a bunch in 3rd party library headers, sadly. So it ultimately doesn’t matter how good my code is.

      There’s also a shedload of legacy things going on a lot of the time, like having to just let all warnings through because of the handful of places that will never be warning free. IMO its a way better practice to turn a warning off for a specific line.. Sad thing is, it’s newer than C++ itself and is implementation dependent, so it probably doesn’t get used as much.

      • merc@sh.itjust.works
        link
        fedilink
        arrow-up
        4
        ·
        1 month ago

        I’ve seen this a bunch in 3rd party library headers, sadly. So it ultimately doesn’t matter how good my code is.

        Yeah, I’ve seen that too. The problem is that once the library starts spitting out warnings it’s hard to spot your own warnings.

    • jmicz3d@lemmy.sdf.org
      link
      fedilink
      arrow-up
      3
      ·
      1 month ago

      I work on one of the larger c++ projects out there (20 to 50 million lines range) and though I don’t see the full build logs I’ve yet to see a component that has a warning.

    • sunbeam60@lemmy.one
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      1 month ago

      Depends on the age of the codebase, the age of the compiler and the culture of the team.

      I’ve arrived into a team with 1000+ warnings, no const correctness (code had been ported from a C codebase) and nothing but C style casts. Within 6 months, we had it all cleaned up but my least favourite memory from that time was “I’ll just make this const correct; ah, right, and then this; and now I have to do this” etc etc. A right pain.

      • merc@sh.itjust.works
        link
        fedilink
        arrow-up
        2
        ·
        1 month ago

        So, did you get it down to 0 warnings and manage to keep it there? Or did it eventually start creeping up again?

        • shane@feddit.nl
          link
          fedilink
          arrow-up
          2
          ·
          1 month ago

          I’m not the person you’re asking but surely they just told the compiler to treat warnings as errors after that. No warnings can creep in then!

        • sunbeam60@lemmy.one
          link
          fedilink
          arrow-up
          1
          ·
          1 month ago

          Once we were at zero warnings, we enabled warnings as errors, despite the protestation of the grognards on the team.

  • wer2@lemmy.zip
    link
    fedilink
    arrow-up
    26
    arrow-down
    1
    ·
    1 month ago

    My issue is C++ will “let me do it”, and by that I mean “you didn’t cast here (which is UB), so I will optimize out a null check later, and then segfault in a random location”

  • UnfortunateShort@lemmy.world
    link
    fedilink
    English
    arrow-up
    25
    arrow-down
    1
    ·
    edit-2
    1 month ago

    I used to love C++ until I learned Rust. Now I think it is obnoxious, because even if you write modern C++, without raw pointers, casting and the like, you will be constantly questioning whether you do stuff right. The spec is just way too complicated at this point and it can only get worse, unless they choose to break backwards compatibility and throw out the pre C++11 bullshit

    • mobotsar@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      13
      ·
      edit-2
      1 month ago

      Depending on what I’m doing, sometimes rust will annoy me just as much. Often I’m doing something I know is definitely right, but I have to go through so much ceremony to get it to work in rust. The most commonly annoying example I can think of is trying to mutably borrow two distinct fields of a struct at the same time. You can’t do it. It’s the worst.

  • BigDanishGuy@sh.itjust.works
    link
    fedilink
    arrow-up
    31
    arrow-down
    8
    ·
    1 month ago

    But it will let you do it if you really want to.

    Now, I’ve seen this a couple of times in this post. The idea that the compiler will let you do anything is so bizarre to me. It’s not a matter of being allowed by the software to do anything. The software will do what you goddamn tell it to do, or it gets replaced.

    WE’RE the humans, we’re not asking some silicon diodes for permission. What the actual fuck?!? We created the fucking thing to do our bidding, and now we’re all oh pwueez mr computer sir, may I have another ADC EAX, R13? FUCK THAT! Either the computer performs like the tool it is, or it goes the way of broken hammers and lawnmowers!

    • Owl@mander.xyz
      link
      fedilink
      arrow-up
      20
      arrow-down
      2
      ·
      1 month ago

      Ok gramps now take your meds and off you go to the retirement home

    • AnyOldName3@lemmy.world
      link
      fedilink
      arrow-up
      9
      ·
      1 month ago

      Soldiers are supposed to question potentially-illegal orders and refuse to execute them if their commanding officer can’t give a good reason why they’re justified. Being in charge doesn’t mean you’re infallible, and there are plenty of mistakes programmers make that the compiler can detect.

      • BigDanishGuy@sh.itjust.works
        link
        fedilink
        arrow-up
        4
        arrow-down
        1
        ·
        1 month ago

        I get the analogy, but I don’t think that it’s valid. Soldiers are, much to the chagrin of their commanders, sentient beings, and should question potentially illegal orders.

        Where the analogy doesn’t hold is, besides my computer not being sentient, what I’m prevented from doing isn’t against the law of man.

        I’m not claiming to be infallible. After all to err is human, and I’m indeed very human. But throw me a warning when I do something that goes against best practices, that’s fine. Whether I deal with it is something for me to decide. But stopping me from doing what I’m trying to do, because it’s potentially problematic? GTFO with that kinda BS.

    • WhyJiffie@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      7
      ·
      1 month ago

      when life gives you restrictive compilers, don’t request permission from them! make life take the compilers back! Get mad! I don’t want your damn restrictive compilers, what the hell am I supposed to do with these? Demand to see life’s manager! Make life rue the day it thought it could give BigDanishGuy restrictive compilers! Do you know who I am? I’m the man who’s gonna burn your house down! With the compilers! I’m gonna get my engineers to invent a combustible compiler that burns your house down!

    • CanadaPlus@lemmy.sdf.org
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      1 month ago

      Yeah, but there’s some things computers are genuinely better at than humans, which is why we code in the first place. I totally agree that you shouldn’t ever be completely controlled by your machine, but strong nudging saves a lot of trouble.

    • Throskie@midwest.social
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      1 month ago

      This comment makes me want to reformat every fucking thing i use and bend it to -my- will like some sort of technomancer

  • Gobbel2000@programming.dev
    link
    fedilink
    arrow-up
    18
    arrow-down
    1
    ·
    1 month ago

    I’m all for having the ability to do these shenanigans in principle, but prefer if they are guarded in an unsafe block.

  • Treczoks@lemmy.world
    link
    fedilink
    arrow-up
    16
    ·
    1 month ago

    Structs with union members that allow the same place in memory to be accessed either word-wise, byte-wise, or even bit-wise are a god-sent for everyone who needs to access IO-spaces, and I’m happy my C-compiler lets me do it.

  • LillyPip@lemmy.ca
    link
    fedilink
    arrow-up
    7
    ·
    edit-2
    1 month ago

    Why use a strongly typed language at all, then?

    Sounds unnecessarily restrictive, right? Just cast whatever as whatever and let future devs sort it out.

    $myConstant = ‘15’;
    $myOtherConstant = getDateTime();
    $buggyShit = $myConstant + $myOtherConstant;

    Fuck everyone who comes after me for the next 20 years.