Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There are numerous ways of handling this that are significantly better, for example using an std::optional yields this code:

    do_something().and_then(process);
Or structured bindings:

    auto [error_code, result] = do_something();
    if(error_code == 0) {
      process(result);
    }


In the presence of types which are truthy (ie, contextually convertible to bool), its too easy to accidentally swap the unpacking:

    // Oops, backwards!
    auto [result, error_code] = do_something();
    if(error_code) {
      process(result);
    }


To me that's not easy...

It's a very well established pattern to have (error, result) I'd immediately see something off with (result, error)

And clang-tidy can catch this: https://clang.llvm.org/extra/clang-tidy/checks/readability-i...


Fair enough, in my codebase we always order data in terms of dependencies so that independent data comes before dependent data. In this case the result depends on the error_code, and the error_code is independent, so the error_code must be listed first.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: