CppCon 2017: Kate Gregory “10 Core Guidelines You Need to Start Using Now”

Sdílet
Vložit
  • čas přidán 16. 10. 2017
  • CppCon.org
    -
    Presentation Slides, PDFs, Source Code and other presenter materials are available at: github.com/CppCon/CppCon2017
    -
    The C++ Core Guidelines were announced at CppCon 2015, yet some developers have still never heard of them. It's time to see what they have to offer for you, no matter how much C++ experience you have. You don't need to read and learn the whole thing: in this talk I am pulling out some highlights of the Guidelines to show you why you should be using these selected guidelines. For each one I'll show some examples, and discuss the benefit of adopting them for new code or going back into old code to make a change.
    Beginners who find the sheer size of the language and library daunting should be able to rely on the Guidelines to help make sane choices when there are many ways to do things. Experienced C++ developers may need to leave some of their habits behind. Developers along this spectrum could benefit from seeing what the Guidelines have to offer, yet the guidelines themselves are just too big to absorb all at once. My examples will be chosen to be beginner-friendly and the focus will be on what's in it for you: faster code, less bugs, and other tangible benefits.
    -
    Kate Gregory: Gregory Consulting, Partner
    Kate Gregory has been using C++ since before Microsoft had a C++ compiler. She writes, mentors, codes, and leads projects, in both C++ and .NET, especially for Windows. Kate is a Microsoft Regional Director, a Visual C++ MVP, and has written over a dozen books (the most recent on C++ AMP for Microsoft Press) and speaks at conferences and user groups around the world. Kate develops courses on C++, Visual Studio, and Windows programming for Pluralsight, founded the East of Toronto .NET Users group, and is a member of adjunct faculty at Trent University in Peterborough.Website: www.gregcons.comTwitter handle: @gregcons
    -
    Videos Filmed & Edited by Bash Films: www.BashFilms.com
    *-----*
    Register Now For CppCon 2022: cppcon.org/registration/
    *-----*

