Yo whatup

  • 0 Posts
  • 63 Comments
Joined 9 months ago
cake
Cake day: September 28th, 2023

help-circle




  • Smart pointers model ownership. Instead of (maybe) a comment telling you if you have to free/delete a returned pointer this information is encoded into the type itself. But that’s not all, this special type even handles the whole deleting part once it goes away.

    Since they model ownership you should only use them when ownership should be expressed. Namely something that returns a pointer to a newly allocated thing should be a std::unique_ptr because the Callie has ownership of that memory. If they want to share it (multiple ownership of the object) there’s a cheap conversion to std::shared_ptr.

    How about a function that takes in an object cause it wants to look at it? Well that doesn’t have anything to do with ownership so make it a raw pointer, or better yet a reference to avoid nullability.

    What about when you’ve got a member function that wants to return a pointer to some memory the object owns? You guessed it baby raw pointer (or again reference if it’ll never be null).


  • Um what? I didn’t like hide extra meaning in what I said. High quality code doesn’t imply all that extra shit you added. It’s code that’s easy to read and modify. Typically this just means you name stuff well and document things that aren’t obvious. Usually my docs explain why something exists since thinking it’s unnecessary cause you don’t remember what the original problem was a common occurrence before I started doing so.

    Is high quality code ran through a formatter? I’d hope so yeah. There should be a consistent code style across the entire project. Doesn’t matter what it it long as it’s consistent.

    100% code coverage is meaningless and as such a pointless metric. Also 100% coverage is explicitly tied to the implimentaion as all code paths have to be reached which is obviously not a good idea (tests have to change when the implimentaion changes as you’re testing the implimentaion not the api).

    Really a lot of this is just meaningless buzz words as an attempt at some sort of gotcha. Really don’t understand how you even interpreted a statement so simple in this way.









  • I’d probably say it depends but I’m no Rust expert and I have no direct experience with C (though quite familiar with C++).

    Basically I’d expect writing C to be easy, but not safe. IE you can quickly and easily write C that compiles but has runtime issues. Rust for the most part will catch everything but logic issues during/before compilation meaning once the program runs you’ll have very high confidence in it’s runtime behavior leading to time spent “fighting the compiler” instead of figuring out wtf is going wrong at runtime.


  • What Rust provides is statically guaranteed memory safety. Some C++ types will prevent memory issues however the language itself is unsafe. Playing with raw pointers is just as valid as using std::unique_ptr. In Rust however you must sign a contact (using unsafe) in order to play with raw pointers. Unsafe is you the programmer promising that you followed the rules. This is like how C++ says it’s illegal to write UB and your program will break (and it’s your fault) but enforced through a special type of block


  • Yes Rust is harder to write than C, that’s basically by design as it’s due to the statically guaranteed memory safety. That’s pretty magical. C doesn’t have that and neither does C++ even with smart pointers and such. Rusts unsafe keyword is poorly named, what it actually does is tell the compiler that you the programmer guarantee Rusts rules are upheld within the unsafe block.

    For example

    Access or modify a mutable static variable

    That is a global, that’s incredibly hard to impossible to statically prove it’s safely done, so you have to do it in an unsafe block. So you violating Rusts rules within an unsafe block is actually using the unsafe block wrong. That’s not what it’s for





  • Traister101@lemmy.todaytoProgrammer Humor@programming.devwait what
    link
    fedilink
    arrow-up
    1
    arrow-down
    1
    ·
    edit-2
    4 months ago

    Looking at code on somebody else’s screen is entirely missing the point of using tabs over spaces. The entire point is that mine looks like how I want and theirs looks like how they want even though the file is identical. We can each have wildly different tab width and it’ll look wildly different to each of us when we program. That’s again the point.

    Code formatters are great! I love them. Using tabs over spaces is objectively a better formatting option. One of my favorite features in code formatters is that they’ll swap out spaces to tabs for you insane people who insist on mashing the space bar to indent.