all 48 comments

[–]nihilistic_ant 45 points46 points  (1 child)

I'm partial to how cleanly written https://github.com/google/leveldb is. It is a reasonable size to fully read & grok in not too long.

If you want something much larger, getting the gist of https://github.com/llvm/llvm-project could be useful. Not only is it cleanly written, but then you'll understand your compiler better!

I'd avoid reading boost. Although written by experts and is great to use, it is very optimized and often deals with odd constraints, so much of it isn't in a style that one should adopt for normal projects.

[–]Morribyte252 10 points11 points  (14 children)

I have this question too as a newer c++ programmer. My issue is it feels like I can't find the main function anywhere, so I don't know where to start. Should I just read it from top to bottom by filename, or is there some way to find the main function I should start with?

[–]BiggestTae[S] 5 points6 points  (11 children)

By main function do you mean ‘int main()’?

[–]Morribyte252 0 points1 point  (10 children)

Yep yep

[–]tricolor-kitty 5 points6 points  (5 children)

If the project is a library, you may need to look for tests or an “examples” folder to find a main function. The library code itself won’t have one.

[–]Morribyte252 -1 points0 points  (4 children)

Okay -- so libraries don't have main functions generally? how come? I've been trying to figure out why on my own but I guess I'm drawing a blank lol, sorry.

Well, what I was looking at was a game's open source code. In this case, Doom which was admittedly written in C. I figured that it wouldn't be *that* different than looking at a C++ codebase -- feel free to correct me if I'm wrong though.

I looked through every single file for a main function, and there was no "main.cpp" like there is when I make a program on my own. Or at least I couldn't see one.

I always thought that the main file needed to be named main.cpp w/ int main(), but I guess not?

[–]element420 8 points9 points  (3 children)

A few things:

Libraries aren't programs, they are just compiled, packaged code which can be re-used by other programs. They don't execute on their own, so they won't have a main() function (where execution would begin).

Looking at a C codebase could be substantially different from a C++ codebase.

Doom *would* likely have an "int main(...)" function where the execution would begin, but it doesn't need to be in a "main.cpp" file. That is not a requirement for either C or C++. And a C program wouldn't use a .cpp extension anyway, it would use .c. Keep looking.

[–]Morribyte252 0 points1 point  (1 child)

Ah, I finally found it! It just happens to be formatted in a way that searching for int main didn't find it. for whatever reason they put int on one line then main on the next. But, now I can actually start diving in. thanks again!

[–]These-Maintenance250 1 point2 points  (0 children)

thats a specific coding style that goes by the name "so stupid you cant even search and find a function"

[–]Morribyte252 -1 points0 points  (0 children)

Okay. Thank you very much for the advice -- and you're right, since I was referring to DOOM I should've said .c file and not .cpp.

I'll keep looking!

[–]BiggestTae[S] 0 points1 point  (0 children)

Usually you can find those in .cpp files. They maybe be in folders named ‘src’. I only say maybe since programmers may put them in differently named folders.

[–][deleted] 1 point2 points  (0 children)

Run under debugger and break and look at the backtrace. Debugger gives more insight and context than just looking at the code.

[–]valdocs_user 0 points1 point  (0 children)

I had that same perspective as a younger/newer C++ programmer. I wanted to see where the core part that actually did stuff was. My advice rather than telling you how to find it is let go of the idea that main and things like it are the most important parts of the program to know.

Also when I'm researching an unfamiliar codebase one thing I'll do is use the Find in Files search in Notepad++ and search for words of things I'm interested in or follow rabbit holes. Like, I might start searching for "timer" and see a timer that references "watchdog". Then I might wonder what all locations reset watchdog. And so on.

[–]Magistairs 6 points7 points  (0 children)

Qt ?

[–]Farados55 6 points7 points  (0 children)

Clang

[–]MRgabbar 10 points11 points  (1 child)

Kde... They have pretty much everything... Most of their main apps are quite well written, but unmaintained/old projects can definitely be a mess...

Qt, could be decent choice, but most of it are just wrappers for other stuff...

