8 ms·
Google's C++ Class
- freedomben 9y agoReally cool that Google is open sourcing this (as well as some of their other classes, like the machine learning one). Back in the day when I was learning to code, one had to trudge through a tome of esotericism after spending hours setting up your compiler. The snow was waist deep and uphill both ways. Seriously tho, this is great. My kids are now learning to code and the availability of great resources makes a huge difference.
- westoncb 9y agoI was hoping this was a class in the object-oriented language sense, and that somehow you'd use this class to instantiate a C++ in your program. Oh well—one can always dream.
- dieterrams 9y agoI was hoping it referred to a singular, monolithic C++ class in use by Google, to which any new C++ code must be added.
- rishabhparikh 9y agoWait, there are other ways to write C++ programs?
- woah 9y agoWe’ve adopted the monoclass architecture at our startup after reading about how well it works at Google.
- oblio 9y agoYes, but do you have the 50 engineers working on supporting tools for the monoclass architecture?
- jschwartzi 9y agoIt's pretty convenient in the embedded world too because it let's us minimize the size of the global initializer table our compiler outputs.
- foota 9y agoYou've heard of monorepo? Well check this out!
- oh_sigh 9y agoI used to touch some cc files at Google. I must say they are really horrible. Basically everything relies on bizarre internal frameworks glued together with opaque macros. Some of the least legible c++ code bases I've worked on.
- freedomben 9y agoDo you have any insights as to how those internal frameworks developed? One would think that with the ease and ubiquity of options these days, the homegrown framework (that is often proprietary to the company D-:) over open standards would be disappearing, but I still encounter it often, even in newer products.
- cmrdporcupine 9y agoThe commenter is welcome to his opinion but I think it's pretty wrong. Google has a few internal frameworks for things but mostly they evolved before the standard library had reasonable standardized equivalents and in cases where the standard has grown to encompass these things there's been orderly migration towards using the standard equivalent where it makes sense. It's a quite well maintained codebase with good sensible standards and review process as well as teams of people whose sole job it is to go through to analyze and migrate whole swathes of the code base for improvement. Plus alot of Google's internal stuff is being open sourced: https://opensource.googleblog.com/2017/09/introducing-abseil-new-common-libraries.html?m=1 https://opensource.googleblog.com/2017/09/introducing-abseil... You can waltz through that code as well as other open sourced things and judge for yourself on the quality.
- ariwilson 9y agoI've written a lot of C++ at Google. Macros are used sparingly, e.g. for command line flags: https://gflags.github.io/gflags/ https://gflags.github.io/gflags/ They are discouraged for use in place of an API: https://google.github.io/styleguide/cppguide.html#Preprocessor_Macros https://google.github.io/styleguide/cppguide.html#Preprocess...
- Upvoter33 9y agoThis sounds really off, and much different than my experience. Honestly, if there's a place in the world that produces quality C++, it's google.
- russellbeattie 9y agoLooking at the Getting Started section... Is this really the best way to write text out to the console in C++, or is it just traditional? cout << "Hello World!" << endl; It seems like something a bit simpler, readable and predictable would be better for beginners and/or experienced devs alike. I look at that and wonder where did cout come from? Is << a heredoc or some sort of pipe indirection, or am I bit-shifting? And is endl a keyword? Does it always follow cout? Is this like a Pascal endif? Oh, it's basically just a newline. What's wrong with just "\n"? I think C++ devs purposefully make their language as obtuse as possible. Maybe NOT starting with streamio and operator overloading might be a more modern way to start teaching the language?
- louiz 9y ago> I look at that and wonder where did cout come from? This is the biggest issue with this. STOP TEACHING “using namespace std;”. This is bad, bad, bad, and it sucks.
- chrisseaton 9y ago> Oh, it's basically just a newline. What's wrong with just "\n"? Not all platforms use “\n” for a new line.
- deleted 9y ago[deleted]
- gpderetta 9y agoThe std::stream will translate '\n' to what's required for the underlying platform, so it is a perfectly portable way to terminate a line. What std::endl really does is flush the internal stream buffer if any.
- johannes1234321 9y agoFor beginners I didn't have trouble with `cout` and `<<`. Just teach that that's how you print without going in details. This is more a problem with folks with experience in other languages, who know bitshift or heredoc syntax. But those can be teached in a more advanced way.
- kodr 9y agoI'm surprised that in their unit tests category, they are using CPPUnit instead of Google Unit (gunit). Any ideas why?
- cglouch 9y agoDoes anyone have any recommendations for learning to write modern, idiomatic C++? I used C++ in college for data structures and algorithms, so I have some familiarity with the basics. But we were mostly restricted to using C++98, and I'm not sure what the best practices look like these days with the changes introduced in 11, 14, and 17.
- Cieplak 9y agoA Tour of C++ http://www.stroustrup.com/Tour.html http://www.stroustrup.com/Tour.html
- ack79 9y agoI found this a good (if short) primer on the latest features: https://www.amazon.com/11-14-Tips-Understand-novelties/dp/1533401780 https://www.amazon.com/11-14-Tips-Understand-novelties/dp/15...
- nijaru 9y agoEffective Modern C++ by Scott Meyers is a good book that I've been supplementing with google searches. It goes over the main new features in '11 and '14 you would want to know.
- hermitdev 9y ago+1 for this book. Also, if your company can afford it, having Meyers or Sutter coming on site to give a day or two lecture to a group about Modern C++ is worth it. They both have great handout material, give great lectures/presentations and are very approachable with (even idiotic) questions. (disclaimer: I am an unattributed - due to company policy - interviewee for Meyers Effective Modern C++) edit: grammar
- binarycrusader 9y agoScott retired in 2015; I don't know if Scott is still active in terms of lectures/presentations: http://www.aristeia.com/ http://www.aristeia.com/
- mmel 9y agoIs there a list of all classes available under developers.google.com ? I could not find one from the front page. Edit: found it, https://developers.google.com/products/#a https://developers.google.com/products/#a
- ericfrederich 9y agoChapter 1: Use Golang
- dang 9y agoPlease don't do this here.
- majidazimi 9y agoChapter 32: Please (GC pauses the world) rewrite (GC pauses the world) it (GC pauses the world) with (GC pauses the world) C++.
- rosege 9y agoI guess Google is in favour of spaces over tabs by the look of that
- saagarjha 9y agoYes, they use 2 spaces: https://google.github.io/styleguide/cppguide.html https://google.github.io/styleguide/cppguide.html
- jrs95 9y agoUgh that's it, I never cared before, but now I'm switching to DuckDuckGo.
- berbec 9y agoBing for me: they open brace on the same line as a control statement/function declaration. Allman style forever! [1] 1. https://en.m.wikipedia.org/wiki/Indentation_style#Allman https://en.m.wikipedia.org/wiki/Indentation_style#Allman
- robbrit 9y agoI'm surprised that they don't go too much into good practice and style; these are heavily enforced in Google's code-bases. I would expect to see things like references and smart pointers, raw pointers are typically avoided as much as possible to avoid all the fun problems that come along with them.
- jgh 9y agoSure but naked pointers and manually managing allocation and deallocation force you to learn about how memory works in C++! Abstracting that stuff away is great for safety, but maybe less great for learning fundamentals.
- jgh 9y agoI really like the idea of google open sourcing stuff but it seems like they always do it in the worst way possible.. Maybe I'm looking at the wrong projects. For example WebRTC is buried in Chromium (so is QUIC iirc) and to build either of those things it's a lot of freaking work because you have to figure out Google's build system with the documentation doing its best to prevent you from figuring it out.
- ma2rten 9y agoI don't think that's true in general. Google released some widely used open-source projects like Kubernetes, Tensorflow, Android, Go, Chromonium and Angular.
- pknopf 9y agoI'd say it's generally true with their native projects (C/C++). With their higher level stuff, they do a lot better.
- agnivade 9y agoDon't forget protocol buffers !
- pjmlp 9y agoAndroid is one of the poster childs how bad Google documentation is scattered all over Stackoverflow, Google blogs, Medium articles, outdated entries in official documentation. Then the multiple revisions of build tools and plugins, support library releases with multiple issues on release day, breakages on Android Studio in spite of several months in beta.
- jschwartzi 9y agoAOSP in particular has very little documentation. When I was learning it the only thing I could find was a draft of an O'Reilly book. That, or reading the source code for the build system.
- helthanatos 9y ago
- hupsterlupster 9y agoPlease for god's sake, let this language die. There has been nothing this bad in the entire history of programming languages and now Google wants to impose more undefined behaviors on our professional lives. Anyone starting out with learning should go through https://www.rust-lang.org https://www.rust-lang.org path and anyone who's wise enough to understand pros and cons of such language will automatically choose rustlang over them in 2018. Even in comparison with C, it falls short. C was a beautifully designed language. The features of the C language were added with purpose. They were intended to solve a real problem and solve that problem they did. Simple arithmetic with promotion. Automatic register allocation with a portable (between compilers) ABI. A simple-but-handy preprocessor. And so on. C is also a lean language: a single person can feasibly write a C compiler in a relatively short time (tinycc is a C99-compliant C compiler written in just 65kLOC of C!). C set out to achieve a clear goal and it achieved its goal. Thanks to the cleanness of C lots of quality compilers came on the market quickly and even decent OSS solutions were available early on. In contrast, C++ never had a clear goal. The features of C++ were added almost at random. Stroustrup's original idea was essentially "C is cool and OOP is cool so let's bolt OOP onto C". Retrospectively, OOP was massively overhyped and is the wrong tool for the job for most of the people most of the time. Half of the GoF design patterns just emulate idioms from functional programming. Real OOP languages like Smalltalk and IO express many useful things that C++ cannot. The feature that C needed most was perhaps parametric polymorphism (aka generics in Java and C#, first seen in ML in the late 1970s) but instead of that C++ got templates that weren't designed to solve any particular problem but rather to kind of solve several completely unrelated problems (e.g. generics and metaprogramming). Someone actually discovered by accident that C++ templates are Turing complete and they wrote and published a program that computed prime numbers at compile time. Wow. A remarkable observation that led to decades of template abuse where people used templates to solve problems much better solved by other pre-existing solutions such Lisp macros and ML polymorphism. Worse, this abuse led to even more language features being piled on top, like template partial specialization. The massive incidental complexity in C++ made it almost impossible to write a working compiler. For example, it remains extremely difficult to write a parser for the C++ language. The syntax also has horrible aspects like List<Set<int>> being interpreted as logical shift right. None of the original C++ compilers were reliable. During my PhD in 2000-2004 I was still stumbling upon dozens of bugs in C++ compilers from GNU, Intel and SGI. Only after two decades did we start to see solid C++ compilers (by which time C++ was in decline in industry due to Java and C#). C++ is said to be fast but the reality is that C++ is essentially only fast when you write C-like code and even then it is only fast for certain kinds of programs. Due to the "you don't pay for what you don't use" attitude, C++ is generally inefficient. RAII injects lots of unnecessary function calls at the end of scope, sometimes even expensive virtual calls. These calls often require data that would otherwise be dead so the data are kept alive, increasing register pressure and spilling and decreasing performance. The C++ exception mechanism is very inefficient (~6x slower than OCaml) because it unwinds the stack frame by frame calling destructors rather than long jumping. Allocation with new and delete is slow compared to a modern garbage collector so people are encouraged to use STL collections but these pre-allocate huge blocks of memory in comparison so you've lost the memory-efficiency of C and then you are advised to write your own STL allocator which is no better than using C in the first place. One of the main long-standing advantages of C over modern languages is the unpredictable latency incurred by garbage collectors. C++ offers the worst of both worlds by not having a garbage collector (making it impossible to leverage useful concepts like purely functional data structures properly) but it encourages all destructors to avalanche so you get unbounded pause times (worse than any production GC). Although templates are abused for metaprogramming they are very poor at it and C++ has no real support for metaprogramming. For example, you cannot write an efficient portable regular expression library in C++ because there is no way to do run-time code generation and compilation as you can in Java, C# and languages dating back to Lisp (1960). So while Java and C# have had regular expressions in their standard libraries for well over 10 years, C++ only just got them and they are slow. C++ is so complicated that even world experts make rookie mistakes with it. Herb Sutter works for Microsoft and sits on the C++ standards committee where he influences the future of C++. In a lecture he gave his favorite 10-line C++ program, a thread-safe object cache. Someone pointed out that it leaks memory. My personal feeling is that the new Rust programming language is what C++ should have been. It has useful known features like generics, discriminated unions and pattern matching and useful new features like memory safety without garbage collection.