How I use C++: a line-by-line code review

  Рет қаралды 228,630

strager

strager

Күн бұрын

Let's walk through quick-lint-js, a 100k-line C++ project, and talk about the code style and design decisions.
Links:
Stewart Lynch's Sane C++ series: • 10x: Sane C++ Live Str...
C++ Core Guidelines: isocpp.github.io/CppCoreGuide...
decode_utf_8_result design help from u/LegionMammal978: / j3b6afz
00:00:00 dev environment
00:00:36 font
00:00:55 editor
00:01:24 terminal
00:01:48 copyright comments
00:03:39 reduce clutter
00:05:05 copyright year
00:05:57 copyright checker
00:06:19 source file generator
00:06:37 how I write new code
00:07:09 includes
00:07:38 .h next to .cpp
00:08:40 namespace
00:10:19 citation comment
00:10:57 naming style
00:11:36 preferred style
00:13:09 custom syntax highlighting
00:15:06 char8_t
00:16:25 char8_t vs char
00:17:55 nested functions
00:19:08 number literals
00:19:38 raw pointers
00:20:35 smart pointers
00:22:04 anonymous namespace
00:22:55 which compiler?
00:23:37 compiler warnings
00:25:02 returning classes
00:25:35 struct vs class
00:26:02 better design
00:27:23 char8_t confusion
00:28:34 designated initializers
00:29:22 zero initialization
00:30:01 assert
00:30:18 QLJS_ASSERT
00:30:51 custom assert message
00:31:46 better assert debugging
00:32:16 cassert sucks to debug
00:34:40 if variables
00:37:25 casting
00:39:22 in_range
00:40:53 narrow_cast debugging
00:42:05 source_location
00:42:36 __builtin_LINE
00:43:33 narrow_cast performance
00:44:25 todo comments
00:45:16 issue tracking
00:46:49 performance notes
00:47:23 column limit
00:48:24 screen size
00:49:02 monitor aspect ratio
00:49:43 hard column limits
00:52:06 type inference
00:53:46 object initialization
00:54:30 explicit
00:55:01 implicit
00:57:06 no implicit in Rust
00:58:21 Rust ..
00:58:40 most vexing parse
01:00:13 header file
01:00:29 include guards
01:01:45 include guard fixer
01:02:40 messy #include guards
01:03:18 file organization
01:04:52 #include
01:05:17 namespace references
01:06:46 code formatting
01:07:28 legacy headers
01:08:37 #include order
01:11:00 #include style
01:12:15 header organization
01:12:29 macros in headers
01:13:19 header example
01:14:02 blank lines
01:15:03 forward declarations
01:15:52 simdjson-fwd
01:18:04 portability and the standard library
01:19:06 working around compiler bugs
01:20:07 port folder
01:21:18 binary bloat
01:22:45 binary size data
01:24:07 std::filesystem::path bloat
01:25:12 #if defined
01:26:41 platform-specific files
01:27:31 everything is compilable
01:28:28 std::filesystem::path
01:30:08 why not std::filesystem
01:32:40 exceptions & error handling
01:33:45 why use C++ exceptions?
01:34:37 try_catch_stack destructors
01:35:34 result
01:36:57 static and dynamic analysis
01:37:45 clang-tidy
01:38:47 warnings in CI
01:40:04 what's next