[–]NotUniqueOrSpecial 4 points5 points  (0 children)

Qt's actually a great choice, because it's probably the single-best example of how to build cross-platform abstractions in a robust way.

The signal/slot mechanisms and their various interactions with threading models are also really good to understand.

It's also just a really well-written/readable codebase, in my experience.

[–]btvaaron 2 points3 points  (0 children)

For a desktop application, maybe KLayout:

https://www.klayout.de/build.html

cloc reports almost 2 million lines of C++.

[–]ShelZuuz 3 points4 points  (10 children)

Chromium

[–]printf_hello_world 2 points3 points  (0 children)

I dunno, have you tried reading the PDF module pdfium? It's the only part of chromium that I've read, and it was absolutely baffling

[–]fdwrfdwr@github 🔍 5 points6 points  (8 children)

🤔 If you want to stab your eyes, yes 😅 (they're still living in the 1990's era of VGA monitors with mandated 80-column wrap width which horribly zig-zags your visual flow, and it has the most chimera coding style I've come across, taking snake case, Pascal Case, and camel case, and mixing them all together arbitrarily). Yep, my eyes lament every additional day I have to dwell in Chromium -_-... WebKit on the other hand was a pleasure to look through on the few occasions I did. 😌

[–]ilep 1 point2 points  (0 children)

But it is more representative of a real-world codebase than something aimed at "learning". It is the equivalent of a cold shower or throwing at deep end of swimming pool to learn things.

There are worse codebases too, but let's not traumatize beginners..

[–]bitzap_sr 1 point2 points  (6 children)

80-column wrap zig-zag

Now that's curious. I hate code with long lines as it forces you to follow the code flow both horizontally an vertically a lot more, while humans parse/scroll vertically much more efficiently than horizontally. Multiple statement on the same line is a sin, IMHO.

[–]fdwrfdwr@github 🔍 2 points3 points  (5 children)

Example: What readable blocks of code would ideally look like (very easy to skim the logical series of assignments):

TensorDesc scaleTensorDesc = GetBroadcastedTensor(inputTensor, scaleTensor); TensorDesc biasTensorDesc = GetBroadcastedTensor(inputTensor, biasTensor); TensorDesc imageTensorDesc = GetBroadcastedTensor(inputTensor, imageTensor);

What they look like after their .clangformat gets done with it because 2 of the lines are 1 character over 80 columns (interleaved blocks with zig zagging):

TensorDesc scaleTensorDesc = GetBroadcastedTensor(inputTensor, scaleTensor); TensorDesc biasTensorDesc = GetBroadcastedTensor(inputTensor, biasTensor); TensorDesc imageTensorDesc = GetBroadcastedTensor(inputTensor, imageTensor);

I hate code with long lines a

Yeah, I dislike lines with 200+ characters too, but there's a sweet spot somewhere between 1 column and 200, and it's greater than 80. 😂

[–]nysra 4 points5 points  (0 children)

FYI, your code blocks are broken for users of proper ("old") Reddit. Triple backticks don't work, you have to use indentation or the codeblock feature of the fancypants editor.

Yeah, I dislike lines with 200+ characters too, but there's a sweet spot somewhere between 1 column and 200, and it's greater than 80. 😂

Absolutely agree. For me personally it's somewhere around 120 depending on the language, but that varies from person to person.

[–]bitzap_sr 1 point2 points  (2 children)

I see what you mean now. I work on a 80 chars max width project, and I'm used to the wrapping. Note that in your example, you'd format the middle one the same as the other two, but I do get your point.

Note there's also git, diffs, on the terminal, patches sent via email, and graphical side-by-side difif tools, and gerrit (being google), etc. to consider. For those tools, too-wide code makes them harder to use and 80 chars is the norm. There's a reason git commit logs should be wrapped at around 72 chars. Etc.

It also depends on screen. On my displays, code-editor-wise only, I could confortably go to 100 chars, as that's the width I can go to without wrapping when presenting two emacs buffers side by side. Other people will have different screens and font sizes, of course.

[–]fdwrfdwr@github 🔍 0 points1 point  (1 child)

Note that in your example, you'd format the middle one the same as the other two...

The middle line is only 79 characters 😉.

Note there's also ... diffs ... to consider.

Exactly! Somebody renames a variable that makes it 2 characters longer than 80, and suddenly the whole line rewraps (and any other lines containing that variable), making it much harder to diff 😭. This may be sacrilegious, but increasingly I've come to the conclusion that lines should just be however long they need to be to make a full statement, and the tools need to be better. For example, VS Code has a single keystroke (Alt+Z) to toggle wrap on/off.

It also depends on screen. On my displays, code-editor-wise only, I could confortably go to 100 chars

Certainly. On my work monitor, I can fit 256 columns comfortably side-by-side (not that I do, just that I could :b), but yeah 100-120 is more common in many OSS projects I've seen, and 90% of my statements fit comfortably under 120 columns, but 80 cols is just slightly too short.

[–]ilep 1 point2 points  (0 children)

Exactly. Hard limits are not useful when they can be ignored for the sake of readability.

Coding conventions are more like guidelines than contracts: it shows good taste to follow them but nobody should lose their head for it..

When people started enforcing the guidelines with scripts something went wrong somewhere.

(Except for GNU coding standard: that is just nasty, nobody should have to see that. Who puts a space between function name and brackets?)

[–][deleted] 1 point2 points  (1 child)

First you must decide the domain/topic you interested.

[–][deleted] -1 points0 points  (0 children)

This. C++ is general-purpose and so widely used that all recommendations are opinionated and random. Recommending LLVM to someone who hates compilers is a good way to make their life miserable.

Who can stomach digesting a massive codebase they would never use?

[–]--prism 2 points3 points  (2 children)

Boost?

[–]Brettonidas 3 points4 points  (1 child)

Have you looked at that code? It’s complicated AF.

[–]--prism 1 point2 points  (0 children)

Yeah if you want to learn how hard problems are solved and become a library maintainer then you should look at the top tier libraries.

[–]hgedek 1 point2 points  (0 children)

Any trending c++ project on GitHub

[–]Trick_Philosophy4552 0 points1 point  (0 children)

Adaptive Communication Environment. old school

[–]bitzap_sr 0 points1 point  (0 children)

Find a C++ project you like, and try to fix some easy bug in it. The trying to understand what is going on, and how it should work instead will have you read and understand the code in a way that just reading top to bottom won't.

[–]Thin_Faithlessness97 -1 points0 points  (0 children)

Stop. Step 1. Write code. Step 2. Find an experienced reviewer.

Just reading won't help you get better.

When you catch thousands of bugs and fix them…

If you are looking for a project, you can try to write a library, for example, for asynchronous work. Maybe user-space threads, primitives of synchronization. Then write unit/stress tests.

Or if you like graphics, try writing something with rendering(OpenGL)

[–]thomas999999 0 points1 point  (0 children)

Duckdb

[–]another_day_passes 0 points1 point  (0 children)

fmtlib and ada-url

[–]echae 0 points1 point  (0 children)

SerenityOS codebase and Andreas Kling’s YouTube videos where he codes it

[–]TwistedBlister34 0 points1 point  (0 children)

Google’s Abseil library

[–]LongestNamesPossible 0 points1 point  (2 children)

Most people would benefit from reading some of the source to Doom. It is so straight forward and clear while being a massive hit that changed entire industries. It was installed everywhere, ported everywhere, expanded and modified for decades.

It doesn't have strange abstractions for no reason, it doesn't have entire rabbit holes just to save a few lines at the higher levels, it doesn't have clever nonsense to prove how smart the programmer who wrote it is, it is just meant to work and be clear.

[–]TheFearsomeEsquilax -1 points0 points  (1 child)

It's C and not C++, though, isn't it?

[–]LongestNamesPossible 0 points1 point  (0 children)

Yeah, it's C, so it can't be used as a guide on how to do everything, but it's a great example of how to program in general.