r/programming • • Sep 26 '09

Ask Proggit: What are the most elegantly coded C/C++ open source projects?

I've recently been reading (parts of) the source for sqlite3 and found it to be a revelation in good programming practise.

What other C/C++ open source projects (of any size) would you recommend that I look at, in order to get an idea of current good practise?

144 Upvotes

292 comments sorted by

View all comments

41

u/[deleted] Sep 26 '09 edited Sep 26 '09

Do read:

Apache portable library

Minix 3

But whatever you do, don't read boost. Unless you are a sad masochist.

I tried reading boost.lambda code, and almost ripped out my eyes.

30

u/api Sep 26 '09

Boost is a mixed bag. Some parts of it are excellent, while other parts are classic examples of over-engineering and unnecessary language acrobatics.

10

u/Inverter Sep 26 '09

Exactly, Boost is not a single library, it's a mix of various things, of various quality, by various people, and, no pun intended, of various genericity.

(For example, many programs can use the shared pointers, or the operators, but only very specific programs will use the boost graph library, or the quaternions...)

5

u/api Sep 26 '09 edited Sep 26 '09

It's also not monolithic, meaning that you don't have to bloat your app's binary with parts you don't use. That's nice.

boost::asio has a bit of a learning curve, but it's truly awesome: write-once-build-everywhere IPv4 and IPv6 network code using a very high performance event-driven paradigm.

→ More replies (6)

4

u/pl0nk Sep 26 '09

Yes to both. Regardless of what you end up thinking of boost, you will absolutely learn by reading it. boost::any and shared_ptr are good small bits to start with.

13

u/KhakiLord Sep 26 '09

Those are all C projects, where are all the C++ projects with practical yet elegantly laid-out object hierarchies that we can all learn from.

0

u/[deleted] Sep 26 '09

[deleted] — view deleted comment

4

u/[deleted] Sep 26 '09

Poor C++. It's paid out a lot of hours, at least.

9

u/eric_t Sep 26 '09

I've heard good things about the Chrome code base. Google's C++ style guide looks very reasonable at least.

6

u/alecco Sep 26 '09

V8 is OK.

2

u/doubtingthomas Sep 26 '09

Skimming through V8, I can't speak for its high-level design, but it is one of the more readable bits of C++ I've seen, especially for what it does.

→ More replies (3)

2

u/elviin Sep 26 '09

The Google style guide abandons some things I regard useful if used properly:

  • default parametres
  • using directive
  • exceptions
  • multiple implementation inheritance
  • operator overloading

Before anyone sticks with a C++ style, I recommend to read http://www.parashift.com/c++-faq-lite/

3

u/reddit_clone Sep 27 '09 edited Sep 27 '09

I can actually understand

  • default parameters : surprises..
  • using : namespace pollution
  • operator overloading : surprises, hidden behaviour..

But exceptions?

It is not C++ without exceptions. Checking for return values during 'happy path' is not fun.

→ More replies (2)
→ More replies (8)

9

u/dnew Sep 26 '09

Qt isn't bad, in terms of design.

6

u/[deleted] Sep 26 '09

It's actually really great. Every time I think I need to figure out something with Qt, I always find the solution simple, elegant, and extensible.

22

u/KenziDelX Sep 26 '09

When it comes to C++ style and code that can be learned from, I think there's a tension between code that is easy to reuse and code that is easy to read. Code that is easy to reuse often has tons and tons and tons of indirection and interfaces. Interfaces make things easier to read if you don't have to understand what happens underneath them - in fact, that's a lot of their point. They make things much, much harder to read if you do have to understand what happens beneath them, particularly when nested (because then they just function as a tower of gotos). I think error checking has much the same impact - it makes code easier to use as a black box, with the trade-off that the actual logic of internals of the code is often obscured by error handling.

I think a lot of good C projects don't, in general, try to make their individual components reusable, because they're so context specific (I mentioned my fondness for Quake below - this is definitely true of that code base). The best C code tends, I think, to be hyper conscious about interfaces, and the act of exposing interfaces tends to be an extremely intentional, specific, planned act. It is not a default.

I spent a year evaluating Open Source game engines for the U.S. military, and many of them were written in much more recent C++ OO idioms, and one thing I found to be absolutely the case was that, even for the good code bases, most of them were borderline unreadable without breakpoints and a stepping debugger because they jump around from class to class to class so much, and tiny method to tiny method to tiny method. And this wasn't just because of inheritance... it was just the practice of making all the internal, tightly-coupled components written as though they were all totally decoupled and self-sufficient.

That's been my experience, anyway.

8

u/dnew Sep 26 '09

and tiny method to tiny method to tiny method.

For what it's worth, that's called not spaghetti code, but ravioli code. :-)

5

u/gameforge Sep 26 '09

id had this novel concept where, instead of making the code reusable, they made the software reusable instead. A few generations of technology "id tech level x" spawned hundreds of games.

A much more extreme example of this, in my mind, was the Duke Nukem and Build source code; that shit is written like assembly language... variable names are letters of the alphabet, while functions are tightly compacted spanning thousands of lines. Yet the Build engine, in its day, was more reused than any other engine of the time.

2

u/KenziDelX Sep 26 '09

Yep. I guess it's just the difference between black box reuse and white box reuse (?). If you try to reuse any of the Quake engines with the expectation that you are going to build on existing tested, finished code, and that you won't need to read and modify the original code, the range of what you can achieve is pretty limited. The code isn't built for that. But because it's so lean, you can make pretty sweeping changes without it actively fighting you - the relative lack of interfaces and indirection and constraint enforcement means that larger scale functionality tends to be much localized and clumped together.

3

u/al-khanji Sep 27 '09

Look at Qt. I find it to be very well written.

13

u/austinwiltshire Sep 26 '09

In boost's defense, some of the things they are doing in a general, reusable and clean (interface) way are almost practically impossible with elegant code.

7

u/[deleted] Sep 26 '09

Some parts of boost are pretty straight-forward. Boost.any for example can be read almost without frustration.

→ More replies (4)

7

u/antithesisadvisor Sep 26 '09

Any library that's so complex that it requires its own make replacement (i.e., bjam) is too fucking complicated.

5

u/[deleted] Sep 27 '09

Oh how I hate bjam.

→ More replies (15)

3

u/shapul Sep 26 '09

It's true that some of the boost libraries are among most complext C++ libraries out there. However, they are complex so that any code that use them can be simple and elegent! Boost::lambda for example is extremely easy to use and elegent but its code is very complex. There are certian techniques that are important to use for library designers but probably do not have much use for application programmers and wider audience. I believe GP was asking about application programs not libraries.

→ More replies (6)
→ More replies (5)