Пікірлер: 366
@allNicksAlreadyTaken
@allNicksAlreadyTaken Жыл бұрын
The point people make oftentimes is not to never use raw pointers, but to never use *owning* raw pointers. And I find it to be very beneficial to me.
@strager_
@strager_ Жыл бұрын
Good distinction! Yeah, I generally agree with you about trying to not use owning raw pointers. I'm not 100% sold though.
@puppergump4117
@puppergump4117 Жыл бұрын
@@strager_ I made a library that uses raw pointers and I changed it to smart pointers to fix something. The smart pointers honestly provided nothing useful. From within the library, there's no need to either take ownership of or use reference counting for an object. I don't really see any situation where you would lose track of something so much that you can't tie its destruction to whatever is using it or explicitly destroy it. For those reasons, since raw pointers are not only easier to work with but also don't cause unexpected behavior by mangling your objects, and because they do the exact same things, I say smart pointers are dumb.
@XD82ful
@XD82ful Жыл бұрын
​ @Pupper Gump For a small project not using smart pointers is usually not a problem, though I would still recommend it. If working on any larger project it helps a lot. When working with raw pointers you need to be very careful taking everything into consideration and if the project is large that can be very difficult. For example if you have a class that has a raw pointer which it owns and the destructor is responsible of deleting it. Do you every time remember to implement the copy, move constructors and assignment operators to make sure nothing bad happens or at least mark them as deleted. And even if you do this it is a lot unnecessary work. If you instead used unique pointers then copy constructors would be implicitly deleted(you can explicitly define it if you really needed it) and move constructor is implicitly defined and it uses the move of the unique pointer. So in case you don't need to do anything special you don't have to define any of the constructors, assignment operators and destructor and you are guaranteed to not have a memory leak and everything with out writing a single line of code. Also declaring pointer as unique pointer indicates clearly which object owns it and is very useful for others reading the code to know who owns it. In general when you see unique_ptr you know you own it and when you see * you know you are borrowing it from some other object. And in case unique pointers are not enough, that is there is no clear owner of the pointer then using shared pointers instead of raw pointers becomes even more important. Handling the raw pointers and doing some sort of manual reference counting to make sure when to delete the pointer is prone to errors if you ever forget to decrement the count you run into a memory leak and in some cases these can be very hard to find especially since they can happen only in very rare cases that are hard to reproduce. Shared pointers are not going to solve everything as they have their own issues like circular references, so you have to still be careful when using them but at least you have one degree of freedom less to keep track of. And if you consider the performance I doubt there is going to be a situation in which using smart pointers is going to be (significantly)slower compared to using raw pointers. Unique pointer is more or less zero cost while shared ptr keeps reference count but you most likely would anyways need to keep it up by your self. And in that special case where the almost non existent performance benefit matters, is most likely some algorithm that is only a very small part of the code in which case I suppose you could consider not using smart pointers, and even in that case I would profile whether it has any benefit. Anyhow you should always use unique_ptr whenever it is possible. If you need shared_ptr try to think if you can do something to only need unique_ptr and if it seems completely impossible then use the shared_ptr. There are also different types of smart pointers but in case you need to use those(or raw pointers) the architecture is probably not very good at that point(maybe in some case you could actually have some good reason to use them but unless you can tell a very good reason to your self: why you need to use them, why nothing else works and why nothing can be done differently then you shouldn't)
@puppergump4117
@puppergump4117 Жыл бұрын
@@XD82ful In my case, smart pointers appear unnecessary for a lot of the pointer operations I'm doing. My project is a gui, so I take objects the user instantiates and store their pointers inside a vector of pointers. On top of that, an object's pointer can also be held by another object through binding, which might be a good case to use shared pointers without ownership. On top of that, to make creating new objects easier, I give objects a vector to store their own instantiations of other objects, which is then accounted for in the update/draw loop. Because of all of this, I've created many functions to abstract any pointer operations. And I haven't had any issues with them for a long time. I think what you're talking about are projects that use many classes from many libraries and the programmer really just mixes them together, but my case is just using one specific library and adding on top of it. Of course, maybe I'm gonna regret using raw pointers in the future, but here's the project if you really wanna tell me off: github.com/PupperGump/gui/tree/master/gui
@XD82ful
@XD82ful Жыл бұрын
@@puppergump4117 I took a quick look and I think you are basically thinking of a different thing here takin a reference like for example having Button b; Object* o = &b; is not actually something where you need a smart pointer, here the memory is in stack and it cannot even leak as it will be released as it goes out of scope. By not using smart pointer one means that you directly do something like Object* o = new Button(); so that you are dynamically allocating memory and you need to release it by calling delete o; In these cases instead of using new and delete you want to use smart pointers in which case you would have something like std::unique_ptr oo = std::make_unique(); this creates object in dynamic memory using new internally and then releases it using delete internally without you having to call it explicitly. So basically using raw pointers is fine even if you are using smart pointers(and in fact necessary) the point is that you won't call new and delete by your self. And in above example you can get the raw pointer by calling oo.get() which returns Object*. Anyways good luck with your gui project :)
@wildmushroom2452
@wildmushroom2452 Жыл бұрын
Just found your channel and I have to say, I could sit and listen to you for hours, you are very articulate and explain things very nicely. Defiently sticking around for a while since I have picked up C++ recently!
@strager_
@strager_ Жыл бұрын
Cool! Follow me on Twitch if you want to listen to me for hours.
@disgruntledtoons
@disgruntledtoons Жыл бұрын
Font choice: The most important features are that the numeral 1 and the lower-case l are readily distinguishable, and that the zero and the capital O are distinguishable. Your IDE should alert you to these things. Also, it's interesting how so many of the problems we struggle with in C++ in 2023 are the result of choices that should have been made when C was first developed.
@doublegdog
@doublegdog Жыл бұрын
This is the type of video i love to watch. Raw explanation and code and no fluff. You just won another sub!
@jimmydandy9364
@jimmydandy9364 Жыл бұрын
Indeed a good video to watch if you have insomnia and can
@edmunns8825
@edmunns8825 Жыл бұрын
@strager I just found your channel, I watched this video the other day. I'm learning C++ at the moment coming from a C background. Just wanted to say this is the best video I've seen for just going through and talking about code structure and why you do certain things and don't do others. I found it really helpful.
@strager_
@strager_ Жыл бұрын
You're welcome!
@apolloapostolos5127
@apolloapostolos5127 Жыл бұрын
This whole video was one rabbit hole after another, for me. . I think it’s helping to get me up to speed with the programming skillsets.
@orestes_io
@orestes_io 6 ай бұрын
This is the perfect video for someone with enough experience looking for a tour of actual C++ code and not just cookie cutter tutorials. Super valuable! Thank you :)
@TerjeMathisen
@TerjeMathisen Жыл бұрын
You seem to care about performance, so just a small tip which I discovered when I updated my old word count (wc) utility to count utf8 letters instead of bytes: Casting the input array to signed char allowed me to detect all follower bytes with (c < -64) instead of checking two boundaries (c >= 0x80 && c < 0xC0). Next I split the processing into chunks of 128 bytes (or any other small multiple of 16) and stored the count as a byte: At this point the autovectorizer would generate pretty much perfect code, i.e. 16-byte (or 32 for AVX2) loads, followed by a simd compare and a simd subtract (since the compare results are -1,0 instead of 1,0). It ran in ~0.3 clock cycles/byte.
@strager_
@strager_ Жыл бұрын
> Casting the input array to signed char allowed me to detect all follower bytes with (c < -64) instead of checking two boundaries (c >= 0x80 && c < 0xC0). That's pretty cool! Clang does this optimization, but GCC doesn't: godbolt.org/z/rGGjdK7xo
@TerjeMathisen
@TerjeMathisen Жыл бұрын
@@strager_ Yeah, I am using Rust on Windows and since MSVC can compile with either their own or the Clang back end, I believe that is what I'm seeing. (I use cargo-asm for disassembly listings)
@xit
@xit Жыл бұрын
The legend is back!
@michaelfekadu6116
@michaelfekadu6116 Жыл бұрын
Very cool walkthrough! Definitely a little bit insane, but aren't we all a bit insane in our own ways ;) Thank you for the delightful view into your coding workflow!
@AaroRissanen-pi4td
@AaroRissanen-pi4td 4 ай бұрын
This video was a goldmine of professional and robust practices. Thank you very much, I learned many a new thing!
@strager_
@strager_ 4 ай бұрын
You're welcome. I hope you don't borrow my bad practices too! 😆
@MrAbrazildo
@MrAbrazildo Жыл бұрын
18:24, I agree. I think outside is prone to errors, because it can be called in an unexpected situation, which currently you are unaware. Before lambdas, I rather even make it a macro, to use inside the f(), only, and #undef it right after the f(). Although I relax if the f() is called by more than 1 f() - this case, it's not technical internal complexity, it has probably a higher-level meaning. 21:20, nowadays, MemorySanitizer catches this. 23:20, I benchmark Clang vs GCC on switch vs if-block vs array vs bitfield. Sometimes 1 was better for some cases, other for other cases.
@fano72
@fano72 8 ай бұрын
Awesome channel, top work! I'd love to watch all your videos since I have the time.
@joeybasile1572
@joeybasile1572 5 ай бұрын
Nice video. Very interesting perspective. Thank you for your time.
@bluefal
@bluefal Жыл бұрын
your code is amazing and fabulous!!!
@spacemonky2000
@spacemonky2000 9 ай бұрын
The most impressive part of this video is your control over Vim
@malusmundus-9605
@malusmundus-9605 Жыл бұрын
Nice vid man- very cool to see someone be honest about what's working and what's not with their setup/code. Oftentimes I see programmers (especially young ones) assert that everything they are doing is correct and if you do it differently you're just plain wrong.
@strager_
@strager_ Жыл бұрын
My style evolves with every project.
@LeadingNPC
@LeadingNPC Жыл бұрын
Good comment.
@alurma
@alurma Жыл бұрын
Clangformat: I exist to solve formatting problems This video: is about formatting problems :D
@silicondecay
@silicondecay 10 ай бұрын
I like the syntax highlighting, makes it a lot better imo. Also would find it pretty hard not to have go to definition/implementation/type definition. I use neovim with all this. I could live without auto completion though
@MrAbrazildo
@MrAbrazildo Жыл бұрын
51:20, although I'm not a fan of vertical code, this is a good exception: - The vertical alignment allows to quickly spot some errors, like lack of bits, even if an operator disalignment was seen 1st, for instance. - Double click in a keyword, and all its occurrences on the block will appear, helping spot errors too - _if the code editor has this feature_ . - The single line would be too large. I use to go towards the right margin, using commands of the "same kind": variable declarations (of any type), tiny lambda definition, loop-if-consequence ("same thing" to me), and so on. 52:17, auto can be: - "Refactorer": if the returning type ever changes, you don't need to hunt its occurrences. - Defensive: avoids you from mistaking the returning type, getting an inplict conversion, which can lead to problems. - Elegant (at least I think so): getting "the same face" for declarations. I have been using it on all places, like a rain, and curiously I'm not running into issues.
@Katniss218
@Katniss218 Жыл бұрын
I find auto (or var in C#) very hard to read. It's very often not clear the type that is returned, and I prefer not having to hover over every identifier to see it.
@MrAbrazildo
@MrAbrazildo Жыл бұрын
​@@Katniss218 In this case, when the f() is already fixed, auto may be replaced by a typedef. But when the f() is being developed, auto is more productive. Btw, its absence for return in Carbon is my main complain against the new language.
@PeteBrubaker
@PeteBrubaker 23 күн бұрын
35:30 - I like that style a lot. It really helps when learning unfamiliar code, or when you inherit something and the original author is long gone.
@jbooks888
@jbooks888 Жыл бұрын
I'm so glad I only write programs for myself, some simple and less than a thousand lines of code and others with tens of thousands of line, BUT apart from a couple of small business friends, I'm the only one who uses them. And I've used Visual Basic 6 and Access Databases for all of them and they are plenty fast enough! With the ability to write your own User Controls, the maintainability and convenience is amplified even more. But, I don't need it to be accessible to any other coders and I don't need it to run on anything but Windows.
@strager_
@strager_ Жыл бұрын
Yeah, portability is a small burden but it's all over my code. =\
@MrAbrazildo
@MrAbrazildo Жыл бұрын
1:14:00, I rather a compact block of declaring f()s: - Blank lines become begin/end of a block section, which is much more meaningful than just separating cmd lines. - Result in compact code, which raises the likelihood of fitting the block in a screen. - According to Kate Gregory, who worked "rescuing abandoned projects" (due to lost of control over them): _"Those blank lines dividing sections of code help us a lot! Please use them."_ (for this propose). 1:26:09, the Codeblocks IDE I use puts a comment after the #endif, referencing its #if argument. It's handy when the block is big.
@strager_
@strager_ Жыл бұрын
> 1:26:09, the Codeblocks IDE I use puts a comment after the #endif, referencing its #if argument. It's handy when the block is big. Yeah, that does sound super handy! Code Biscuits does this for many languages in many editors: github.com/code-biscuits/
@mattshu
@mattshu Жыл бұрын
I am in love with the monospaced Comic Sans. I feel like society would shame me but I can't look away
@MaxCoplan
@MaxCoplan Жыл бұрын
While I know comments are normally suboptimal, I might’ve put a comment at 37:00 explaining what `size` meant in this case. Or add a comment on the definition of the struct, but if `size` doesn’t always mean how much you skip then I’d just put above line 96 in this file
@strager_
@strager_ Жыл бұрын
Yeah, my util/utf-8 module does need to be improved. Same with every other module... 😅
@bsgamer5069
@bsgamer5069 Жыл бұрын
Your voice is so good it always help me fall asleep.
@strager_
@strager_ Жыл бұрын
I'm sorry that I am so boring 😭
@bsgamer5069
@bsgamer5069 Жыл бұрын
@@strager_ You voice is absolutely amazing.🫀
@mikeharry1799
@mikeharry1799 11 ай бұрын
regarding usage of raw pointers: use them when you don't care about ownership; so an owning class uses unique ptr, but when passing it to other functions for usage it should just pass raw pointers or references
@codahighland
@codahighland 11 ай бұрын
When it comes to identifier style, I follow Qt's style guide. The fact that this largely agrees with most other modern languages (C# choosing to use initial caps on functions and properties is annoying) is a plus, even though I was using C++ first. (I learned C++ in 1997. The keen-eyed among you may notice this is before the language was standardized.) This has become mildly irritating as C++ gets better and Qt starts using more out of std::, but what can you do?
@GrindAlchemyTech
@GrindAlchemyTech 10 ай бұрын
🧑🏽‍💻Thanks this was great🏆
@channel-so2st
@channel-so2st Жыл бұрын
What roadmap / books would you recommend to learn modern C++ for an intermediate Python / Kotlin programmer? Don't want to risk reading a 1000 page book that teaches bad habits
@strager_
@strager_ Жыл бұрын
I recommend against roadmaps. I suggest picking a project idea and working on it.
@simplepycodes
@simplepycodes Жыл бұрын
Very nice! impressive.
@Heyim18bro
@Heyim18bro Жыл бұрын
I didn't realize I cared so much about font until I saw this, this font would drive me crazy :x
@vytah
@vytah Жыл бұрын
This kind of font is very readable, I use Fantasque Sans Mono for the same reason. Many "normal" programming fonts have characters that look too samey, made of repeating identical angles.
@Heyim18bro
@Heyim18bro Жыл бұрын
@@vytah i can't get with it, i feel like there's a lot more fonts that are easier to read but i'm not one to care for them too much; that being said I would care if this was the font set and switch it to something else xD
@oldnews4160
@oldnews4160 Жыл бұрын
@strager - In your opinion, what are the uses for a programmer nickname? In this case, it's not for anonymity. Follow up question, Is it something you would like to be called in the workplace or just by friends? Thank you for this video and your content. Much appreciated. I enjoyed this one particularly and seeing your take on C++.
@strager_
@strager_ Жыл бұрын
If you are referring to 'strager', I consider that my name, not a 'programmer nickname'. > Follow up question, Is it something you would like to be called in the workplace or just by friends? I go by 'strager' everywhere not-legal, but sometimes I use 'Matthew' if I think someone would have a hard time pronouncing or remembering my name. (I should get around to changing my legal name...)
@oldnews4160
@oldnews4160 Жыл бұрын
@@strager_ lol That'd be awesome. Thank you for the reply. Just curious is all. The more I get into programming, web development, and showcasing my projects I have been considering adapting a media name of sorts or alias. I have seen cybersecurity professionals and hackers also use tech names for anonymity, but I have yet to think of a name. In college I had a nickname from friends, but other than that I have had no other aliases other than just youtube or video games. I constantly change these names though, there hasn't been one alias I've stuck on. Again, thank you for the reply, I appreciate you.
@marioc485
@marioc485 Жыл бұрын
Great Video :)
@ItzAnameOk
@ItzAnameOk Жыл бұрын
1h and 40m of Strager? Yes pls. Also, nice shirt
@ihatethewind
@ihatethewind 9 ай бұрын
You are sooooo close to Unix beard! Keep going!
@gblargg
@gblargg Жыл бұрын
7:14 For the header if you include it first this ensures that it doesn't have any dependency on other includes for it to compile. This is especially important in the library case you mention. 8:47 The point of namespaces is to avoid clashes and unwanted interaction with other code you have no control over. Making sub-namespaces like util adds none of this value because you're the author of all the code and can thus ensure it works well together in a single namespace.
@strager_
@strager_ Жыл бұрын
> if you include it first This is a good idea. However, usually my build system uses precompiled headers which makes you trick not work as desired. I've found it better to write a separate tool which #include-s all public headers and doesn't use my build system.
@strager_
@strager_ Жыл бұрын
> The point of namespaces is to avoid clashes and unwanted interaction with other code you have no control over. Exactly! I don't want *my* code having symbol collisions which I need to manually resolve. That's confusing!
@sebastiangudino9377
@sebastiangudino9377 Жыл бұрын
​@@John-kd3bfWho hurt you?
@Hazanko83
@Hazanko83 Жыл бұрын
Really the only times that I use nested namespaces is to define a ''NAMESPACE_implem'' within the primary namespace that holds things not necessarily meant to be used as part of the public interface; usually something like a templated base class meant to be inherited and expanded upon, or free helper functions. In these situations, you can just add ''using namespace NAMESPACE_implem;'' within the block scope of your primary namespace and it won't leak out to elsewhere - and then you don't need to type NAMESPACE_implem when using that stuff in your intended-public-facing classes/functions. Technically yes, the primary purpose of namespaces is to help avoid naming collisions, and may very likely never be an issue in a personal project; but I think it's a good tool for organizing things, as well as removing clutter from the global or individual namespaces. Even in the situation of having multiple contributors, there is a big advantage in being more verbose/concise with naming and structure and what not. You can't ''using namespace'' in a class, but you CAN do it inside of a function. typedef's can also be quite useful, although slightly straying off topic maybe: ''typedef MyPersonalNameSpace::implem::MyClass_Base CLASS_BASE;'' and now you have an alias called CLASS_BASE for your more verbose alternative of a templated class that can be used anywhere the first one would have been used.
@Rust_Rust_Rust
@Rust_Rust_Rust Жыл бұрын
​@@sebastiangudino9377 who asked?
@blacklistnr1
@blacklistnr1 Жыл бұрын
Came here from you big O video where I thought you were programming in Comic Sans, but it looked monospace so I brushed it of as my imagination. It's nice to know it actually was a Comic inspired font and that I'm not going mad.
@strager_
@strager_ Жыл бұрын
Yup, it's Comic Code. I love it! tosche.net/fonts/comic-code
@allNicksAlreadyTaken
@allNicksAlreadyTaken Жыл бұрын
Usually structs are classes without invariants and when I hear struct, I assume it has no invariants, i.e. it's just a bunch of data without methods that do anything overly interesting or complicated.
@felipemalmeida
@felipemalmeida Жыл бұрын
About the qualification of identifiers (where you actually talks about using multiple namespaces), you may actually have clashes with ADL, which is annoying. So, qualifying identifiers is a good thing for libraries at least where you don't know what else code may be included in the same translation unit.
@strager_
@strager_ Жыл бұрын
Yup! Luckily I don't have to deal with that much because I am making an application, not a generic library.
@tricky778
@tricky778 9 ай бұрын
You don't need to write util:: everywhere, you just need using directives. But if your utils are not imported/shared/common/conflicting, then you don't need to bother.
@tabletuser123
@tabletuser123 Жыл бұрын
This thumbnail is a mood
@ZiViZiV
@ZiViZiV 8 ай бұрын
Have you tried treesitter for highlighting?
@strager_
@strager_ 8 ай бұрын
Nope, not yet! I want to try it though.
@ZiViZiV
@ZiViZiV 8 ай бұрын
Just saying because of the highlighting issues you highlighted (scuze da pun 😅) Using treesitter solved some highlight issue I used to have on neovim.
@ZiViZiV
@ZiViZiV 8 ай бұрын
BTW, I am using the same font as you. I agree it is easier to read. Though I hope one day I'll have the time to patch it with my own handwriting LoL
@rid9
@rid9 Жыл бұрын
How about ending type names with "_t"? decode_utf8_result_t, uint8_t, etc... It would make names consistent (decode_utf8_result_t decode_utf8_inline() { ... }) and wouldn't force you to think about how to capitalize acronyms (and would make it very easy to highlight as well).
@WoD2008
@WoD2008 Жыл бұрын
_t types are reserved by the POSIX standard and you shouldnt define your own to prevent future overlap in namrd
@rid9
@rid9 Жыл бұрын
@@WoD2008 Thank you for the comment, I didn't know that, I only knew about C's reserved underscore prefixes. I wonder if there are any other reserved naming conventions.
@strager_
@strager_ Жыл бұрын
I don't like it.
@dagoberttrump9290
@dagoberttrump9290 Жыл бұрын
@@WoD2008 That's a really unreasonable paranoia imho. If you happen to write a language feature outside of any namespace that happens to exactly match a future posix type and you hapoen to update to that posix standard, then you probably hit the 0.0001 percentile case. I would play the lottery with this kind of thinking. Besides, if you happen to declare the exact same type chances are you can now deprecate your own lib in favor of the new posix standard. Wouldn't it be nice if the compiler sends some error down the lines to inform you about this? Tldr; i use _t all the time, never had any hickups
@yogxoth1959
@yogxoth1959 Жыл бұрын
I really wanted to see how you navigate the codebase without LSP and such, do you talk about that at all in this video? I don’t see anything in the chapter titles.
@strager_
@strager_ Жыл бұрын
I didn't talk about code navigation in this video. (I think...)
@metin4yt
@metin4yt 10 ай бұрын
37:18 why do you prefer to have an if/else when you return in the if? Dropping the else would reduce the nest level
@strager_
@strager_ 10 ай бұрын
In this case, I think it's easier to read similar code if they're at the same nesting level.
@neur303
@neur303 Жыл бұрын
When I use CamelCase, each block only contains 1 uppercase letter that way it is mappable to the subset of lower snakecase. i.e. not HTTPWriter but HttpWriter would be equivalent to http_writer.
@strager_
@strager_ Жыл бұрын
blasphemy
@puppergump4117
@puppergump4117 Жыл бұрын
That is the only way to use camelcase.
@tissuepaper9962
@tissuepaper9962 Жыл бұрын
@@puppergump4117 nah, mixed-case letters in an initialism just looks so wrong. If something is common enough that you can use the initialism without a question of whether or not the reader will understand, then you can be assured that the reader will also be able to separate the blocks correctly. If you don't think the initialism is recognizable enough, don't use it at all.
@puppergump4117
@puppergump4117 Жыл бұрын
@@tissuepaper9962 Semantics are tough so here's the breakdown. When you use an ide, most of the time you will just type a few letters of the function or variable and hit enter to get the whole thing. The advantage of camelcase is that it lets you just say httpwr and it autocompletes. Or even htpwr. With snake case, you can only type a single word separated with underscores. So it would be http_wr instead. This is useful to filter out variable names such as members of classes.
@realisticlevel2553
@realisticlevel2553 3 ай бұрын
I would love a vid or stream about your general workflow😢
@henka4166
@henka4166 Жыл бұрын
at 17:38, about pointer aliasing: can't this also be solved by doing it in pure C and using restricted pointers?
@strager_
@strager_ Жыл бұрын
I'm in C++, not C, so no 'restrict'. Even if I had 'restrict', I cannot use 'restrict' on struct members. Often I use pairs of pointers (e.g. a source_code_span has a begin and an end), and 'restrict' is not allowed for those kinds of things.
@PeteBrubaker
@PeteBrubaker 23 күн бұрын
Have you switched to polyglot or tree-sitter for your syntax highlighting yet? It's so worth it.
@sqwert654
@sqwert654 Жыл бұрын
Was wondering what font you use, 49secs in I got an answer. I use MS Comic Sans (since the 95 odd) for the same reason. I find it very readable and natural. But the one your using looks better.
@strager_
@strager_ Жыл бұрын
Comic Code: tosche.net/fonts/comic-code
@sqwert654
@sqwert654 Жыл бұрын
@@strager_ cheers have already bought and installed in Rider. Looks very readable.
@thepirat000
@thepirat000 Жыл бұрын
God mode! ❤
@Zex-4729
@Zex-4729 Жыл бұрын
What are your thoughts on C++ core guidelines? I see you use orthodox(not 100% for your case) C++, I mean a lot of professionals still use orthodox C++ but will you guys ever get on to ISO C++ and C++ core guidelines? This is probably the larges rift in C++ community right now, everyone can write in their own sets of rules, It definitely has a benefit but it's also a problem right?
@defnlife1683
@defnlife1683 Жыл бұрын
Why is it a problem? Maybe an annoyance for a week or two if you move from one job to another while you learn the nuances, but that’s the same thing in every lang no?
@strager_
@strager_ Жыл бұрын
> This is probably the larges rift in C++ community right now The coding style things which the C++ core guidelines discusses are minor. Learning libraries (especially for concurrency) and the architecture is a much harder part of switching codebases than code style.
@strager_
@strager_ Жыл бұрын
> What are your thoughts on C++ core guidelines? The C++ code guidelines has good advice, but it doesn't change how I code much. Some advice, such as "ES.11: Use auto to avoid redundant repetition of type names" and "ES.20: Always initialize an object" and "ES.23: Prefer the {}-initializer syntax", are stupid. I tried it and didn't like it. Other advice, such as "ES.12: Do not reuse names in nested scopes" and "ES.26: Don’t use a variable for two unrelated purposes", is good and can sometimes be compiler-checked (thus enforced).
@Tyler11821
@Tyler11821 Жыл бұрын
@@strager_ I do like the auto style. Not for everything, but it's a consistent line style and you can specify type on the right side. Most code analysis tools can tell you the type if you aren't clear, and C++ doesn't have multi-line inference like rust. It _does_ decrease code changes needed when changing the type in other parts of the code base. I don't personally use {} for everything myself.
@tissuepaper9962
@tissuepaper9962 Жыл бұрын
@@Tyler11821 not a fan of needing IDE features to be able to tell what type is returned. I legitimately see no reason why you would want to abbreviate the function definition with auto. If you're in double-colon hell, use auto-complete or simplify the structure of your namespaces. Make a recognizable alias, if you don't like the other two solutions.
@kvbc5425
@kvbc5425 Жыл бұрын
27:05 literally me, the classic "it works for now" (i'm never going to fix it)
@anonimowelwiatko9811
@anonimowelwiatko9811 Жыл бұрын
if it works don't fix it
@arashiryuu1624
@arashiryuu1624 11 ай бұрын
Would it be possible to get that selection/range highlighting where the token colour gets taken in and used as the background colour in VSC?
@strager_
@strager_ 11 ай бұрын
I don't know about Visual Studio Code extensions, sorry. Maybe it's possible with a color scheme.
@skeleton_craftGaming
@skeleton_craftGaming 16 күн бұрын
I would probably name decode_UTF_8_result the same as you (I may remove the _ between the F and 8) But if it doesn't have any acronyms in it, I would just use camel case If it's truly a helper function, having it defined as a lambda inside of the function that it's helping seems better to me ... You're supposed to use std: unique 11:16 And on top of that I'm not always looking at my source control, which is why putting some sort of identifier there is a good idea
@berkaybakacak
@berkaybakacak Жыл бұрын
I don't know you, KZbin just recommended this video. And, I liked your environment
@hbobenicio
@hbobenicio 11 ай бұрын
thoughts on #pragma once? very well supported by compilers, no need for header guards matching directories and filenames (which kinda eases refactoring of file names and dirs) and avoid's the need of a tooling script to fix it.
@hbobenicio
@hbobenicio 11 ай бұрын
I'm a long time user of header guards, but recently changed to #pragma once. Just wanned to check if you see a reason for not to prefer it.
@strager_
@strager_ 11 ай бұрын
For this project, I prefer to stick to standard C++ unless there's a good reason to deviate. I don't have any good technical to avoid #pragma once.
@isodoubIet
@isodoubIet 11 ай бұрын
@@strager_ You'll intentionally invoke UB just to avoid exceptions but pragma once is too radical?
@arnabthakuria2243
@arnabthakuria2243 Ай бұрын
what font is this . Looks really good. Also great vid btw. learned a lot .
@PeteBrubaker
@PeteBrubaker 23 күн бұрын
That's like the first thing in the video, how'd you miss it? :)
@cloudstrife7083
@cloudstrife7083 Жыл бұрын
I agree with you on most of what you said what's your opinion about modern C++ and the zealot who think about doing just that and never use old c/c++ like you said earlier they think of never using normal old pointer or managing memory by hand etc I think it's starting to be a bit ridiculous etc... can't wait to see what A.I. will do to the field ...lots of devs are afraid of AI since they are just typing text in a file thinking about it
@____-pn8yb
@____-pn8yb Жыл бұрын
I prefer C, really. I like how GLib implements objects in C which can be called from C++. Also, I like the explicitness of manual malloc.
@cloudstrife7083
@cloudstrife7083 Жыл бұрын
@@____-pn8yb loool from what I understand and I read online now from C++ guru's most of the people who didn't move from C are dinosaur who didn't learn modern C++ correctly... who don't like change etc
@TheDriftingStig
@TheDriftingStig Жыл бұрын
My favorite part of this video is the Hooli shirt. Gavin Belson would be very proud. Where can I get this shirt?!
@strager_
@strager_ Жыл бұрын
It's a custom shirt. Not for resale!
@apasserby9183
@apasserby9183 Жыл бұрын
why name the type 'decode_utf_8_result' instead of 'decoded_utf_8_bytes'?. I am amateurish so may be missing something, but I think of verbs as methods ('decode') so would change to an adjective('decoded') and am not familiar with utf-8 so using 'bytes' helps me personally(not sure if it's helpful though). EDIT: understand better now, watched a bit longer and realized it's metadata of the 'decode_utf_8' method results, not the resultant bytes...
@strager_
@strager_ Жыл бұрын
> why name the type 'decode_utf_8_result' instead of 'decoded_utf_8_bytes'?. Your name makes sense, but when I see "bytes" I expect to see an array or something. I use [functionname]_result in a few other places in my code. It's a convention I picked up from somewhere and never noticed until you made me think about it.
@runtimejpp
@runtimejpp 11 ай бұрын
this is good
@MattXChrist
@MattXChrist Жыл бұрын
I looked at this code for two seconds and dipped… this is one way to give your senior software engineers a stroke
@sourestcake
@sourestcake Жыл бұрын
1:18:30 This is why i always use my own prefixed name for some questionably portable functions. So if i have to work around it, i don't have to go replace the name everywhere.
@strager_
@strager_ Жыл бұрын
For me, the problem isn't changing the codebase to use the new function. The problem is remembering to use my version in 6 months when I forgot I had to work around the bug. 🙈
@sourestcake
@sourestcake Жыл бұрын
@@strager_ One problem i have with code is that useless and outdated things don't look out of place at a glance. Sometimes i honestly leave snippets of code around because i quickly needed to go solve another problem and forgot to continue the previous one.
@Wren6991
@Wren6991 10 ай бұрын
This is good, clean code that I would be happy to work with or contribute to. Pragmatism over dogmatism, I like it!
@strager_
@strager_ 10 ай бұрын
I wouldn't call it clean! 🤣
@sourestcake
@sourestcake Жыл бұрын
At the start, the assert in the 'byte' lambda is incorrect. It's off by one.
@strager_
@strager_ Жыл бұрын
You're right! I'll fix it. Classic off-by-one error. github.com/quick-lint/quick-lint-js/commit/e9fccba1f9f842dab130f559e0562a0e40a1542a
@AlexSav
@AlexSav 2 ай бұрын
Usually C++ code is ugly. But this one is just outrageous. Make a custom execution with UB ( 1:35:26 ) and be proud of it
@isodoubIet
@isodoubIet 11 ай бұрын
The naming convention I personally favor in C++ is snake_case for everything that's not a type and Snake_case for types. This way I get the readability of snake_case with the benefits of distinguishing types from other things, like getting to use the very common pattern Foo = foo();. I hold to this even if the name contains acronyms; so a frobnicator for UTF-8 characters would be called a Utf_8_frobnicator. This avoids having to come up with unnatural names to prevent any class names ending up in ALL_CAPS, which to me looks like a macro.
@strager_
@strager_ 11 ай бұрын
Hmm, this makes sense. I might try it some day.
@fano72
@fano72 8 ай бұрын
I prefer camel case, maybe because I am a Java nerd... Hate the underscores.
@wanfuse
@wanfuse Жыл бұрын
Put jump short hash/text above the copyright, so you can quickly get to it with a shortcut key attached to a find script? Obvious I know! Huge fan!
@strager_
@strager_ Жыл бұрын
Are you saying I should put the copyright stuff in a separate file, and just link to it from the top of the file? I could do that, but if someone copy-pastes the entire file into their project, the relationship is lost.
@wanfuse
@wanfuse Жыл бұрын
@@strager_ I was suggesting that you put a comment beneath your code that you put in all your code like a signature. Like #end-5437 , then put a shortcut key in your editor attached to find feature that automatically jumps to this code bottom rather than bottom of license which is after the code, simple and probably stupid for some reason I haven’t thought of, this assumes your editor supports macros
@arijanj
@arijanj Жыл бұрын
@@wanfuse you could jump to the end and scroll back once or twice
@greyfade
@greyfade 3 ай бұрын
At 28:30, you're expressing concern about "older compilers" and `char` vs `char8_t`.... Why? You're using designated initializers, which is a C++20 feature and requires a newer compiler _anyway._
@allNicksAlreadyTaken
@allNicksAlreadyTaken Жыл бұрын
37:25 If I feel tempted to nest ternary operators, I turn it into an immediately invoked lambda instead. Honestly also in a significant part because clang-format poops its pants consistently otherwise, as you have demonstrated.
@strager_
@strager_ Жыл бұрын
Hmm, I don't know how I feel about it. I think immediately invoked lambda expressions are kind of ugly in C++, and that outweighs the ugliness of the conditional operators. What do you think? github.com/quick-lint/quick-lint-js/commit/463ca5ee1d4093221a0718ea0ebb9190041a5840
@isodoubIet
@isodoubIet 11 ай бұрын
@@strager_ One thing you can do when clangformat craps its pants is to use empty // to add newlines in the appropriate places. It's ugly still, but better than bad formatting, and way better than /* clang-format off */ nonsense. I found most instances of bad formatting could be turned into something acceptable with this (not sure there's much it can help re nested ternaries though)
@ArriEllieJelly
@ArriEllieJelly Жыл бұрын
Holy hell
@LorenMLang
@LorenMLang Жыл бұрын
Why not make char8 alias unsigned char when char8_t in unavailable? That should avoid all the casting issues.
@strager_
@strager_ Жыл бұрын
String literals are const char*, not const unsigned char*. 😬 But you do bring up a good point. I could use a string operator thingy to fix this problem... 🤔
@adamodimattia
@adamodimattia Жыл бұрын
I want this t-shirt!
@strager_
@strager_ Жыл бұрын
It's MINE!
@ajakororo5366
@ajakororo5366 Жыл бұрын
what do you do for go to definition?
@strager_
@strager_ Жыл бұрын
I don't. 😭
@strager_
@strager_ Жыл бұрын
Either I know what file the definition is (in which case I open that file and search) or I use a project-wide string search (which finds more than just the definition).
@rastaarmando7058
@rastaarmando7058 Жыл бұрын
@@strager_ Bruh, that's alot of wasted time.
@strager_
@strager_ Жыл бұрын
Yup.
@flynsequeira7237
@flynsequeira7237 10 ай бұрын
Galvin Belson himself approved this code.
@codahighland
@codahighland 11 ай бұрын
I forgot about the anonymous namespace thing. I would recognize it if I saw it, I know the semantics without having to think about it... But I could be using it in my code and I tend to forget. ^^()
@gerardsk8ordie
@gerardsk8ordie 9 ай бұрын
omg I want your shirt!!!!!!!!!!!
@cicerothecrow6958
@cicerothecrow6958 Жыл бұрын
Your code will run faster if you reduce the font size...... j/k. Thanks for sharing.
@strager_
@strager_ Жыл бұрын
My code would be so fast I can't even see it!
@guyross1664
@guyross1664 Жыл бұрын
2:20 you've come such a long way "I disagree with Zifre. Any repeated license at the top is irritating for me. - strager May 10, 2009 at 19:55"
@strager_
@strager_ Жыл бұрын
What.
@guyross1664
@guyross1664 Жыл бұрын
You wrote "Any repeated license at the top is irritating for me." May 10th, 2009. I was making a joke.
@duartelucas5746
@duartelucas5746 Жыл бұрын
That shirt!
@Canonfudder
@Canonfudder 10 ай бұрын
Were do i get a hooli shirt like that? Big Gavin Belson fan here.
@strager_
@strager_ 10 ай бұрын
It's custom made by someone who is now retired. Sorry.
@Canonfudder
@Canonfudder 10 ай бұрын
@@strager_ He is not by any chance writing erotica? Thanks for the reply.
@d0jackson
@d0jackson Жыл бұрын
love that shirt.
@spikezlee
@spikezlee Жыл бұрын
all i want to know what IDE he is using ? and how is he so efficiency on the keyboard?
@strager_
@strager_ Жыл бұрын
Vim. I'm efficient after over 20 years of use and practice.
@MNbenMN
@MNbenMN 11 ай бұрын
​@@strager_ I spent a year or two getting used to vscode instead of vim... but I still sometimes type :w in my files by muscle memory and sprinkle some extra "i" characters around
@amansagar4948
@amansagar4948 Жыл бұрын
How cool is this guy, i'll get anxiety working with this large of a codebase. Btw which keyboard is that?
@magetaaaaaa
@magetaaaaaa Жыл бұрын
It would seem that he truly is a C++ guru.
@MrAbrazildo
@MrAbrazildo Жыл бұрын
1:30:15, D.R.Y. assassination? 1:38:14, does deleting 'x' content is the same as 2 identical blocks? Will it execute 'X' if it's in 'x' case?!
@strager_
@strager_ Жыл бұрын
> 1:30:15, D.R.Y. assassination? I wouldn't say "assassination". It's not that bad, really. But yes, the copy paste should be fixed. > 1:38:14, does deleting 'x' content is the same as 2 identical blocks? Will it execute 'X' if it's in 'x' case?! Yes. C++ has case fallthrough by default.
@MrAbrazildo
@MrAbrazildo Жыл бұрын
​@@strager_ I thought fallthrough would still check the case for 'X'. And shouldn't you use the [[fallthrough]] (C++17)?
@strager_
@strager_ Жыл бұрын
> I thought fallthrough would still check the case for 'X'. Nope. That doesn't make sense, because only one case can equal the switch's input. > And shouldn't you use the [[fallthrough]] (C++17)? [[fallthrough]] is unnecessary if cases are right next to each other.
@edgecrush3r
@edgecrush3r 11 ай бұрын
10x for the Hooli shirt 😊
@rankail
@rankail Жыл бұрын
Couldn't you make helper-functions static?
@rastaarmando7058
@rastaarmando7058 Жыл бұрын
A lambda that does not capture anything is stateless, so making it static is pointless.
@strager_
@strager_ Жыл бұрын
I could make certain helper functions static, yes. What advantage would that have?
@pvt.ltd.yt_industries
@pvt.ltd.yt_industries 9 ай бұрын
I lol's at the squirrel
@cyrusol
@cyrusol 11 ай бұрын
Why does your Vim logo kick the Neovim logo to the curb?
@strager_
@strager_ 11 ай бұрын
I don't need no trendy bells and whistles!
@andersama2215
@andersama2215 Жыл бұрын
I'd put the license/copyright and other initial comments after the header guard and #pragma once, reason being, if your code gets run through one of those header tools that merges everything into one header file the resulting file will likely have those comments and sections copy-pasted for each time your header file got included. It's not critical, but will likely save some kb's of comments.
@strager_
@strager_ Жыл бұрын
It sounds like those header merging tools have a bug!
@andersama2215
@andersama2215 Жыл бұрын
@@strager_ well you would think, but since that's what the #include directive is meant to do eg: treat the included file as though it were copy/pasted (unless otherwise told to do so) if the unguarded section were in fact functional in some way, like a macro or some other weird shenanigan then things could break, so I'm guessing for simplicity's sake most header merging tools don't bother. In theory it'd be a simple fix, take any file, keep track (as you'd need to) whether it's been included or not, for the first copy leave as is, for the second and on remove comments.
@DM-qm5sc
@DM-qm5sc Жыл бұрын
Computer Jesus! I found you!!!
@porky1118
@porky1118 11 ай бұрын
4:30 I always found it stupid to add the same copyright notice in every file. Even if one copies the code, they would not copy this message, would they?
@strager_
@strager_ 11 ай бұрын
Copying a file is easier than copying a file + editing out the copyright notice.
@porky1118
@porky1118 11 ай бұрын
@@strager_ When I copy code, I normally remove the comments first, then reformat, then rename variables and functions so it's easier to understand for me, or to fit my coding style better, I also do a bit of small refactors in cases where I don't like their coding style or it's difficult to understand.
@aslkdjfzxcv9779
@aslkdjfzxcv9779 Жыл бұрын
dig the shirt
@user-zl8il5ki8e
@user-zl8il5ki8e Жыл бұрын
friendly to newbie
@aingle4239
@aingle4239 10 ай бұрын
Love the shirt lol
@expert2570
@expert2570 11 ай бұрын
Long hair programmers.. are OP af
@daleemmons36
@daleemmons36 Жыл бұрын
Please tell us more about your 9:8 monitor!
@strager_
@strager_ Жыл бұрын
LG 28MQ780-B
@daleemmons36
@daleemmons36 Жыл бұрын
​@@strager_ one or two of them? How do you like it? After 3 years of WFH I finally realized I've been missing the nice 32" 16:10 I had at the office that let me keep 4 code windows open on the same screen. Nice monitors were hard to find at the beginning of the pandemic and so I've been suffering with my "temporary" 28" 16:9 setup, which just isn't enough vertical space. DualUp seems like the complete opposite direction but I may have to try it!
@strager_
@strager_ Жыл бұрын
One. I prefer to use a single monitor. Multiple monitors is a pain.
@daleemmons36
@daleemmons36 Жыл бұрын
@@strager_ ordered. This will be a fun experiment.
@daleemmons36
@daleemmons36 Жыл бұрын
@@strager_ ok, yeah, this monitor is pretty great! Thanks!
@another212shadow
@another212shadow Ай бұрын
angle brackets are for system headers. why aren't you using quotes instead?
Faster than Rust and C++: the PERFECT hash table
33:52
strager
Рет қаралды 499 М.
CppCon 2014: Mike Acton "Data-Oriented Design and C++"
1:27:46
Why? 😭 #shorts by Leisi Crazy
00:16
Leisi Crazy
Рет қаралды 22 МЛН
Did you find it?! 🤔✨✍️ #funnyart
00:11
Artistomg
Рет қаралды 111 МЛН
Can You Draw The PERFECT Circle?
00:57
Stokes Twins
Рет қаралды 69 МЛН
Add To Cart With Functionality Using HTML, CSS & JAVASCRIPT.
1:21:54
The ARM chip race is getting wild… Apple M4 unveiled
4:07
Fireship
Рет қаралды 987 М.
Big O myths busted! (Time complexity is complicated)
21:33
strager
Рет қаралды 131 М.
How to contribute to open source
14:15
strager
Рет қаралды 102 М.
Why i think C++ is better than rust
32:48
ThePrimeTime
Рет қаралды 261 М.
You don't need libraries to write a game engine in C++ | OpenGL | Devlog
2:50
how NASA writes space-proof code
6:03
Low Level Learning
Рет қаралды 2 МЛН
GTA3 Code Review: Weapons, Vehicles, Cops and Gangs
15:00
Code With Ryan
Рет қаралды 962 М.
how Google writes gorgeous C++
7:40
Low Level Learning
Рет қаралды 738 М.
How principled coders outperform the competition
11:11
Coderized
Рет қаралды 1,5 МЛН
Why spend $10.000 on a flashlight when these are $200🗿
0:12
NIGHTOPERATOR
Рет қаралды 17 МЛН
Он Отказался от БЕСПЛАТНОЙ видеокарты
0:40
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 1,9 МЛН
How about that uh?😎 #sneakers #airpods
0:13
Side Sphere
Рет қаралды 10 МЛН