• 4 Posts
  • 164 Comments
Joined 2 years ago
cake
Cake day: June 10th, 2024

help-circle
  • balsoft@lemmy.mltomemes@lemmy.worldSolid advice.
    link
    fedilink
    arrow-up
    1
    arrow-down
    1
    ·
    1 day ago

    Except in this case this particular fridge has worked for 40 years already, so just by Bayesian statistics it is more likely to keep working than a modern one from a range that are known to break. Same reason why some old cars are getting more expensive nowadays.


  • Documentation will always have to be actually written by the author(s) of the code (or at least someone who understands the code really well), because only the author knows the intent behind a certain function or API endpoint, and that’s what the documentation is for.

    LLMs don’t understand shit (sorry AI bros), they will sometimes produce accurate descriptions of the function code as written, but never the intent. Even if the LLM “wrote” the code, it doesn’t “understand” the real intent behind it, because it is just a poor mashup of code taken/stolen from someone else, which statistically fits the prompt.

    What LLMs could help with is generating short, human-readable descriptions of what is happening in a given function. This can potentially be helpful for debugging/modifying projects with poor documentation, naming, and function separation, so that instead of gleaning through multiple 2000-line C functions in a 100k SLOC file, you can kind of understand what it does quickly. I’ve used deepseek for this before, with mixed-to-positive results.

    But again, this would just be to speed up surface-level digging and not a replacement for actual documentation or good practices.





  • balsoft@lemmy.mltoProgrammer Humor@lemmy.mlrustmas
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    23 days ago

    Rust does not have exceptions. You never have to try/catch. Functions usually encode the possible failures in their types, so you’d have something like this C++ snippet:

    struct HttpError {
      std::string message;
      int responseCode;
    }
    
    struct FsError {
      std::string message;
    }
    
    typedef std::variant<HttpError, IoError> FileDownloadError;
    
    std::variant<std::filesystem::path, FileDownloadError> downloadFile(std::string url) { /* ... */ }
    

    And then the caller of downloadFile has to decide what to do if it returns an error:

    auto res = std::visit(overloaded {
    	[](FileDownloadError e) { /* ignore error, or handle it somehow, and return a new path */ },
    	[](std::filesystem::path p) { return p; }
    }, downloadFile(url));
    
    /* res is guaranteed to be a valid std::filesystem::path, you don't have to care about errors from downloadFile anymore */
    

    However, Rust makes this whole thing a lot easier, by providing syntax sugar, and there are helper libraries that reduce the boilerplate. You usually end up with something like

    #[derive(Error, Debug)]
    enum FileDownloadError {
      #[error("HTTP request failed")]
      HttpError(http::status::StatusCode),
      #[error("Filesystem error: {0}")
      FSError(#[from] std::io::Error),
    }
    
    fn download_file(String url) -> Result<Path, FileDownloadError> {/* ... */}
    

    (notice the #[from], which forwards the error message etc from the std::io::Error type)

    The Result type is kind of like a std::variant with two template arguments, and, mostly by convention, the first one denotes the successful execution, while the second one is the error type. Result has a bunch of methods defined on it that help you with error handling.

    Consumer code is something like this:

    let res : Path = download_file(url).unwrap_or_else(|e| {
      /* Ignore the error or handle it. You have to return a new path here */
    });
    
    /* res is guaranteed to be a valid Path, you don't have to care about errors from download_file anymore */
    

    Or

    let res : Path = download_file(url)?;
    
    /* res is guaranteed to be a valid Path, you don't have to care about errors from download_file anymore */
    

    Which will just forward the error to your caller (but your function has to return Result as well), or proceed with the execution if the function succeeded.

    Finally, download_file(url).unwrap() is if you can neither ignore, nor handle, nor pass the error to the caller. It will abort if the function fails in any way, and there’s no (practical) way to catch that abort.


  • balsoft@lemmy.mltoProgrammer Humor@lemmy.mlrustmas
    link
    fedilink
    arrow-up
    7
    ·
    edit-2
    23 days ago

    It’s worse than just exceptions in C++. There’s (almost) no way for the caller of your function to catch it. It’s a bit like this snippet:

    std::optional<int> foo = <...>;
    try {
      return foo.value();
    } catch(const std::bad_optional_access& e) {
      std::cout << e.what() << std::endl;
      abort();
    }
    

    It’s the abort that is the crux of the issue here. Usually you would pass the std::optional up/down the call stack. If you don’t control the types (e.g. using a library or a framework) you’d come up with some “default” value instead, like this:

    std::optional<int> foo = <...>;
    return foo.value_or(123);
    

    Or in Rust:

    let foo : Option<i32> = <...>;
    return foo.unwrap_or(123);
    

    But sometimes there’s no good “default” value, and then you have to resort to just unwrap-ing the value, and accepting that the entire program will abort when that function call fails. Usually this is a sign of poor engineering somewhere, likely in a library you’re using, and should be fixed; but sometimes you don’t have the time to fix it, and then it ends up in production.




  • Nah, social division of labour is almost as old as civilization itself. It’s not possible to do everything by yourself, if you’re lucky enough not to die of hunger in a few weeks, you’ll die of a preventable disease within a few years.

    Capitalist exploitation of it (combined with faux individualism) is the issue.

    Translating to this scenario, there would be no issue if we had community kitchens with cheap/free food and well-paid workers. The issue is the capitalists extracting surplus value from a basic necessity, partly by convincing everyone to hate their neighbours and eat alone.