Komentáře • 103

  • @robbie_
    @robbie_ Před 2 lety +36

    Kate Gregory speaks to me as an actual coal-face software developer, not some lofty academic on a standards committee. I find her talks very useful in the real world.

  • @lemondrop887
    @lemondrop887 Před 6 lety +179

    static_cast: the "it's MY gun and I'm pointing it at MY foot" operator. incredible

    • @TanzinulIslam
      @TanzinulIslam Před 6 lety +46

      I suppose reinterpret_cast would be the "it's MY gun and I'm pointing it at MY head" operator. :D

    • @dusparr
      @dusparr Před 5 lety +11

      TO VOID AND BACK AGAIN!

    • @robrick9361
      @robrick9361 Před 8 měsíci +2

      @@TanzinulIslam const_cast is the "it's your gun and I'm pointing it at your family" operator

  • @Danielle_1234
    @Danielle_1234 Před 6 lety +136

    Another great talk by Kate Gregory. She does excellent 101 and 102 talks. Her how to teach C++11 talk (titled: CppCon 2015: Kate Gregory “Stop Teaching C") is one of the better explanations of how C++ can become an easier language than C, by explaining the thought process behind abstraction a bit.

    • @theicebeardk
      @theicebeardk Před 6 lety +9

      Yes, her C++11 talk changed the way I approached teaching and mentoring C++ entirely and with some success.

    • @cepi24
      @cepi24 Před 6 lety +2

      D D are you aware of any books/courses/ or some other materials that teachec c++ the way she promotes? Thanks

    • @Danielle_1234
      @Danielle_1234 Před 6 lety

      I learned C++ in the early 90s, so I do not know of any classes, even if they are a google away, because I have no reason to look for C++ classes. sorry
      I hear www.amazon.com/C-Programming-Language-4th/dp/0321563840 is good, but have not had a reason to look at it.

  • @tux1968
    @tux1968 Před 6 lety +2

    Very nicely presented. A shining example of how to make a clear and accessible talk. Thanks.

  • @Embedonix
    @Embedonix Před 6 lety +14

    Kate is great...her lectures on PluralSight helped me a lot back in the day!

  • @thunder852za
    @thunder852za Před 5 lety +25

    on of the best talks about c++ I have ever watched.

  • @krytharn
    @krytharn Před 6 lety +29

    Very good talk, well presented and great info. Thanks for sharing.

  • @LaurentLaborde
    @LaurentLaborde Před 2 lety +3

    So I'm relearning C++, I never really used it seriously and I didn't touch it since C++11 came out. That was one of the "most helpful" video I ever watched about C++. (I didn't even know the core guideline existed)

  • @childhood1888
    @childhood1888 Před 2 lety +1

    How calm and confident. Loved it!

  • @georganatoly6646
    @georganatoly6646 Před 3 lety

    Very well informed and presented.

  • @jplflyer
    @jplflyer Před 4 lety

    Amazing talk. Thank you so much.

  • @draguin
    @draguin Před 6 lety +18

    About the order of member variables, Alexandrescu had a talk explaining they should be sorted by hotness, because adjacent members are adjacent in memory, and they'll go in a CPU cache together.

    • @jaredmulconry
      @jaredmulconry Před 6 lety +4

      draguin For optimisation purposes, yes. When you're first writing your application, you tend to start with some other grouping, usually logical groupings or semi-random.

    • @vogue43
      @vogue43 Před 6 lety +3

      Late optimization is the root of all evil.

    • @jaredmulconry
      @jaredmulconry Před 6 lety +9

      @vogue43 I suspect that if you think you know ahead of time how hot your member variables are, you're probably wrong.

    • @dartzon
      @dartzon Před 6 lety +8

      The member variables' order impacts the memory alignment of the struct or the class and you can sometimes shrink your struct's size just by reordering theses members

  • @silkworm6861
    @silkworm6861 Před 5 lety +4

    Good talk, I learned a lot!

  • @maskaywilfred8593
    @maskaywilfred8593 Před 5 lety +1

    she’s made to do this! great talk!!

  • @shenzi1118
    @shenzi1118 Před 4 lety +11

    18:25
    "... and just try to make the compiler happy. Just add starts and ampersands until the compiler shuts up."
    Thats my life basically.

  • @andrez76
    @andrez76 Před 2 lety +1

    Great teaching skills on display.

    • @CppCon
      @CppCon  Před 2 lety +1

      Glad you think so!

  • @yutubl
    @yutubl Před rokem

    Very good tipps!
    @Refactore & avoid const_cast after violating const correctness by introduce cacheValue:
    I am a little bit proud that I thought directly the presented solution before shown in the video (refactoring "extract member value into own class and replace a pointer reference to it")

  • @PascalSommerMovies
    @PascalSommerMovies Před 5 lety +27

    ok why the hell did noone tell me about enum class before this is all i ever wanted in my life for gods sake

  • @cmdlp4178
    @cmdlp4178 Před 6 lety +15

    I think to the second question, immediately used structs/anonymus structs are useful for returning an unnamed struct with named members, but c++ only allows immediately used structs for typedef and variables but not functions...
    immediately used structs in variable-declarations:
    static struct {
    int coins, lives;
    } player { 10, 3 };
    or as typedef:
    typedef struct {
    int x, y;
    } Position;
    But why not also in function-declarations or just used to directly initialize:
    struct {
    int div, rem;
    } divmod(int a, int b) {
    return { a/b, a%b };
    };
    Or
    auto divmod(int a, int b) -> struct {
    int div, rem;
    } {
    return { a/b, a%b };
    };
    Or
    auto divmod(int a, int b) {
    return
    (struct { int div, rem; }) { a/b, a%b };
    };
    Why does the c++-committee not got the idea to allow a struct-definition everywhere as a type-expression like "std::vector" or "unsigned int*"?

    • @mikehebert5489
      @mikehebert5489 Před 2 lety

      It may have been impossible to allow that syntax without syntax conflicts

    • @robrick9361
      @robrick9361 Před 8 měsíci +1

      @@mikehebert5489 What about a new keyword?
      anon {
      int div, rem;
      } divmod(int a, int b) {
      return { a/b, a%b };
      };

  • @richardcavell
    @richardcavell Před 5 lety +21

    39:53 “The problem with C++ is, our early defaults are mostly wrong.”

    • @homelessrobot
      @homelessrobot Před 4 lety +5

      this is what is wrong with a great many languages.

  • @dennisrkb
    @dennisrkb Před 2 lety +1

    Kate when can we expect some more talks from you?

  • @CharlesHogg
    @CharlesHogg Před 6 lety +8

    Terrific talk, both in content and presentation.
    I was surprised by the guideline about initializer lists, because I thought compilers always warn if you initialize them in the wrong order. (At least, mine always does, and I had taken it for granted as a language feature.) I think some of the advice about not using member variables in subsequent member initialization becomes obsolete if the compiler enforces the initialization order.
    Again, though -- this is a minor nitpick for a talk that should be required viewing for C++ engineers.

    • @vxorpsxorlox9918
      @vxorpsxorlox9918 Před 4 lety +1

      msvc didn't have a warning for this until Visual Studio 2017 :( Now there's C5038.

  • @mcmaddie
    @mcmaddie Před rokem

    9:24 I don't remember time when initialization list could be in any other order than the one the variables are listed in class. I'm guessing that has not always been the case but as recently as 2017?

  • @JohnDlugosz
    @JohnDlugosz Před 6 lety +2

    I remember the bit about pipes. :)

  • @sumeeetk
    @sumeeetk Před 6 lety +1

    informative.

  • @s-c-iulian
    @s-c-iulian Před 4 lety +1

    when I saw the scissor I tough is a talk about computer graphics, well I was wrong, seems to be general guidelines as the title says...

  • @TheMrKeksLp
    @TheMrKeksLp Před 3 lety

    gsl::non_null is amazing!

  • @zhulikkulik
    @zhulikkulik Před 7 měsíci

    Why c++ doesn't support = {.x = 42, .y=7} kind of initialization?

  • @michelromero7671
    @michelromero7671 Před 2 lety

    35:57 so pythonic, I love it!

  • @Beatleman91
    @Beatleman91 Před 2 lety

    Can somebody explain the guys' complaint on 54:03? Like for someone who is not that proficient?

  • @Hauketal
    @Hauketal Před 6 lety +4

    At 44:25 the include has a bad \ in the file name. At least escape it as \\ or better use / even on MS.

  • @kamalabuhenamostafa
    @kamalabuhenamostafa Před 6 lety

    Nice Lecture ........

  • @CharIie83
    @CharIie83 Před 6 lety +5

    what if you like bikeshedding?

  • @TranscendentBen
    @TranscendentBen Před 2 lety

    Excellent content so far, but I got to 14:38 but there's four words on a slide which wraps to the next line!
    Oh, never mind, it that's the worst thing in the talk I'll be happy.

  • @VoyivodaFTW1
    @VoyivodaFTW1 Před 2 lety +1

    The fighting with the compiler thing is a really big issue still.

  • @feraudyh
    @feraudyh Před 5 lety

    is there a performance loss by using the suggested style of 13:20 ?

  • @ZombieLincoln666
    @ZombieLincoln666 Před 4 lety

    Does she have a C++ course somewhere online? If not she should..

  • @atimholt
    @atimholt Před 4 lety +1

    @ 24:30 - It was my understanding that, since C++11, movable types only get shallow copied when returned out of their originating scope. That is, they’re *moved*.

    • @MagnificentImbecil
      @MagnificentImbecil Před 4 lety +6

      Since long before C++11, return value optimization (RVO) has been supported in many cases.
      And it has been even better than constructing-a-temporary-and-moving-the-temporary-and-destroying-the-remains-of-the-temporary.
      With RVO, there is no temporary--we are directly constructing the return value residing in the stack frame of the caller.
      The callee is passed an invisible parameter--a pointer to the stack-allocated-by-caller-but-not-yet-constructed return value, and the callee uses something similar to "placement new" to construct-without-allocating at the specified address.
      That is part of the reason why writing "return std::move (my_local_variable);" is often a pessimization. It is better to write "return my_local_variable;".
      I think C++17 has made RVO a requirement.

  • @blanket8236
    @blanket8236 Před 2 lety

    i found it very funny how the main point of this talk was being consistent however, every slide kates code style was wildly different,

  • @robrick9361
    @robrick9361 Před 8 měsíci +1

    Every code base should have at least 1 const_cast, otherwise it's like playing russian roulette with no bullets.

  • @N.i.c.k.H
    @N.i.c.k.H Před 5 lety +6

    -127 to 128?

    • @Speglritz
      @Speglritz Před 2 lety

      I'm more used to -128 to 127

  • @VictorOrdu
    @VictorOrdu Před 2 lety

    Sensei...

  • @hello-ld8bs
    @hello-ld8bs Před 6 lety

    She's just like my biology teacher, lol.

  • @JakobRobert00
    @JakobRobert00 Před 5 lety +10

    36:30 You don't write multi-page functions in the first place if you care about readability, she should have stressed that. You should see instantly, what a function is doing at a high-level. If you are interested in the details, then you look into the sub-functions.
    one example:
    void drawGame()
    {
    drawMap();
    drawPlayer();
    drawEnemies();
    // ...
    }
    If you are interested what is drawn in a game, then this function explains it to you, you do not need to know any implementation detail. If you have a monster function of thousands of lines, you just have no idea what this function is actually doing and need to read through all the details and then guess what the intent behind this function is.

    • @robrick9361
      @robrick9361 Před 8 měsíci +2

      But if you're looking at the function's body then you do care about its implementation.
      If those 3 functions are only called in drawGame then you have to go chase their function bodies in other files.
      Seeing what a function does at the high level is accomplished with a good function name...i.e. drawGame()

    • @cantthinkofaname1029
      @cantthinkofaname1029 Před 4 měsíci

      ​@robrick9361 it's to gain an immediate high-level understanding of what the implementation actually does before you get into the specifics. You'd have to read the entirety of the implementation logic to see understand what it does even at a design level.

  • @MrAbrazildo
    @MrAbrazildo Před rokem

    9:17, this should be a core guideline: never write a sentence that 1 var changes more than 1x. It saves you from reading the standard. Stop arguing, just don't do that.
    1:02:05, I had once a bug regarding to the 'auto' keyword. It was on a far, far ancient time, when I was starting to use it, and didn't know it wouldn't grab the reference declaration...
    Don't remove it from the language, it would be a blunder.

  • @misium
    @misium Před 4 lety +3

    About being expressive: gsl::narrow_cast should be gsl::narrow_unsafe_cast. No need to explain the meaning then.

    • @sprytnychomik
      @sprytnychomik Před 3 lety +3

      Careful what you wish for... It might end up in C++23 as narrow_cast throwing an exception, narrow_unsafe_cast doing what narrow_cast is doing currently and narrow will result in a compile time error. Or we will see gsl23::narrow_unsafe_cast, etc. ;)

  • @philiptrueman1022
    @philiptrueman1022 Před 2 lety +1

    I wonder how many people, Kate Gregory included, know where the 'bikeshed' metaphor comes from. It's from the original book on Parkinson's Law by C. Northcote Parkinson. Beware of other authors trading on the name - go to the master himself.

    • @gregcons
      @gregcons Před 2 lety +1

      Of course I know. But it's not actually important. It's become one of our jargon terms. If you know what it refers to, that's what matters.

  • @Donmegamuffin
    @Donmegamuffin Před 6 lety +24

    This talk just seems to be straight out of "Effective C++" by Scott Meyers

    • @korvmakak7727
      @korvmakak7727 Před 6 lety +3

      Electromag[e] Good for you!

    • @Donmegamuffin
      @Donmegamuffin Před 6 lety +2

      ?

    • @korvmakak7727
      @korvmakak7727 Před 6 lety +9

      Exactly. And even though he may have already written about that stuff, the point was to educate about the mentality and usage. Many talks could be considered redundant if you said, "just read this book"... but that's not really the point.

    • @korvmakak7727
      @korvmakak7727 Před 6 lety +3

      Further, I would not be surprised if the guidelines was inspired by Scott or vice versa.

    • @Donmegamuffin
      @Donmegamuffin Před 6 lety +5

      Nowhere in my wording did I call any of it a bad thing or wrong

  • @williamchamberlain2263

    One Weird Trick To ...

  • @MrSapps
    @MrSapps Před 6 lety +3

    Argh stop saying null pointer exception.. that won't happen it will just crash, you can't catch(const std::NPE&) {}

    • @MagnificentImbecil
      @MagnificentImbecil Před 4 lety +2

      Yes, you can, and with good reason, at least in the world of Windows.
      During the same period when support for C++ exceptions was gaining support in compilers (the early 1990's), Windows introduced Structured Exception Handling (SEH).
      SEH has similar rational/intent as C++ exception handling, and there were some differences.
      (Similarity: They both support decoupling of error-detection from error-recovery.)
      (Difference 1: C++ exceptions can transport arbitrarily large pieces of information from the throw-site to the catch-site, while Structured Exceptions have fixed format--which also makes the matching faster than catch-matching-to-thrown-object-type.)
      (Difference 2: Errors such as integral-division-by-zero and reading-or-writing-via-bad-pointers are undefined behaviour with Standard C++, but the behaviour is precisely defined with Windows SEH--as with POSIX signals, but in a much more useful way--really, in a "structured" way).
      Both C++exception handling and SEH are good.
      Programmers have been using both, e.g. in different parts of the same program.
      Therefore, compilers have added support (e.g. the /EH... switches for Visual C++) for allowing us to specify how the two mechanisms interact:
      - Do Structured Exceptions unwind the stack like C++ exceptions do (by destroying stack objects until a matching handler is found) ?
      - Can we use the C++ "catch (...)" clause to match a Structured Exception ?
      - Can we use the SEH "__raise" clause to catch a C++ exception of a very simple type (e.g. DWORD) ?
      Also, the _set_se_translator Windows API function can register a callback for the __raise-ing of a Structured Exception, e.g. one which throws a C++ object which carries the fixed pieces of information from the Structured Exception to a C++ catch clause (not just to the C++ "catch (...)" clause, which gets no information).
      Interestingly, these features, which are not standard in C++, but widely available in the world of Windows, constitute reasons why, for portability, we may want to avoid throwing C++ objects of non-std::exception-derived types.

  • @KFlorent13
    @KFlorent13 Před 2 lety

    47:40 Why is signed int a bad choice for an integer from 0 to 59 ?

    • @muhammadharris4470
      @muhammadharris4470 Před 2 lety

      She said the opposite. 0-59 will fit but some int might not fit in like the system time. Better to have unsigned int to have a bigger range of positive integers

  • @mrlithium69
    @mrlithium69 Před 6 lety +4

    Lol @ three_ints_and_a_string

  • @LemonChieff
    @LemonChieff Před 3 lety

    If you have code that doesn’t follow the guidelines but is *benchmarked*, and runs faster: use the faster code, make sure you don’t have ANY UB, and write the guideline compliant code in the function documentation.

  • @patrickvollebregt855
    @patrickvollebregt855 Před 6 lety +2

    I'm sorry, but the very first example about the initializers, the refacored class has a different meaning. In A a,b,c become 1,2,3 with the default constructor. In B they become -1,-1,-1. In A c is optional in the second constructor, in B it is mandatory. I think the first guideline should be: When refactoring, don't change the meaning of the code.

    • @EmmanuelIstace
      @EmmanuelIstace Před 5 lety

      That's why you should use test harness when refactoring code (Cfr working with legacy code from michael c. feather)

    • @gregcons
      @gregcons Před 4 lety +4

      The point was that the old meaning was accidental and organic. People assigned default values without really thinking about it, often assuming they would never really be used. As part of stopping and thinking about the class, you can give consistent default values without relying on the authors of future constructors. So this meaning change was a deliberate correction.

    • @richtourist
      @richtourist Před 2 lety +1

      @@gregcons How do you know that the original meaning isn't the correct one? Your assumptions might be wrong.
      I think you should have shown the 'better form' of the 'original' meaning, because that code has been running just fine for years but recently they have been getting some strange results over in Sector 7-G.

  • @michaelkohlhaas4427
    @michaelkohlhaas4427 Před 5 lety

    *I have never seen a good presentation or a course with Kate Gregory!!!*

  • @Zzznmop
    @Zzznmop Před 6 lety

    Lost me at fudge factors lol

  • @isodoublet
    @isodoublet Před rokem

    The example about casting away const is completely wrong. Taking away "const" from getValue _is_ the correct thing to do, because it clearly is _not_ const anymore. That's the tradeoff you made when you decided to implement that cache. Being explicit about it is not violating const-correctness, it's _enforcing_ it. It doesn't matter that reading from a class is usually const; for this one it's not. This class behaves differently, so beware! Communicating that is good and useful. On the contrary, slapping the keyword "mutable" on the cache is effectively syntactic sugar for... casting away const, precisely the thing that was to be avoided, and it makes the header file a lie, again something to be avoided.
    Our API types should reflect what our code _is,_ not what we _wish_ it were.

    • @masheroz
      @masheroz Před 11 měsíci +2

      I disagree. The reason we have mutable in there is because we want to cache the value and only recalculate when we want to get the value. We're not really changing anything when we get Value, we're triggering a calculation that would have happened anyway, just in a more computationally friendly manner.