How to get good at reading code?

What are some good (small) git* projects that can be used for learning purposes?
It is really difficult to find small projects that:
1. Use modern C++
2. Are small
3. Use good practices
Thank you :)
hehe... I would argue that reading bad code is the better skill to have... there is a lot more of it out there so odds are whatever you encounter won't be amazingly readable.

I don't have anything in mind that would help you, apart from advising you to read whatever small project you are interested in, and if the quality is lacking in places, that is real world code...
Last edited on
You can learn a lot right here on this forum. Read the answers from the frequent contributors. JLBorges in particular makes heavy use of the standard library and modern methods.
For good practice guidelines, have a look at https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md which describes the core guidelines for writing c++ programs.

There are also some good blogs which cover c++

https://www.fluentcpp.com/
https://www.bfilipek.com/
http://www.modernescpp.com/index.php
https://mariusbancila.ro/blog/
https://herbsutter.com/

Not quite a blog, but very useful https://isocpp.org/faq
You should also start to critique code you're looking at. I came across the following a few days ago:

1
2
3
4
5
template <typename T>
bool any_of(T const v, std::initializer_list<T> r)
{
   return std::any_of(r.begin(), r.end(), [v](int const x) { return v == x; });
}


with a test usage as (opt is an int):

1
2
3
4
5
6
	if (!any_of(opt, {1, 3, 5, 7}))
		std::cout << "Bad option!\n";
	else {
		std::cout << "Good option\n";
		// process option
	}


As it was being used, it compiles and runs OK. Yet there's a major bug in it and another issue!

Bug as in it produces wrong results? Can you give a hint without giving away the answer?
T is an arbitrary type. x is an int. So does it only work when called - as here - with int?
@lastchance. Yep.

But finding the bug wasn't really the point I was trying to make. It was to critique code you see.

The other issue is passing the function params by value (involving a copy) and not by const ref. Passing v if an int by value is OK, but what if v is of type std::string?

Last edited on
Fair enough. Not sure if the constant reference applies more to the initialiser list than to the comparator value v.

It depends what types the template function is intended to work with. I wouldn't dream of passing a simple numerical value (int, double, etc. ) by constant reference as the overhead for a reference outweighs the pass by value. But for larger classes or arrays, then yes.

You've probably observed that I don't exactly go overboard on the use of qualifiers like "const". I prefer the "get the calculation done", not "make it unbreakable" code.
Topic archived. No new replies allowed.