77 pointsby szmarczak4 hours ago11 comments
  • Night_Thastus2 hours ago
    Nothing particularly notable here. A lot of it seems to be 'We have something in-house designed for our use cases, use that instead of the standard lib equivalent'.

    The rest looks very reasonable, like avoiding locale-hell.

    Some of it is likely options that sand rough edges off of the standard lib, which is reasonable.

    • ryandrake44 minutes ago
      > We have something in-house designed for our use cases, use that instead of the standard lib equivalent

      Yea, you encounter this a lot at companies with very old codebases. Don't use "chrono" because we have our own date/time types that were made before chrono even existed. Don't use standard library containers because we have our own containers that date back to before the STL was even stable.

      I wonder how many of these (or the Google style guide rules) would make sense for a project starting today from a blank .cpp file. Probably not many of them.

    • vitaut25 minutes ago
      Somewhat notable is that `char8_t` is banned with very reasonable motivation that applies to most codebases:

      > Use char and unprefixed character literals. Non-UTF-8 encodings are rare enough in Chromium that the value of distinguishing them at the type level is low, and char8_t* is not interconvertible with char* (what ~all Chromium, STL, and platform-specific APIs use), so using u8 prefixes would obligate us to insert casts everywhere. If you want to declare at a type level that a block of data is string-like and not an arbitrary binary blob, prefer std::string[_view] over char*.

      • ChrisSD7 minutes ago
        `char8_t` is probably one of the more baffling blunders of the standards committee.
  • dnmcan hour ago
    There are yet more interesting docs in the parent directory :)

    https://chromium.googlesource.com/chromium/src/+/main/styleg...

  • lateforwork21 minutes ago
    You almost never see a list of banned Java features (or even banned C# features). On the other hand any serious C++ development team is going to have a list of banned features. Java eliminated the features that you would want to ban.
  • Tempest1981an hour ago
    Where does it list the preferred alternatives to banned features?

    For example:

    > The <filesystem> header, which does not have sufficient support for testing, and suffers from inherent security vulnerabilities.

    • comex44 minutes ago
      For most of the banned library features, the preferred alternative is listed right there in the notes. <filesystem> is one of the exceptions.
    • TheRealPomaxan hour ago
      Gonna venture a guess and say probably https://www.chromium.org/developers, as that's where all the information for folks who actually need to know that kind of thing lives.
      • an hour ago
        undefined
    • jeffbeean hour ago
      base/files
  • ddtaylor2 hours ago
    Exceptions are banned, but an exception is made for Windows.
  • ameliusan hour ago
    Is there a way to make this formal, like in the code, making the compiler complain when you try to use these features?
    • 6r1724 minutes ago
      I'm not a c++ user but i'm pretty sure you should be able to pull-off a macro to do that ; in c you could alias the lib for something that breaks + alert ; I don't know how I would integrate such additional compiler checks in rust for other kinds of rules however - it's interesting to think about
    • loeg27 minutes ago
      It is relatively easy to check these things with static analyzers, if nothing else.
  • dfajgljsldkjag2 hours ago
    The banned list proves that context matters more than having the newest tools. These features work well for small apps but they cause problems in a project this size.
    • trinix91234 minutes ago
      IIRC a big part of Google’s coding guidelines is also about making it easy for people not heavily invested in a specific language to contribute safely. So not necessarily a project size but rather an organizational concern.

      They’d rather see it done the same way it would’ve been in any other similar language than with a language specific feature.

      There are also portability concerns in mind given that projects like Chromium have to be easily portable across a vast amount of platforms (this shows with things like long long which is also on the list).

    • loeg29 minutes ago
      Some of it is historical reasons or portability more than anything else. Chrome is an old C++ project and evolved many of its own versions of functionality before standardization; and there's benefit to staying on its own implementations rather than switching.
  • einpoklum18 minutes ago
    Since Chromium stopped allowing manifest-v2 extensions, i.e. significantly crippled what extensions can do and made it impossible to use some key extensions like uBlock Origin, I've decided to avoid it.

    Anyway, about these C++ conventions - to each software house its own I guess. I don't think banning exceptions altogether is appropriate; and I don't see the great benefit of using abseil (but feel free to convince me it's really that good.)

  • jesse__2 hours ago
    It's remarkable to me how many codebases ban exceptions and yet, somehow, people still insist they're good.
    • BeetleB2 hours ago
      > Our advice against using exceptions is not predicated on philosophical or moral grounds, but practical ones. ... Things would probably be different if we had to do it all over again from scratch.

      They are clearly not against them per se. It simply wasn't practical for them to include it into their codebase.

      And I think a lot of the cons of exceptions are handled in languages like F#, etc. If f calls g which calls h, and h throws an exception, the compiler will require you to deal with it somehow in g (either handle or explicitly propagate).

      • jesse__2 hours ago
        My issue with exceptions is also practical. If they didn't introduce significant stability issues, I'd have no problem. As it stands, it's impossible to write robust software that makes use of C++ exceptions.

        > the compiler will require you to deal with it somehow in g

        I agree, this is the sensible solution.

        • yunnpp43 minutes ago
          What stability issues?
      • jandrewrogers2 hours ago
        In low-level systems software, which is a primary use case for C++, exceptions can introduce nasty edge cases that are difficult to detect and reason about. The benefits are too small to justify the costs to reliability, robustness, and maintainability.

        Exceptions in high-level languages avoid many of these issues by virtue of being much further away from the metal. It is a mis-feature for a systems language. C++ was originally used for a lot of high-level application code where exceptions might make sense that you would never use C++ for today.

        • drnick142 minutes ago
          > In low-level systems software, which is a primary use case for C++

          I don't this this is true. There is A LOT of C++ for GUI applications, video games, all kind of utilities, scientific computing and others. In fact, I find that the transition to "modern" alternatives from native GUI toolkits in C/C++ has led to a regression in UI performance in general. Desktop programs performed better 20 years ago when everything was written in Win32, Qt, GTK and others and people did not rely on bloated Web toolkits for desktop development. Even today you can really feel how much more snappy and robust "old school" programs are relative to Electron and whatnot.

          • saghm9 minutes ago
            To clarify, you think that low-level systems software is only a secondary use case for C++? The part you quoted does not make claims about whether there are other primary use cases, just that low-level systems software is one of them, so it's not clear why it being useful elsewhere is a rebuttal of that.
        • BeetleBan hour ago
          > In low-level systems software, which is a primary use case for C++

          I can assure you: Most C++ SW is not written for low-level.

          > exceptions can introduce nasty edge cases that are difficult to detect and reason about.

          That's true, except for languages that ensure you can't simply forget that something deep down the stack can throw an exception.

          BTW, I'm not saying C++'s exceptions are in any way good. My point is that exceptions are bad in C++, and not necessarily bad in general.

          • jandrewrogers40 minutes ago
            > That's true, except for languages that ensure you can't simply forget that something deep down the stack can throw an exception.

            Sometimes it is not safe to unwind the stack. The language is not relevant. Not everything that touches your address space is your code or your process.

            Exception handlers must have logic and infrastructure to detect these unsafe conditions and then rewrite the control flow to avoid the unsafety. This both adds overhead to the non-exceptional happy path and makes the code flow significantly uglier.

            The underlying cause still exists when you don't use exceptions but the code for reasoning about it is highly localized and usually has no overhead because you already have the necessary context to deal with it cleanly.

          • beached_whalean hour ago
            The model of communicating errors with exceptions is really nice. The implementation in C++ ABI's is not done as well as it could be and that results in large sad path perf loss.
        • kllrnohjan hour ago
          If you forget to handle a C++ exception you get a clean crash. If you forget to handle a C error return you get undefined behavior and probably an exploit.

          Exceptions are more robust, not less.

          • nomelan hour ago
            Yeap. forgetting to propagate or handle an error provided in a return value is very very easy. If you fail to handle an exception, you halt.
        • senfiajan hour ago
          > "In low-level systems software, which is a primary use case for C++, exceptions can introduce nasty edge cases that are difficult to detect and reason about. The benefits are too small to justify the costs to reliability, robustness, and maintainability."

          Interestingly, Microsoft C / C++ compiler does support structured exception handling (SEH). It's used even in NT kernel and drivers. I'm not saying it's the same thing as C++ exceptions, since it's designed primarily for handling hardware faults and is simplified, but still shares some core principles (guarded region, stack unwinding, etc). So a limited version of exception handling can work fine even in a thing like an OS kernel.

          • jandrewrogers29 minutes ago
            FWIW, I think it is possible to make exception-like error handling work. A lot of systems code has infrastructure that looks like an exception handling framework if you squint.

            There are two main limitations. Currently, the compiler has no idea what can be safely unwound. You could likely annotate objects to provide this information. Second, there is currently no way to tell the compiler what to do with an object in the call stack may not be unwound safely.

            A lot of error handling code in C++ systems code essentially provides this but C++ exceptions can't use any of this information so it is applied manually.

        • matheusmoreiraan hour ago
          Exceptions are actually a form of code compression. Past some break even point they are a net benefit, even in embedded codebases. They're "bad" because the C++ implementation is garbage but it turns out it's possible to hack it into a much better shape:

          https://youtu.be/LorcxyJ9zr4

          • secondcomingan hour ago
            There is no such thing as the 'C++ implementation' of exceptions. Each vendor can do it differently.
        • beached_whalean hour ago
          C++ exceptions are fast for happy path and ABI locked for sad path. They could be much faster than they are currently. Khalil Estell did a few talks/bunch of work on the topic and saw great improvements. https://youtu.be/LorcxyJ9zr4
      • jayd162 hours ago
        Is this correct? I don't know F# but I thought it had unchecked exceptions. How does it handle using C# libs that throw unchecked exceptions?
      • heyitsdaad2 hours ago
        The “pros” list is exceptionally weak. This was clearly written by someone who doesn’t like exceptions. Can’t blame them.
    • ryandrakean hour ago
      I think reasonable people can disagree about whether C++ exceptions are "good" or not.

      There are things you can't do easily in C++ without using exceptions, like handling errors that happen in a constructor and handling when `new` cannot alloc memory. Plus, a lot of the standard library relies on exceptions. And of course there's the stylistic argument of clearly separating error-handling from the happy-path logic.

      I won't argue that it's popular to ban them, though. And often for good reasons.

    • azovan hour ago
      Most codebases that ban exceptions do it because they parrot Google.

      Google’s reasons for banning exceptions are historical, not technical. Sadly, this decision got enshrined in Google C++ Style Guide. The guide is otherwise pretty decent and is used by a lot of projects, but this particular part is IMO a disservice to the larger C++ ecosystem.

      • alextinglean hour ago
        I agree. I've worked on large C++ code bases that use exceptions, and they've never caused us any real problems.
    • tester7562 hours ago
      They're good for exceptional situations where foundamental, core assumptions are broken for some reason.

      In such scenario there's no error recovery, software is expected to shutdown and raise loud error.

      • spacechild19 minutes ago
        > They're good for exceptional situations where foundamental, core assumptions are broken for some reason.

        No, that's what assertions or contracts are for.

        Most exceptions are supposed to be handled. The alternative to exceptions in C++ are error codes and `std::expected::. They are used for errors that are expected to happen (even if they may be exceptional). You just shouldn't use exceptions for control flow. (I'm looking at you, Python :)

      • jesse__2 hours ago
        If you're planning on shutting down, what's the fundamental difference between throwing an exception, vs simply complaining loudly and calling exit() ..?
        • trinix9122 hours ago
          Sometimes it’s useful to handle the exception somewhere near its origin so you can close related resources, lockfiles, etc. without needing a VB6 style “On Error GoTo X” global error handler that has to account for all different contexts under which the exceptional situation might have occurred.
          • PhilipRomanan hour ago
            Your process can crash or be killed at any moment anyway. Depending on in-band cleanup is not reliable.
            • nomelan hour ago
              Sure, but there are many cases where you don't have to halt because you can cleanup and carry on.
          • matheusmoreiraan hour ago
            > a VB6 style “On Error GoTo X” global error handler that has to account for all different contexts under which the exceptional situation might have occurred

            ... That seems like a pretty accurate description of how exception handling mechanisms are implemented under the hood. :)

        • einpoklum11 minutes ago
          The code that's throwing an exception typically does not know that the exception catcher will shut anything down.

          And - very often, you would _not_ shut down. Examples:

          * Failure/error in an individual operation or action does not invalidate all others in the set of stuff to be done.

          * Failure/error regarding the interaction with one user does not mean the interaction with other users also has to fail.

          * Some things can be retried after failing, and may succeed later: I/O; things involving resource use, etc.

          * Some actions have more than one way to perform them, with the calling code not being able to know apriori whether all of them are appropriate. So, it tries one of them, if it fails tries another etc.

      • ljman hour ago
        Yet, if you can only explain an exception using the word ‘exception’ you’re not making any head way.

        I like the idea of an exception as a way to blow out of the current context in order for something else to catch it and handle in a generic manner. I don’t like the idea of an exception to hide errors or for conditional logic because you have to know what is handling it all. Much easier to handle it there and then, or use a type safe equivalent (like a maybe or either monad) or just blow that shit up as soon as you can’t recover from the unexpected.

      • dijit2 hours ago
        I use asserts for this purpose.
    • wvenable2 hours ago
      Looking at this ban list, they've removed everything from C++ that makes it fun. Come on people, who doesn't love a little std::function?!?

      On banning exceptions:"Things would probably be different if we had to do it all over again from scratch."

      https://google.github.io/styleguide/cppguide.html#Exceptions

  • WalterBrightan hour ago
    Modules are banned - they should have just copied D modules.
  • grougnax41 minutes ago
    C++ itself should be forever banned