I work at Google. This video barely scratches the surface of Google's C++ style guide. IMHO, the style guide is a wonderful thing, and makes C++ a pleasure to work with at Google. Although your preferences may not agree with every little detail, the consistent application of the style guide across a massive codebase is highly valuable to everyone that will ever lay eyes on your code. There could easily be hundreds or thousands of engineers that interact with your code - interfacing with its API, extending it's functionality, debugging it, improving it's performance, etc. Every decision made by the style guide is one less thing all those engineers have to consider.
@LowLevelTV Жыл бұрын
Wow that’s awesome! Thanks for watching. Yeah there were tons of interesting bits I wanted to go into but wouldn’t make a good video.
@name_cpp Жыл бұрын
Agreed, google C++ is the really nice to work with. I also really love the absl and base libraries. I was intimidated at first by how large the codebase was but the consistency and cleanliness of the code helped a lot
@8Trails50 Жыл бұрын
just the no exception rule is so nice. I hate exceptions in C++. It's not like in Java where you can declare "throws". You have to understand all the callers. And you have to trust that someone doesn't add an exception in the future and breaks your code.
@nathanoy_ Жыл бұрын
Does Google have a linter that can hint to the programmer that they are possibly violating the style guide?
@hellowill Жыл бұрын
@mipmipmipmipmip Google engineering seems pretty damn good. Their product side seems lacking.
@astrahcat12126 ай бұрын
I like reading 90s C++ projects, they released the source code for 3D Movie Maker recently and been reading through that. It delves you into another time, a 'simpler' time.
@LastKidStanding-92 ай бұрын
can i ask you where you can find the 90s C++ projects ? is there a book or website i can read on it ?
@robgrainger5314Ай бұрын
@@LastKidStanding-9 Just look for any projects that haven't been updated for a while. SourceForge still has plenty for example.
@polvoazul21 күн бұрын
However, having no smart pointers is very sad
@chrisrockscode1202 Жыл бұрын
One of my favorite videos on KZbin, I never even thought of looking at style guides. With a good amount of C++ under my belt I can totally see these guides being useful
@LowLevelTV Жыл бұрын
Glad you enjoyed it!
@bitw1se Жыл бұрын
The multiple inheritance example is bad imo, because in that case you should actually let "Child" inherit from "Person", and not "Mother" and "Father". Inheritance is usually used to generalize something and abstract it away, like a "Table" is a type of "Furniture", but not a child is a mother and a father.
@TheRationalPi Жыл бұрын
Definitely agree, he could have used a better example there. To expand on your furniture idea, you could have something like a "convertible sofa" that inherits both from "bed" and "couch," which are themselves inheriting from "furniture."
@2JulioHD Жыл бұрын
Mother and Father are Persons after all, so I see no issue here. Child referes to them being child classes, but that doesn't mean the example is incorrect. As you said, inheritance is there to generalize something and Person is more general then Mother and Father, they are something more specific.
@TheRationalPi Жыл бұрын
@@2JulioHD I think @ruarq1510's broader point here is that the Child/Mother/Father example is confusing because it conflates biological "inheritance" between children and their parents with class "inheritance" between a subclass and its base class. It reinforces a metaphor that is not particularly good to begin with.
@bitw1se Жыл бұрын
@@2JulioHD Yeah, but you usually say „a mother is a type of Person“ or „a father is a type of person“, that’s how inheritance works. In the example it doesn’t make sense to let „Child“ in inherit from „Mother“ and „Father“, since a child is a person and not a mother or a father. Instead, the Child class should have the mother and father class as an attribute. In the real world, yes, a child inherits the genes from their parents, but genetic inheritance is not the same as inheritance in programming, it’s a completely different thing. You also can’t generalize a child to be a mother or a father, but that’s what polymorphism does.
@Dennis1990111 ай бұрын
@@2JulioHD Inheritance should be logical. Is a ? And this should follow through the inheritance structure. In this case, mother and father are both people. Child is a person. But child is not a mother nor a father, and especially can't be both at the same time.
@ToCarlosHuevos Жыл бұрын
I never write any comments on YT but I really feel like doing it this time. I'm a C++ developer and it's my first job ever (same thing for my teammates), so we struggle with many of these details every day because I think it's quite hard to decide on your own which is the best practice. It's nice to know it's not only me and that it's a real deal out there to write good code in C++ (I also thought those many details of the language make it very powerful but difficult to manage correctly, but I thought it was just me being an inexperienced engineer). I really enjoyed this video, I would also like to see more C++ guidelines and stuff (obviously). Keep up the good work!
@KohuGaly Жыл бұрын
In C++, having an explicit style-guide that is enforced in code-reviews (and preferably by some automatic linters too) is absolutely essential. C++ is just too bloated with 40 years worth of legacy features that often interact with each other in weird ways. You have to pick some subset of C++ that all of you understand and use only that subset. Many such style guides are publicly available. You can copy them or take inspiration and make your own.
@ToCarlosHuevos Жыл бұрын
@@KohuGaly Thank you! We try to use C++ 11, which we found quite readable, but tbh we don't strictly follow any standard. Most of the code that we inherited was programmed in a C style (but with classes) instead of C++ with a standard, so that poses a problem.
@KohuGaly Жыл бұрын
@@ToCarlosHuevos I know exactly what you mean :-D I recently got a job as a embedded automotive C/C++ developer. The standard used there is MISRA C++. The oldest parts of the code base pre-date the standard and are literally written in C. "Fun" fact: MISRA C++ forbids all forms of pointer arithmetic except indexing into fixed-sized arrays. If you read the last two sentences and have basic experience in C and C++, you know exactly why "Fun" is in quotes. 😀
@sharana.p5921 Жыл бұрын
@@KohuGaly Hi, I'm sharan. I'm working in a startup called Yali Mobility. We are making electric three wheeler for wheelchair users. I'm just a year old experienced programmer. Currently I'm the only person who programs the vehicle. So I'm struggling alot, kindly help me in some way like standardization of the code etc. This may help us to make the work flow and performance of the code much better. Thank you
@KohuGaly Жыл бұрын
@@sharana.p5921 Hi Sharan. Unfortunately, you are probably more skilled in C++ than I am. I was just lucky to land a job in a large corporate, with a lot of pre-existing expertise and know-how to learn from. Not gonna lie, writing software for a road vehicle, as a 1-year-of-experience solo developer... that sounds like a pretty reliable way to kill someone by software bug.
@decky1990 Жыл бұрын
Aw man, two spaces??? I’m yet to try looking at code like that, but four has always looked better
@pierelenigus8598 Жыл бұрын
4 spaces has always been the standard for tabs. see ansi for the real world proper use of tabs. google has retards running the show and the two tab mandate has already hurt their developers immensely. but they would never tell the truth.
@matthewfennell7886 Жыл бұрын
I like two personally, but professionally 4 with a reasonable column limit is better as it dissuades excessive nesting
@itzvoko1 Жыл бұрын
Two spaces are very hard to see. IT'S a C++ CODE, NOT HTML!
@nullptr. Жыл бұрын
I'm sure theres a quick way to replace whatever tabs you prefer with two spaces before commiting the code
@dickheadrecs Жыл бұрын
this quibble is by far the least important thing about writing c++
@SevenRiderAirForce Жыл бұрын
Composition over inheritance and RAII (smart pointers) are the biggest lessons here. They drastically de-shittify your code.
@hyde40047 ай бұрын
You would figure everyone would know by now that most inheritance is shit but I guess there's still people out there crafting the most illegible polymorphisms possible.
@User948Z7Z-w7n5 ай бұрын
@@hyde4004 I haven't figured out. Guide me
@prumchhangsreng9793 ай бұрын
@@hyde4004does inheritance still have good use cases? Genuinely asking as a learner
@ProGaming-kb9io3 ай бұрын
"OOP vs _" is a dumb conversation most of the time, just use the best tool for the work, while taking into account the conditions and project you currently are in.
@SevenRiderAirForce3 ай бұрын
@@prumchhangsreng979 Inheritance is useful for interfaces. A Base class defines the interface, and then Derived1 provides the implementation. You can create Derived2 and Derived3 with different implementations of the same interface. It's very useful. People get stuck by putting implementation details in the base class and then trying to change or get around it in the derived classes. Wrong. Factor it out and include it as necessary in derived classes. The classic example is vector. Nobody extends from vector, they include it as needed.
@silent_ptr Жыл бұрын
If Google doesn't like using exceptions cause of flow control stuff what do they prefer to use instead
@gerardmarquinarubio9492 Жыл бұрын
Return values
@Nape420 Жыл бұрын
Yeah, this was just glossed over as if its some minor thing. How do they handle their exceptions and, more importantly, errors then?
@trainerprecious1218 Жыл бұрын
probably simple optional or rust like solution `Result`
@Nape420 Жыл бұрын
@@jmickeyd53 Thanks for the info. Makes sense
@JeffCaplan313 Жыл бұрын
@@Nape420 They probably collect requirements and validate their input.
@kuhluhOG Жыл бұрын
1:26 Yeah, that's the point of tab. That everyone who reads the code can configure it the way they want to be. That way no matter who looks at the code, it will always be with the indentation the person feels comfortable with. Especially 2 spaces are very little and becomes over the course of a work day quite eye straining (which btw is the reason why the Linux kernel defines 8 spaces..., tabs would still be better tho). This is quite frankly the type of rule where I would configure my setup to convert 2 spaces into a tab when opening the file and on save convert a tab into 2 spaces...
@m4rch3n1ng Жыл бұрын
while i am not even close to a kernel dev, looking through the code, they seem to be using tabs, and the top-level .clang-format has the config "UseTab: Always" enabled
@miguelborges7913 Жыл бұрын
I'm advocate of using tabs over spaces too, due to the feature of being able to change the tab size in every editor (it visually replaces tabs with spaces, but the text data is just tabs). Not only it saves more memory, it makes it also more portable.
@spfy Жыл бұрын
@@m4rch3n1ng Kernel style guide does say to use tabs, but also defines tabs as being 8 spaces. IIRC, there is also a column-width limit of 80 characters, which means the size of the tabs needs to be defined. Which kinda nullifies the point of tabs doesn't it, since someone that likes tabs as 4 spaces will potentially be formatting their line-wraps incorrectly lol
@forbiddenera Жыл бұрын
Tabs set to 4 ftw and half spaced returns switch(smt) { case 0: do_smth(); break; case 1: for (i=0;i
@m4rch3n1ng Жыл бұрын
@@spfy oh that makes a lot of sense in that context then, i didn't even think about column-width - i just thought it was a visual thing (that you should be able to override in your editor)
@9SMTM6 Жыл бұрын
The worst Java code I've ever seen was part of (Googles) easier mediapipe API. They had DEEP Inheritance, manually written getters and setters which contained a few surprises (like giving back properties that already had their own getters), made intensive use of method overloading and inherited methods including methods, and these methods often took parameters that were named in a way to imply they had a certain function, only to be thrown away/used in a different way, or they were actually set, but that didn't matter as at a later point in the lifecycle these values would be overwritten before doing anything. Combine that with the need to also use the not easy and not well documented camera2 API (which they combined with some parts of CameraX) and that under all that Spagetti mess they used JNI to call C++ code that also wasn't particularly well documented, and I got nowhere but sure wasted a lot of time thinking I finally got to the target. Honestly, while we're at this, LARGE swaths of the Android API require you to use implementation Inheritance, so thanks for forcing me to do the things you forbid your engineers to do Google. Sorry for the rant, I'm still super salty from that experience. What they archived with mediapipe is remarkable, and it's nice they made it open source. But mediapipe did not keep it's promises (real time body tracking on IOT, pretty much every mobile - top models released after mediapipe - we tried didn't get over sustained 10 FPS, while being at best at a mean of 18 FPS), and the API was a nightmare and not at all flexible enough for our needs.
@jordixboy Жыл бұрын
These are general guidelines. Not all engineers/teams follow them.
@ReadThisOnly Жыл бұрын
@@jordixboy also for c++ lol, not java
@nikitapustovoi8987 Жыл бұрын
Android is a bit separated at Google
@uwuLegacy Жыл бұрын
Google has tens of thousands of engineers working across hundreds of projects. Whoever developed this style guide probably doesn’t even touch Java code in his day-to-day.
@tatianaes3354 Жыл бұрын
Android was a purchase for Google, had nothing to do with it in the core. So no wonder its code does not follow Google’s guidelines. Besides, those guidelines changed a lot from the 1990s to 2000s, then to the 2010s, and then to the 2020s, so Google’s catalogue of code is a huge nasty mess. Probably only MS is worse than Google.
@haxney Жыл бұрын
Great video! A few things, from a Google engineer (not speaking for the whole company, of course). 1. We absolutely have tech debt. These things help reduce certain kinds of tech debt, but it still exists all over the place. We're not magical; we make bad choices which we're then stuck with. 2. The lexical code style goes beyond just tabs vs spaces. Our code review system will yell at you if the code isn't formatted according to whatever clang-format would produce. Our style guide for indentation, line breaks, etc, is just "run clang-format and use that". I don't always like the choices which clang-format makes, but it's infinitely better for everyone to use the same standard than for me to use a standard I like more, but for diffs to be larger due to whitespace changes. 3. An important part of the style guide boils down to "don't get clever with template metaprogramming." I've been writing C++ for about 3 years now, and as soon as things get beyond the most basic template usage, I'm lost. If you're relying on SFINAE, you should probably reach for a different solution. Same thing once you start dealing with rvalues and lvalues. There are some places where those features really do make sense, but for things like "call this service. If foo is greater than 25, set bar to true", you probably don't need that stuff.
@spinsmctwist2719 Жыл бұрын
I appreciate the insight!
@TheOnlyAndreySotnikov Жыл бұрын
Item 3 is a typical complaint of someone who learned 30% of the language but still decided to put it on his resume.
@anonimowelwiatko9811 Жыл бұрын
@@TheOnlyAndreySotnikov Dude, do you work with someone else code beyond your own? Templates can get really confusing. It's all abstraction so there is certain difficulty to comprehend someone else idea in form of code, specially if it doesn't read well. I spent many hours trying to decipher what author had in mind while creating his solution to problem so speaking from experience (just lately I had to decouple 3000 lines long class implementation written and edited by different people). It included weird lambda expressions and templates.
@TheOnlyAndreySotnikov Жыл бұрын
@@anonimowelwiatko9811 Dude yourself. The most frequent source of complaints about templates is a lack of education. Software engineering is a unique field. In any other area, you can't say: "or, integrals is a too complicated abstraction for me," and claim you know calculus. Not only that but in software engineering, you can also force all your colleagues to dumb down to your level and never use integrals in 𝘵𝘩𝘦𝘪𝘳 work. In any other profession, you just shut up and learn. In software engineering, you complain. Meanwhile, not "professional" programmers successfully learn, write, and maintain huge packages written in TeX, which is way more complicated to comprehend than C++ templates. So, 𝘥𝘶𝘥𝘦, learn and practice.
@ignotlichitikus931410 ай бұрын
instead of SFINAE you should use more modern features like concepts and if constexpr from c++17 many things that are written in older versions of c++ can be written more clearly and using less "magic syntax" in the latest versions of the language (c++20/23)
@brendanhansknecht4650 Жыл бұрын
Google doesnt allow exceptions because that is what they happened to pick years ago. They have debated enabling exceptions (they are generally more performant on the happy case for example), but mixing error returning code and exception code is a bad idea. They say that if they were to start from scratch today, they probably would use exceptions instead of error returns.
@amrojjeh Жыл бұрын
Do you have a source?
@brendanhansknecht4650 Жыл бұрын
@@amrojjeh my first reply doesn't seem to have gone through since I put a link in it... Anyway, two sources: 1. It mentions in the style guide under the exception section > Things would probably be different if we had to do it all over again from scratch. 2. Reading an internal email chain about them.
@amrojjeh Жыл бұрын
@@brendanhansknecht4650 Thanks! That's quite interesting. I personally find exceptions to be really strange. I've found that code is much more readable whenever the error is embedded within the type itself. I suppose though that's not really an option in C++
@johnshaw6702 Жыл бұрын
@@amrojjeh I started out as a self taught C programmer designing and writing all the code, so I know you can write good code without exceptions. Everything you can do in C, you can do in C++. The only issue I ever had with exceptions in some languages is that some programmers use them to be lazy, instead of preventing possible exceptions from occurring in the first place. An exception should be an exceptional case (memory or similar issue), not a go to solution.
@amrojjeh Жыл бұрын
@@johnshaw6702 I personally still find it difficult to assess what should be a typical failure and what's an exception. Would you have any resources to recommend on that?
@JeremyCoppin8 ай бұрын
My absolute favourite is working on a code base where a class inherits class templates that inherit other templates that inherit an interface. That's the ultimate in "FU figure this out" coding..
@ХузинТимур21 күн бұрын
Don't forget that they also use the derived class a template parameter for grandparent class.
@gvanvoor6 ай бұрын
2:27: make_unique< foo > will return a unique_ptr< foo >, not a foo (assuming the std namespace was dropped)
@wlockuz4467 Жыл бұрын
People often role their eyes at style guides, or having their PRs blocked because it doesn't follow a certain style rule. They don't understand how easy it makes for third parties looking at your code.
@TheArakan94 Жыл бұрын
but why would you consider "it will look the same in different editors" to be a plus? Main reason for tabs is exactly that - it allows devs to view the indentation to way they prefer it. Someone likes small indentation, someone needs large one.
@RigelOrionBeta Жыл бұрын
Ever initialized a large struct array with data of varrying sizes, and wanted it to look like a table of rows and columns? Your tabs may make it look nice on your screen, but for someone with a different tab size, it will look awful. With spaces it looks identical to each editor. That goes for any code that you wish to line up, such as when you have a function with many params that you wish to line up on a newline.
@TheArakan94 Жыл бұрын
@@RigelOrionBeta well in these corner cases, nothing is stopping me from using spaces whenever "lining up" is my goal.. It's not argument for universal space usage for indentation.
@tidepool5400 Жыл бұрын
At a large company where potentially a hundred people might look at the same code, it’s helpful to ensure they look at the EXACT same code. You’re right, it limits freedom and might make some developers slightly less happy, but it removes the possibility of bugs due to visual differences between developers.
@tdplay4135 Жыл бұрын
@@RigelOrionBeta That is alignment, not indentation.
@monochromeart7311 Жыл бұрын
@@tidepool5400 tabs are more accessible for people with vision problems. Great minds can be left out just because they were unfortunate to have really bad vision.
@kuhluhOG Жыл бұрын
2:25 Ehm, no, it returns a unique_ptr (a smart pointer) to Foo (and creates a Foo while doing so).
@yevgeniysimonov590611 ай бұрын
It is true that in large projects using exceptions might be difficult to maintain, especially when there are many developers working on the same piece of code, however, they are good to have in situations where we receive completely unexpected data, corrupted data, or for validation reasons to bullet proof complex data pipelines.
@theintjengineer Жыл бұрын
I don't agree with 100% of the guide (e.g. I can't have that 2-space indentation haha), but I loved the fact the video is about C++❤️ Please keep it up. Greetings from Germany.
@opra-bodibotond1775 Жыл бұрын
In the Inheritance example, the problem with the Mother-Father-Child structure isn't inherently an issue of Inheritance, instead it is just really bad design. One shouldn't use composition just to avoid inheritance, but use it where it makes sense (like in the example).
@9SMTM6 Жыл бұрын
Well that isnt the only issue present with Inheritance. Other nice situations are long Inheritance chains, especially if combined with overloads and stuff like specialization. Inheritance chains can already make it difficult to track down where a method is coming from, but with specialization of overloaded functions, or the overwrite of methods that get called by overloaded methods in ancestors, things can get super nasty, and quick at that. After experiencing the lack of inheritance in Rust I don't want it back.
@jordixboy Жыл бұрын
inheritance does more bad than good.
@makezdtem Жыл бұрын
bojler eladó
@dnull Жыл бұрын
inheritance doesn't work very well in the real world. obviously, as you stated, one shouldn't use composition *just* to avoid inheritance, but 1. in most cases it makes much more sense to use composition, and 2. it is generally better to avoid inheritance at all costs. now from what i see in the recent years, programmers gradually move away from OOP (and there are good reasons for that), and even hardcore OOP users avoid using some of its features. generally, i think it's a good thing, because we all know what overuse of OOP features of the 90s and early 00s resulted in. obviously, if you suck at code it doesn't matter what paradigm you use (and vice versa), but again, there are reasons to avoid OOP anyway.
@theultimateevil3430 Жыл бұрын
One should always use composition. Inheritance brings ambiguity by definition and hides the implementation details from the person who's working inside the class code. You can still have dynamic dispatch and all benefits of polymorphism without classic inheritance, even in C++ though it's not enforced by the compiler. It is enforced in Rust.
@CartoType Жыл бұрын
It may work for Google but some of this wouldn't work for me; and I've been a successful C++ programmer for 30 years now. Part of that is because Google has thousands of programmers, and to an extent the convoy has to travel at the speed of the slowest ship, but there are other foot-shooting rules like not using exceptions. If the overall project doesn't use exceptions, the way to add an exception-using module is for the module to expose an API that doesn't use exceptions, but to use them inside the module. Each API function can easily trap exceptions and convert them into error codes or whatever. I use this technique myself.
@earlye6 ай бұрын
If I were writing this guide, I would say "use exceptions for all error conditions," "write exception safe code," "generally do not catch exceptions other than when you could conceivably recover," and "use RAII to avoid needing to catch exceptions." It's a shame IMHO that exceptions seem to be losing favor industry-wide, as they really _can_ make error handling easier and less error-prone. :sigh:
@mastermati7732 ай бұрын
I found always one problem: How 'problematic' a problem must be to switch from status code, etc. to exceptions? For me, it was always unrecoverability. So while exceptions seems cool, there should be no more than 2-3 try-catches per entire humonguous project.
@earlye2 ай бұрын
@@mastermati773 I agree that there should almost never be a try/catch. Basically, _main_ or _http/thread entrypoint_, only. I think it's not so much a matter of "switching from status code" to exceptions as deciding at the onset of a project that you're going to automate as much error handling as possible and use them from the start. If you're knee-deep in a project that uses error codes, switching to exceptions is probably not a great idea, and certainly a huge effort.
@vincenthilla3762 Жыл бұрын
Great video and all... just please don't build an inheritance relationship Child -> Mother, Father -> Person. Even with inheritance as a concept, that does not make sense. But especially in C++, inheritance means "is-a" and a Child is not always a Mother. Everything that would apply to Mother, would also be applicable to Child under C++.
@DominikLoeffler17 ай бұрын
6:40 --> An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.
@brandyballoon8 ай бұрын
Every course I've done uses the classic animals or shapes example to teach inheritance, but none of them demonstrated a real world use case for inheritance. So the result of that is that I understand the principle of inheritance, but I have no idea when it might be appropriate to actually use it.
@User948Z7Z-w7n5 ай бұрын
i suggest you find a good read on OOP principles. I have one but it's in Korean and there is no English translation afaik. The book is '객체지향의 사실과 오해' (truth and misconception about oop) I'm reading it now so I can't summarize the key points for you either.
@poleve54093 ай бұрын
read the gang of four book for oop design patterns
@brandyballoon3 ай бұрын
@@poleve5409 Thanks for the recommendation
@mdrehan4939 ай бұрын
The diamond problem in Inheritance, Can be solved in C++. Because there is the concept of virtual ness which deals with that problem and prevent the base class to inherit multiple times
@darkwoodmovies9 ай бұрын
The Google C++ style guide was my bible for a long time. It's incredible and makes C++ truly beautiful and easy. I miss my C++ days :)
@lightning_118 ай бұрын
People still complain at me a bit sometimes because I use 2 spaces as code indentation... although I know they'll complain no matter what I use, so I just try to get past it.
@LiminalThought7 ай бұрын
What do you code in now? I’m mostly curious because I started off with Python 2 years ago, and I’m only now getting into C++.
@darkwoodmovies7 ай бұрын
@@LiminalThought Haha I fell to the dark side, I do JS/TS for web dev now
@logicaestrex22782 ай бұрын
@@LiminalThought I recommend learning c first, and if your really serious about programming, 6502 assembly. NES hacker has a great crash course on it :)
@THE16THPHANTOM3 ай бұрын
finally. a standard/convention you can point to. i will go through this guide when i have sometime. but the two spaces is pretty rough. you can barely tell if its two spaces or not when you are looking at it unless its super zoomed in. code just looks weirdly off and you can't tell if its because of the font or if its the editor.
@kebien6020 Жыл бұрын
2:29 If the type is not "Widget" then the function is wrong, not the convention. Also, just hover over the variable name to see the type in most editors. 3:52 If the class/function needs to *take over* the ownership then pass a smart pointer. Otherwise, just .get() a regular pointer from the smart pointer. Regular pointers are not evil, they are just not good at owing stuff. 4:56 That example only leaks memory because it's not using RAII, not due to exceptions. I'm sure that Google has good reasons to avoid exceptions, but this one ain't it. 6:17 Completely agree, avoid inheritance like the plague. As for interface inheritance, C++20 arguably has an even better option: concepts. Concepts can only restrict an interface and never the implementation, and unlike inheritance-based interfaces, concepts do not have a runtime impact. (The trade-off is that they do trap you in template-land).
@erikb4407 Жыл бұрын
Template-land is inescapable 😔 once trapped, you'll never see the light of explicit types again
@rasimbotАй бұрын
Then how do they use features that internally allocate a heap memory, like vector or make_shared? These things expect a bad_alloc exception when the allocation fails
@casperes091211 ай бұрын
The fact code looks different on different editors with the tab character is the benefit of tabs in my opinion. It allows personal preference to apply to something universal. The individual programmer can choose if they prefer the tab to look like 2 or 4 spaces or whatever else. It's technically also one byte less than two space characters but that hardly matters today
@Asto50811 ай бұрын
I honestly think large part of this debate is really caused by Python and its inability to deal with mixed tabs and spaces. I also had issues with editing YAML files in the past for the same reason. I actually abandoned tabs for whitespaces because I was just sick of having random syntax errors showing up at runtime just because I used a \t in a sea of whitespaces in my editor.
@casperes091211 ай бұрын
@@Asto508 I only deal with languages that parse both the same these days.
@BoletusDeluxe8 ай бұрын
Yeah I never understood why people are so adamant to make indentation look the same on both machines. If you need to align something visually, use spaces sure, but indentation is there to represent scopes, not for alignment. Removing tabs also hurts developers who use large or small tabs for accessibility or because it helps them be more productive.
@brandyballoon8 ай бұрын
I think the issue is that allowing tabs can result in the use of both tabs and spaces for vertical alignment. If the tab stop locations are changed, it gets messed up.
@BoletusDeluxe8 ай бұрын
@@brandyballoon trust your programmers to use tabs for indentation and spaces for alignment? There's so many tools to detect tabs and spaces. Most editors let you visualise tabs and spaces.
@nerdbot4446 Жыл бұрын
Why is it considered good when the indention is using spaces to force the same width on every developers editor? The important thing about indention is the *level* of it to convey the depth of a line in a code strcuture. A tab perfectly matches that. One tab = one level. And the width of the code is configurable to individual preferences without interfering with other developers. Depending on the monitor a dev uses (or when having a visual impairment) you might want to have a visually easier to distinct indention (like on a high resolution / high dpi one) to see the levels better. Or you want to utilize it to force yourself to write better code by making it painful to deeply nest. I absolutely don't see why on earth using spaces - especially just 2 per level - is a good idea... If you disagree, feel free to throw convincing arguments at me
@RigelOrionBeta Жыл бұрын
Consider a function that has a lot of params. What do you do? You put them on the next line. But what if you want to line them up with the previous params? You cant do that with tabs, because tabs are differing in size. Your lined up params may look nice on your screen, but not someone else's who has configured tabs to be different. There are numerous other examples of why tabs are worse than spaces. I used to be a fan of tabs, but once I started working with other developers on large projects, I realized tabs are not a good idea. There are too many formatting problems with them. Instead, your project should agree on an indentation size of spaces, say 2 or 4. Then configure your editor so that hitting the tab key adds 4 spaces instead of a tab. The key thing here is you can get all the functionality of the tab key, without all of the visualization differences that the tab character will create.
@pauldegroot1959 Жыл бұрын
@@RigelOrionBeta "But what if you want to line them up with the previous params? You cant do that with tabs" You can use tabs to get to the right indentation level, then line up with spaces.
@RigelOrionBeta Жыл бұрын
@@pauldegroot1959 That's pretty error prone. How can you easily tell what white space is a tab or a space? At that point, you're mostly just using spaces anyway for alignment. In fact, if you just configured your tab to put spaces instead, you'd save time tabbing to the alignment point, since it inserts multiple spaces, instead of using spaces. You'd get there 2-4 times faster than if you used spaces. There is really no point. I used to do exactly what you are describing, but you can never be immediately sure that you have tabs and not spaces. Why bother?
@ghosthunter0950 Жыл бұрын
@@RigelOrionBeta I didn't have an opinion but I think you've convinced me on spaces.
@monochromeart7311 Жыл бұрын
@@RigelOrionBeta 1. You can make whitespace visible. 2. Tabs are for indention, spaces are for alignment. 3. You can align like the following: int long_function_name( TypeFoo param1, TypeBar param2, Data ¶m3 ) { .... }
@saaah707 Жыл бұрын
1:20 That's actually the prime argument in favor of tabs. They use different tabs because they have different setups. I believe in Tabs for indentation, Spaces for alignment, i.e. anything after the initial tabs on a line should automatically expand into spaces. All editors should support this (but most don't)
@TheMohawkNinja7 ай бұрын
That no exceptions rule sounds strange. I wrote a dice rolling program that takes a RPG-formatted (e.g. d20, 3d8, 2d6+1) string and parses out the number of dice, faces of dice, and modifier. It was much faster to just write "try{std::stoi(s);}catch(...){}" then to for-loop through each string and do an isdigit() check against it, with the added benefit that if the user puts in a number > INT_MAX, it will still handle the exception by printing an error message end exiting with a failure. Trying to do pure input validation seems like it would make for less elegant code for the most part.
@dnull Жыл бұрын
can you also make a vid on AUTOSAR? i think it's a pretty interesting case study of a more modern approach on safe use of c++ in a critical infrastructure development
@chrisbradley32247 ай бұрын
Autosar is trash. Same with Rational Rhapsody and all those similarly horrible tools the Germans like to torture themselves with. It’s a utopian approach that ends up giving you a bad product that took 2-5x as long.
@realms42198 ай бұрын
Tabs vs spaces is a non-fight. Use tabs with the correct indentation(read the project guidelines), let your IDE replace them with spaces. Do no check in tabs in your files and amend the commit if you did.
@Kknewkles Жыл бұрын
Ah yes, the Google code, proof even against the future, unless it's the future where they discontinue the project
@anon1963 Жыл бұрын
i don't think they discontinue it because of bad code
@_____case Жыл бұрын
Discontinuing projects is necessary sometimes. Have you ever worked at a company that supported a product for way longer than they should have?
@Syndiate__5 ай бұрын
@@anon1963 I don't think that's what the point of the original comment was. I think he was saying that writing code for whatever particular application is designed to be future proof, to last a *long time even into the future*. However, if the project is discontinued, like the original comment said, then it can be said that the code was not future proof since it was cut short by the overall project's discontinuation.
@anon19635 ай бұрын
@@Syndiate__ future proofing code = making it fairly easy to make changes and expand on it, cutting projects has NOTHING to do with future proof code. hence my comment
@Syndiate__5 ай бұрын
@@anon1963 But that's what the comment might've meant I believe, at least it makes a bit more sense to interpret it that way
@alphabasic17597 ай бұрын
If you can configure your editor to insert spaces when a tab is pressed you can also configure how much a tab indents.
@HoloTheDrunk Жыл бұрын
I love that the solution to OOP's problems always ends up being to avoid using OOP.
@Liam_The_Great Жыл бұрын
No. Interface inheritance is still OOP
@officialraylong Жыл бұрын
I'm not sure how you derived "avoid using OOP" from that video. Composition with objects and interface inheritance for different classes are OOP techniques.
@Liam_The_Great Жыл бұрын
@@officialraylong The "OOP is useless" crowd are not known for their critical thinking skills
@MoolsDogTwoOfficial Жыл бұрын
@@Liam_The_GreatOOP for me is the best thing since sliced bread.
@chief-long-john9 ай бұрын
Also semantic issue but abstract class are actually for the sake of implementation inheritance and actual interfaces are for the case of composition
@anon_y_mousse Жыл бұрын
I should probably read that style guide because a lot of these things I already do in my own code. Two spaces for each indentation level and I've set my tab key to insert four. Plus, since vim has dedicated keys for it I can increase or decrease indentation levels with ease. Need to push right 3 levels, 3> and done. Anyone that thinks tabs are superior should read that guide.
@sqlexp Жыл бұрын
That guide is trash.
@LowLevelTV Жыл бұрын
Sign up for my NEW weekly newsletter on software development and cybersecurity industry trends! bit.ly/3OaLCVb
@Parker8752 Жыл бұрын
I tend to use spaces out of habit, but tabs are better for accessibility (blind programmers often use braille displays, which only offer 40 characters per line), and with the exception of alignment, it doesn't really matter if the code looks identical. In the case of alignment, you just use spaces after the tabs. 2 spaces is probably better for alignment than 4 in the case of braille displays (though a tab is still better as it's one character vs two), but then people with poor vision due to age or the like may find that such narrow indentation is difficult to read by sight. In such a case, the fact that tabs are of inconsistent width is actually a positive.
@natansandle9284 Жыл бұрын
As others have already said, the "Mother-Father-Child" example is very misleading, and imo so is the "Car-Engine" one. A car is something that "has" an engine (association), not something that "is" an engine (inheritance). Thus nobody would have wanted to make Car a subclass of Engine to begin with. This is actually the same problem as the "Mother-Father-Child" example, it's just less noticeable cause the implementation we're shown is the correct one.
@vladomaimun Жыл бұрын
1:20 That's the whole point of the TAB character - it lets you adjust the visual amount of indentation to your liking without modifying the code. The fact that the code can look differently when viewed in editors with different configurations is not an issue but a feature. The rest of the rules are perfectly reasonable.
@afelias Жыл бұрын
Yeah but that's exactly why the TAB or \t special character is so bad in text editors - it's not monospace. It takes up some unknown integer amount of monospace blocks. The same way you don't want to write code with crazy Unicode characters in naming conventions (and that's if the language even allows them), you don't want TABs themselves. You just want the utility TABs provide, hence the spaces.
@vladomaimun Жыл бұрын
@@afelias As you said, tabs take an integer number of monospace positions. Therefore they don't mess up monospace fonts - characters typed after tabs are still aligned to the monospaced grid. I absolutely do want tabs themselfs because they provide better utility than spaces.
@Alx-gj2uz17 күн бұрын
The type deduction sample is wrong no? specifies a template/generic not the return type?
@sorek__ Жыл бұрын
About inheritance issue with diamond example I recently found great workaround which is class Bar : virtual public Foo {} which makes Foo class only being inherited once no matter how many classes its derived by. Its really awesome and powerful.
@tornado-zex47 Жыл бұрын
for inhiretance problem it called the daimond problem , and u can fix it easily by adding virtual key word , like ( class mother: virtual parent | class father: virtual parent )
@jhgvvetyjj6589 Жыл бұрын
How to write future-proof C++ for Win32: compile with Digital Mars to support Windows 95-11, by the time x86 emulation becomes popular, users will be able to emulate Windows 95 or Windows NT 4.0 without taking excessive resources to emulate newer versions!
@profoundgames_21 күн бұрын
Also, spaces show up correctly in a web UI, where tabs could look different across UIs
@Xeverous Жыл бұрын
1:26 the difference in tab length is noted as a benefit by tab fans. People have different preferences for indentiation and tabs allow them to use it freely. If tabs are used for indent and spaces for alignment code which should line up does line up. 2:24 a) to be precise, it's a function template, not a function b) it doesn't return Foo but std::unique_ptr 2:50 the auto issue is controversial - a lof of editors will support rich code information and display what auto deduces too. Looking at the committee, opinions differ widely. IMO worth to mention. 4:00 I know it's an artificial example but in an education video you should do some effort and write a standard exception class caught by const reference. 5:00 a) missing semicolon at the end of class definition b) mixed namespace use: std::string but not std::uint32_t Also please note that Google's Style guide has been criticized by C++ creators. A lot of C++ Core Guidelines conflict with Google's guide. Google's guide is aimed mainly at Google's legacy code. Core Guidelines recomment NOT TO use it.
@اشکانمحمدی-ز1ث Жыл бұрын
Man, honestly, you catch errors better than my compiler does 😂
@Xeverous Жыл бұрын
@@اشکانمحمدی-ز1ث nah, this channel is simply of low quality
@isodoublet Жыл бұрын
" If tabs are used for indent and spaces for alignment code which should line up does line up." Or, you can just use the one character that will always work and always be correct. I have never seen tabbed code that wasn't messed up. Not once. It's impossible to maintain invisible characters.
@Xeverous Жыл бұрын
@@isodoublet I frequently see pure-space code that also isn't aligned. Some people will fail at both no matter what.
@isodoublet Жыл бұрын
@@Xeverous Everyone fails at aligning tabs, so that's no excuse
@Epinardscaramel Жыл бұрын
1:16 isn’t that a good thing? Every one can set the tab width to their preference?
@AlLiberali Жыл бұрын
If you can configure your editor to write 2 spaces when you press tab, surely you can also configure how wide actual HORIZONTAL TABULATION characters are.
@randolphbusch7777 Жыл бұрын
The whole point of tabs is that you can configure how far the indentation is for yourself and that someone else DOES NOT get to dictate how it looks for you.
@vaijns Жыл бұрын
yeah, that's what people arguing for tabs say. I also prefer tabs. But you also gotta see the point of consistency that spaces have. If you're using tabs and have them configured to be as wide as 4 spaces e.g. you might add more of them to make the code look good on your end. But then someone else who has the width of tab (character) configured as 2 spaces has a completely different looking piece of code. For normal indentation that might be fine but if you have a long function signature e.g. that you wanna break down to multiple lines, you might wanna line it up with the opening parentheses and where those are differs depending on your tabs setting. say you have this function: int doSomeStuff(int (*callback)(int*, int*), int* a, int* b){ // do something... return callback(a, b); } might not be the best way to format your code but in that case the width of your tabs matters. With spaces the two lines of your function signature will always be lining up.
@AlLiberali Жыл бұрын
@@vaijns I won't do that tho. When I go a level deeper in indentation I push tab once and only once. The argument list already has its own brackets; It doesn't need to be aligned.
@vaijns Жыл бұрын
@@AlLiberali So do I. But that's why someone might argue for spaces and why both opinions are valid, depending on how you format your code.
@ghosthunter0950 Жыл бұрын
@@vaijns Honestly I don't think it matters. The issues of both can be solved by defining code formatting for the project. You just have to choose one and stick with it. For example for functions with a lot of parameters you define going down a line and tabbing once.
@R00M4D6 ай бұрын
Why did their golang choose tabs for gofmt? Seems inconsistent
@SasaraSara266 Жыл бұрын
I think tabs are better than spaces due to one specific reason only: accessibility. People with problems with their vision usually have different ways to make code readable. Some people would blow up the font size and reduce tab width, some people would increase tab width instead. If spaces were used instead of tabs, those people would have to manually convert the spaces to tabs so they can customise the tab width, then change the tabs back to spaces afterwards.
@gagagero Жыл бұрын
Yeah, I don't really get this "looks the same" point. Who cares if it looks the same, if it has the same meaning?
@kkiimm009 Жыл бұрын
Sounds like a job for the editor.
@DaddyFrosty Жыл бұрын
@@gagagero yeah if all code uses tabs everything will look the same according to your settings. Actually most retarded decision by google
@CosmicRadishes Жыл бұрын
I think it's getting better over time. Older generation is still using some weird *notepad like* IDEs because of habit. But in my circle young devs are working with more modern tools like VSC.
@gagagero Жыл бұрын
@@CosmicRadishes How does that relate to anything?
@perfectionbox Жыл бұрын
"Spaces only, and two spaces per indent. Now, we will be... hey, where are you going?"
@Henrix1998 Жыл бұрын
Good old spaces vs tabs. The only situation I dont like spaces is when one arrow key press doesnt move one full indentation or backspace doesnt remove full indentation.
@nilau8463 Жыл бұрын
Just hold ctrl and boom
@khodok9636 Жыл бұрын
@@nilau8463 that's what I tell everyone who say spaces are annoying... How does that change anything for you when using full word directional arrows movements?
@orterves10 ай бұрын
4:23 the fact that in languages like C#, you can have exception free code everywhere but still have to constantly be aware of exceptions coming from any dependencies - notably, the standard libraries - is exhausting and a major detriment to the language. People deride checked exceptions - but I honestly wish they were a thing in C#, forcing the developer to realise and manage the true impact exceptions have on the code, instead of being blind to it
@Sibearian_ Жыл бұрын
Hi
@fuery. Жыл бұрын
Hello
@scorcism. Жыл бұрын
@@fuery. hello hi
@LowLevelTV Жыл бұрын
uwu
@fuery. Жыл бұрын
@@LowLevelTV youtube: yes this perfectly translates to "this", such logic
@nebularzz Жыл бұрын
hi
@ChocolateMilkCultLeader Жыл бұрын
Do more like these. These are great
@LukeVilent Жыл бұрын
You can still write a spaghetti code by using composition. I've had to work with the other's code, where each next class contained one and only instance of a previous one as a private field, and not much beyond that.
@bestnocture8 ай бұрын
It really is impossible to not write spaghetti code no matter what guidelines one uses.
@LukeVilent8 ай бұрын
@@bestnocture I'm afraid don't fully get the wording. Did I understand you correctly, that any good guideline can be abused - intentionally or not - in such a way that it's gonna lead to a spaghetti code?
@gnarfgnarf40047 ай бұрын
I agree with all of this, except the spaces vs. tabs. How hard is it to configure each person's editor so that a 'tab' will indent 2 or 4? This way everybody gets what they want. Indent of 2 is too tight and confusing.
@sudokode Жыл бұрын
Very informative video. I wasn't sure what to expect, but I'm glad I clicked. The tab/space debate will rage on, but I like Google's take on it. That's not one I've heard before because, well, I've never been on a giant team like that where consistency is the key to not losing millions in a day. That's a fun example you chose for the classes (Mother, Father, child), but in my experience, it's best to avoid the noun/verb model and just do what works best. Trying to line up everything philosophically as classes just introduces logical issues like this. In that example, you'd probably be better off defining Person, Parent, Child, and Family classes. Since children in real life don't inherit everything evenly and directly from both parents, it doesn't make any sense to define their classes like that. Rather you should define a Person that Parent and Child will inherit from, and then you can define a Family which takes two parents and a list of children as arguments. If you really wanna get inclusive, just take in a list of parents and children lol. But your point is valid, inheritance can be non-intuitive and should be handled with caution.
@spell105 Жыл бұрын
Or, alternatively: all people are people. You don't need a 'mother' and 'father' class, or a 'child' class. This kind of metadata belongs to a family; a family is not a person, but a lot of people in a specific hierarchy of parent -> child. That sounds like a data structure to me.
@trapOrdoom Жыл бұрын
Why are you so good at explaining? I wish you were my prof.
@m4rch3n1ng Жыл бұрын
tabs vs spaces should focus on one thing, and one thing only: accessibilty. using spaces (especially small amounts of spaces like 2) over tabs forces people with vision impairment that need a larger tab-width to properly be able to code, to either reformat the entire code that they are working on (twice), not contribute to that project at all or just live with it and suffer, which i think is unacceptable.
@heavymetalmixer91 Жыл бұрын
2 spaces has never been enough for me, so I go with 4 instead like in Python, so I set my editor to introduce the 4 spaces everytime I press Tab.
@m4rch3n1ng Жыл бұрын
@@heavymetalmixer91 yeah but what if someone needs to use 8 space width to properly code? or someone has a text size so large they have to use 2 spaces because otherwise the code won't fit in the width? if you use tabs and set your editor to display the tabs as 4 spaces, then these two can set their own editor to display 8 spaces or 2 spaces respectively, and all of you can code with your own preferences. it's like forcing someone to use a specific font: don't
@PinakiGupta82Appu Жыл бұрын
@@m4rch3n1ng hit the tab to insert 4 spaces. That's what he said, as much as I understood. Tabs are not allowed in many places. A code formatter will solve the problem you mentioned. Tabs characters will be converted to spaces, anyways.
@m4rch3n1ng Жыл бұрын
@@PinakiGupta82Appu yes but you should hit the tab key to insert a tab character, so other people can configure how code looks to them. i know about the convenience, but forcing someone else to change your formatter and reformat your code, or worse, manually format your code (as is the case in the majority of javascript repos) to be able to code for literally no benefit (other than the benefit of forcing your style preferences on someone else) is extremely unnecessary and inaccessible for people who need it edit for clarity: this is specifically about open source projects or other types of projects where multiple people work on the same code. i don't care what you do on your own disk, but you should at least think about other people when opening up your code for everyone else, especially if you are a company that tries to pride itself on "inclusivity"
@PinakiGupta82Appu Жыл бұрын
@@m4rch3n1ng They will probably give you an Artistic Styler or a Clang-Format script. You'll have to run it to comply with the standard before committing the changes to git, even if the code looks bad according to someone else's preferences. Different people may have different preferences. It's not always possible to force them to accept what I want. Tabs are not allowed in many places, even if it solves the readability problem.
@ov3rcl0cked7 ай бұрын
I follow this inheritance rule in pretty much every language. Unless it's a clear cut case where inheritance makes the most sense, in terms of sharing a lot of code, I'll opt for interfaces. I'll also prevent multiple layers of inheritance. I feel like the further you come from the base class then more confusing and implicit things can become. You end up not really knowing what the class is composed of. I instead will often look to encapsulation, and usually dependency injecting these encapsulated pieces, because it's also astonishingly easier to test. I have no strict rule around this, but I feel like if you're 5 layers of inheritance deep you're usually shooting yourself in the foot. I feel like many developers stray away from encapsulation when they can use inheritance, like one is somehow intrinsically better than the other. A trick I've come to like when things start to get complicated with inheritance is to create an interface that defines all common methods, in C++ this would be a purely abstract class, and then have a base class for any common pieces to inherit. So in the case of a mother, father, and child, the parents might have their own base class which implements the interface, and child would just implement the interface, and maybe child could encapsulate a reference to the parents. This way any similarities in implementation the child shares with either parent is also specific to the instance of their parents. Child could even have it's own base class and you could create daughter and son, where maybe they each emphasize the characteristics of the parent who has the same sex as them, given this isn't how it actually works but serves the analogy. I really see inheritance as mechanism to not repeat yourself, but I often see people trying to think of it too literally. Like the example mother, father, child, the child obviously literally inherits the mother and father, but you're kind of abusing the programming term "inheritance" to mean something it really doesn't. The child CLASS doesn't inherit traits from the parent CLASSES, it inherits PROPERTIES from a given INSTANCE of it's parents. Java doesn't allow multiple inheritance, and I honestly feel like that's prevented a lot of mishaps. On the other hand, I've dealt with inheritance nightmares in Pythons. I've seen a lot of people who abuse inheritance. We had a network protocol that had a major change between version 1 and 2, and the original developer implemented a base class to handle all the lower level networking pieces, many of which were common among version 1 and 2, and then inherited that into the v1 client. That's all well and good, but the real mind bender is the fact that they inherited the v1 client into the v2 client. Wild. It was so hard figuring out what happened where, and why these 2 major versions had overlapping code bases. It was a nightmare to pull that apart. This guy actually now works at Google, though I'm sure they've taught him a few lessons.
@_____case Жыл бұрын
Really curious to see if Carbon will be designed with a bias towards Google's style guide. Are there C++ features for which they won't build compatible functionality in Carbon, or will they aim to be 100% compatible with the entire C++ language?
@MarcoAntoniotti8 ай бұрын
You can configure The Editor to do TRT when you hit tab. Hence not vim or other stuff. 😊
@shooting4star20235 ай бұрын
Many thanks for the video. I was educated in the 1980’s in Taiwan with Pascal and 1990’s in the US. As an engineering manager for most of my career, I’ve navigated through the minefields with engineers on (2) spaces vs tab and much more on exception. Can you make this a series?
@fuery. Жыл бұрын
My code is in no way future-proof, it is overly compact and simultaneously not, it is the embodiment of spaghetti code and I myself have a hard time reading it at times. This is a mistake. Glad Google isn't making it. Edit: dang didn't expect the replies to be an argument on the readability of code relating to how easy or hard the code was to write lmao
@pierelenigus8598 Жыл бұрын
If it was hard to write it should be hard to read.
@fuery. Жыл бұрын
@@pierelenigus8598 I mean you have a point
@Henrix1998 Жыл бұрын
@@pierelenigus8598 No, not really. Hard ideas can be expressed in a simple way in most cases.
@DajesOfficial Жыл бұрын
@@pierelenigus8598 it should not if it's a finished work. After you wrote a working implementation it is messy indeed, but then you usually can optimize readability by a lot without chaning the logic. The code is finished not when there is nothing to add but when there is nothing to remove.
@pierelenigus8598 Жыл бұрын
@@DajesOfficial Well if you're not busy and in demand problem solving solution provider I reckon one would have time for all that. As far as I know the real world a working solution is a completed solution.
@Mercury13kiev20 күн бұрын
I checked one of their replacement libraries for STL, and… it has no layers when simple parts are used by complex parts. To pull one feature, you need to pull the entire library. Strange, people working at Google are ways more clever than I.
@MikkoRantalainen Жыл бұрын
I think that if you don't use RAII and exceptions, you shouldn't bother with C++ at all. Just use modern C. Yes, that requires writing wrapper code for all incorrectly (that is, without RAII and exceptions) written C++ code.
@anonymousnearseattle2788 Жыл бұрын
Some of us still enjoy having access to classes, virtual methods, and templates. All of my C++ projects have exception frame generation disabled.
@valizeth4073 Жыл бұрын
Modern C is an oxymoron.
@MikkoRantalainen Жыл бұрын
@@valizeth4073 C23 standard is being specified *this year* - what do you consider "modern"?
@christianknuchel Жыл бұрын
Each environment has its requirements, and with that, opinions on how to write code are different as well. I think it's a good idea not to get wrapped up too much into any one of them, lest one becomes prone to shoehorning paradigms into environments where they're not a good fit.
@kadenhansen6 ай бұрын
I don't even know C++, but I've used these ideas elsewhere in languages I do know. Thanks for making this video!
@NewLondonMarshall Жыл бұрын
Why is there an argument about tabs vs spaces? A tab can be converted to spaces, and it is a single key press. It can be converted in your editor to match any number of spaces. On the other hand, manually pressing the space bar for 2 or 4 spaces has issues if you don't tap the right number of times, it is harder to do and more error prone. And if somebody else sees the code in their editor, they might not be able to change the spacing to match their preferences. I can't believe this argument is still happening. Tabs should win outright with no competition. Oh and also tabs are smaller in file size. Need more persuading?
@vaijns Жыл бұрын
tabs vs spaces is not about pressing the tab key on your keyboard or the space key (it's always pressing tab). It's just about what is inserted when pressing tab. (mostly) all editors give you an option to define how many spaces should be inserted by the press of tab (or no spaces but stick with tabs). While tabs allow every user to define the width of their indentation themselves, spaces force the width which helps with consistency (depending on how wide your tabs are you might add more or less of them in your code, which could look weird on someone elses device).
@BlueEyedSexyPants Жыл бұрын
Absolute horseshit. No one who uses spaces ever presses the space bar to insert their spaces. They let their editor insert 2-4 spaces when they do their "single key press" of the tab key. You don't have to press it the right number of times. The workflow is exactly the same. The problem is when tab people try to use their damn tab characters in other places than the beginning of the line, they fuck up everyone else's code alignment if the settings are not exactly the same between editors. Tab people end up with alignments like: const int short\t\t\t= 1; const int longvariablename\t= 2; And it aligns perfectly on their tabwidth, so they didn't think that it wouldn't work on anyone else's. If the advantage is that everyone can set their own tabwidth, why do they introduce code that misaligns with different tabwidths? Another example is for function signatures where the parameters are on different lines. Spaces is the only way to align them with the start of the first param. If tab people used editors that could put tabs at the beginning of a line and spaces in the middle, that might be okay, but they never do. They litter the code base with awful tabs in the middle of lines. I can't believe this argument is still happening. Need more persuading?
@vaijns Жыл бұрын
@@BlueEyedSexyPants I think as long as you're consistent within a codebase both are fine. I for example prefer tabs to have my indentation just the width I want. And I don't use any tabs for aligning assignments and for function signatures I just add one tab to the horizontal position where the signature started (usually the return type). I agree that lining up with tabs doesn't make sense. But it comes down to how you format your code if it is a problem or not. As I said, just keep it consistent within the codebase. If I'm working on someone elses codebase, surely I don't force my tabs but I also try to format my code based on what's already there (like, are function signatures aligned at the same horizontal position or not, ...). A lot of code I'm working with uses 4 spaces and I'm not a huge fan of it as I think that's too wide. But that's not my choice to make if it's not my codebase.
@BlueEyedSexyPants Жыл бұрын
@@vaijns Yeah, if everyone in a codebase is perfectly consistent, that's fine. So if the codebase wants you to set your tabwidth to 8 but you prefer 2, you either need to set it something you don't enjoy, losing the supposed advantage of tabs completely, or you need remember every time you insert your 2-space tab that everyone else will see it 4 times larger. (8 is insane, of course, but just for the purposes of illustration.)
@9SMTM6 Жыл бұрын
@@BlueEyedSexyPants that is a good argument with mixed usage in situations other than code indenting. Ultimately I've just accepted the situation as is. However sometimes it's very much annoying, eg. when I write documentation with TeX and similar. You can include code directly from the source file, which is my preferred way as it avoids me forgetting to update code after changes. But if you used 4 space indenting (as is the default in most Code bases and also most formatters), this makes most code hard to include, as that quickly gets too long with little indentation. Similar thing when I open some code in a small window /on a small screen.
@berkan19232 ай бұрын
thanks for the video, what about reflection, do they use it in google projects offen??
@sebastianfia9541 Жыл бұрын
It's funny how half of the things are just "do it like in rust" lol (e.g. having only one level of inheritance and only with abstract classes as parents is basically rust traits)
@theultimateevil3430 Жыл бұрын
Rust is made with a 40 years of history of writing terrible code in C++. Makes sense they dropped some concepts that have been proven to be extremely bad. You could see the same in earlier languages, for example, C# and Java removed the multiple inheritance and encouraged the use of interfaces. All mainstream languages (incl. C++) play with the functional programming. The most modern languages (Go, Rust, Zig) drop the exceptions in favor of C-style error handling with syntactic sugar. This is basically a trial-and-error on the scale of generations of programmers, and while it's painfully slow, it's a steady progress towards the perfect programming language in which it's harder to write bad code. Because if it's possible to write shit, it will be done, and very fast. Javascript programmers cried in tears with jQuery until came the frameworks that enforce their architecture on you (React, Vue, Express, Nest, etc.)
@sebastianfia9541 Жыл бұрын
@@theultimateevil3430 Yes I totally agree with your analysis
@Slowdive-ue9sn Жыл бұрын
I really liked this summary, well done👍
@olafbaeyens8955 Жыл бұрын
I completely agree with Exceptions, they should only be used in extreme rare exceptions and never replace an error handling. When an exception gets through then you as a developer have made a very bad design.
@nothingisreal6345 Жыл бұрын
Container.GetElementByID. What do you reasonably return when the container does not contain a matching element? Null -> null pointer access (exception). Default element - what should that even be? A tuple (success, element)? Callers will forget to evaluate success and element still needs to be a nullable pointer. Unions are the only thing that comes to mind. But then the interface of the method MUST list all possible result type and the error cases (which are an implementation detail) get exposed. This will hinder changing the internal implementation. Exception decouple the implementation details from the caller. A call must expect the unexpected. It get's even worth for Class.DoSomething(); A call expects something to be done But what to do when it simply can't be done? What if DoSomething() has to call other methods and those fail? Simply go back to learning exceptions...
@ColinGrealy Жыл бұрын
Container.GetElementByID is a textbook example of when *not* to use exceptions. The function itself has no possible way of controlling what ID is passed into it. By definition, an element with an arbitrary ID not being found is not exceptional (you could argue that for the set of all possible IDs, only a tiny percentage are probably in the container). So yes, return some value (tuple, union, end()) for element not found and then slap your clients for not checking the return value. That said, imagine your container is somehow broken. Let’s say it wraps an array pointer or a linked list. If the internal state of the container is invalid, THAT is an exceptional circumstance.
@anonimowelwiatko9811 Жыл бұрын
Praise the algorithm. I am going to write coding specification for my project tomorrow and this reminded me of some thing I should probably include.
@hoopengo2289 Жыл бұрын
two spaces.. bruh..
@quezip8 ай бұрын
I do dat I don't like tabs
@Lighter7900music7 ай бұрын
@@quezip tabs make your files smaller
@quezip7 ай бұрын
@@Lighter7900music and..? I'm not here with like 2 mb of space
@-Cocell7 ай бұрын
@@quezip Same, spaces makes it much more easier and read-able.
@tongpoo89857 ай бұрын
🤮
@Zipperheaddttl Жыл бұрын
Any chance of a video on this cool library called Raylibs? I think you would really like it.
@torarinvik4920 Жыл бұрын
I agree on all these things, inheritance thought I feel that it really depends on what your making. In a video game or a GUI framework there is a naturally occurring hierarchical relationship. So I believe in the "is a, has a, and uses a" rules. Also a huge fan of the Gof book. Great video! If you really want safe code, you need to use a FP language like F#.
@nankinink Жыл бұрын
Even in game development composition is better than inheritance. GUIs can also be done by composition. I follow the rule that if your class has more than 1 layer of inheritance, you are doing it wrong.
@torarinvik4920 Жыл бұрын
@@nankininkThat is a good rule to follow as guideline. The benefit and danger of inheritance is that you can modify millions of classes by just modifying the parent class. So for instance if you want to change the default speed for enemies or default size for a window you can do that in just one place. Also imagine having a biology simulator and this program having perhaps over 10,000 different species or more then you can save an enormous amount of code by using inheritance, if you have many types that differs slightly from each other. I don't use inheritance much myself but it certainly has it's niche use cases.
@nick15684 Жыл бұрын
I would generally prefer composition in the case of a game or a GUI whenever possible. Create "foundational" classes that compose the entire structure. Keep inheritance as shallow as possible and prefer abstract classes.
@torarinvik4920 Жыл бұрын
@@nick15684 Yeah, if possible then do it!
@Dennis1990111 ай бұрын
@@nick15684 An abstract class is still inheritance. Especially if it's not a pure abstract class.
@rubenverg Жыл бұрын
The space argument is dumb. The point of using tabs is *exactly* that you can decide how large the indentation is, which is a) important for accessibility (I know more than one person who requires at least four-char wide indentation to easily understand the nesting of the code they read) and b) means that you can always keep the same indentation on every project, and your brain gets used to knowing that an indent is however many characters wide.
@anon_y_mousse Жыл бұрын
The biggest problem with tabs is that different people "require" different sizes for their tabs. Indentation is alignment and changing alignment causes problems. I don't buy the accessibility argument, especially since every editor can generate visual cues to indicate whitespace, but that you said "easily understand" makes me think it's not even a valid accessibility issue at all, but rather a laziness of reading issue. Although, truthfully if someone can't understand the code when they read it, I wouldn't want them on any team that I manage because they'd be a hindrance to development.
@isodoublet Жыл бұрын
"The point of using tabs is exactly that you can decide how large the indentation is," Everyone knows what the point is. You're just wrong.
@rubenverg Жыл бұрын
@@isodoublet the video states that it is a disadvantage
@isodoublet Жыл бұрын
@@rubenverg Don't think it does but that's irrelevant, you're still wrong.
@rubenverg Жыл бұрын
@@isodoublet explain why
@mowinckel10 Жыл бұрын
A fun thing, this is how google does INTERNAL things. Here they care about developers. They FORCE you to break a lot of these if you use their IDE's. Because Google does not care about developers that are not working for them.
@eniky Жыл бұрын
What is the diamond thing? I'm learning C# and it just doesn't allow to inherit from more than 1 class (either abstract or not). Instead we should compose classes or implement dozens of interfaces. Which is greate, code is always clean.
@Klayperson Жыл бұрын
spaces are inaccessible. some people's eyes or visual cortex just doesn't read the code easily unless they can set their indentation to something crazy. with tabs, that's trivial, and editor-specific; it doesn't bother other people if my IDE shows indentations at 8 characters. with spaces, google is forcing everyone to have the same experience, whether it accommodates their visual processing abilities or not. not to mention that they mandate TWO spaces, which is bad because it inherently encourages deep nesting.
@igorordecha Жыл бұрын
Yes! "It looks different for different people/editors" THAT'S. THE. FUCKING. POINT. If I want my tabs to be 3.141592653589 spaces wide because [reasons] that's none of your business. What's next? Storing code as PDFs to preserve the company approved font, size and color? I would even say this is straight discrimination of visually impaired people and it's disgusting.
@jtfoog5220 Жыл бұрын
Lmfao 🤣
@AnarchistEagle Жыл бұрын
I've worked at companies that used tabs and companies that used spaces. The companies that used tabs gave me a much harder time reading code because I would often have to manually change my tab width to match whatever the original author used. Which is a problem in a file touched by a lot of devs. This is a largely a problem with people not understanding the difference between indentation and alignment.
@isodoublet Жыл бұрын
"some people's eyes or visual cortex just doesn't read the code easily unless they can set their indentation to something crazy." Such strawmen can pick a different career. It doesn't justify having the code formatting suck for everyone, sorry.
@isodoublet Жыл бұрын
@@igorordecha "THAT'S. THE. POINT. " Everyone knows what the point is. You're just wrong.
@Kvltklassik7 ай бұрын
I went to click on this video only to find the left side of the thumbnail actually isn't a link. Then that got me thinking, isn't google that site where everything has been getting worse, and worse, and worse, and more broken, and more invasive, and worse, and worse, over the past decade? Maybe I don't want to "CODE LIKE GOOGLE".
@JuusoAlasuutari Жыл бұрын
Google using spaces is just giving up. It's not difficult to indent with tab but align with space only.
@isodoublet Жыл бұрын
It's not only difficult, it's impossible.
@JuusoAlasuutari Жыл бұрын
@@isodoublet skill issue
@isodoublet Жыл бұрын
@@JuusoAlasuutari I mean only the tab side consistently messes up formatting so ¯\_(ツ)_/¯
@JuusoAlasuutari Жыл бұрын
@@isodoublet the ones who mess up formatting in tab-indented code are people who keep mixing spaces into indentation because they can't understand the difference. Sounds like something you might do.
@isodoublet Жыл бұрын
@@JuusoAlasuutari "he ones who mess up formatting in tab-indented code are people who keep mixing spaces into indentation because they can't understand the difference." And that's all of them. "Sounds like something you might do." Nope, I use 100% spaces so the error is fundamentally impossible for me.
@darkmagic5438 ай бұрын
My opinions: Tabs vs spaces: Disagree, it is just as easy to configure tab size in your editor. (also I find code with only 2 spaces hard to read) Type deduction: Agree Ownership: Mostly agree, depends on the kind of project Exceptions: Somewhat disagree, exceptions are fine if they are documented and used only in exceptional cases Inheritance: Disagree, diamond problem is not a real problem since virtual inheritance exists
@simple_classic Жыл бұрын
Basically Rust. Now I get it
@larrybud Жыл бұрын
The list is already invalid if you're using spaces. The reason tabs are correct is so that it can be up to the individual dev on how he wants it to look on his screen. Want it to look like 2 spaces? You can do that. Want it to look like 4 spaces? You can do that too.
@Henrik0x7F Жыл бұрын
With tabs you can't align code unless you mix them with spaces. At that point just use spaces
@larrybud Жыл бұрын
@@Henrik0x7F I'm aligning code on a tab stop.
@Henrik0x7F Жыл бұрын
@Slukke Yes, that's the reason I use spaces
@qutuz9495 Жыл бұрын
The point of tabs is to look different based each engineer's config. Some people may like 4 spaces, others 8, some 2. You can't have that customization with spaces.
@nochan99 Жыл бұрын
Today I learned that I arrived at google's guidelines independently (except for spaces vs tabs. Tabs til I die).
@norazoe4359 Жыл бұрын
“Tab width might be different from one editor to another” that’s the point of them!!!! 😭 I hate it when other engineer’s tell me what my indent should be, write better code that doesn’t rely on horizontal alignment to actually read it. Maybe someday source code will just be ASTs and I’ll be able to have the style I actually want.
@CartoType Жыл бұрын
A good way to make code readable is to line up opening and closing braces. Microsoft does that, but most other companies don't, for reasons of fashion rather than logic. Even better, use Whitesmiths style; but Microsoft style (braces line up and statements are indented relative to them) is okay.
@Fudmottin9 ай бұрын
I don't get the hate for exception handling. If you're going to throw an exception, you can include information on where it was thrown from in addition to why. Also, provided you are using RAII properly, cleanup of resources will happen.
@gnikdroy5 ай бұрын
Because something like an std::optional or std::expected is very explicit. With exceptions, you have to rely on documentation for what can throw and what kind of exceptions it can throw. Documentation can become stale, but with std::expected types compiler and type checkers can verify everything. That being said, using exceptions properly will very much yield the same results. But you have to make sure you document everything. With std::expected types it is impossible to "miss" stuff.
@Fudmottin5 ай бұрын
@@gnikdroy I see. Improvements have been made. Good to know.
@dsuess Жыл бұрын
100% agree with the first rule. "Use 2 spaces" I can begin my weekend now 😎
@darkfoxwillie Жыл бұрын
jajaja loved that ending. Good luck bro!
@2005kpboy Жыл бұрын
C++ is plain awesome.
@weeb3277 Жыл бұрын
would it kill ya to add the link to the style guide? would save me some time googling it ;)
@RadenVijaya Жыл бұрын
Actually what Google did is already taught in most University... But I still use 4 spaces indentation. Because I feel the code reads better with 4.