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?

145 Upvotes

292 comments sorted by

View all comments

Show parent comment

35

u/pbiggar Sep 26 '09

I'd listen if Fortran was a very-close-to-perfect subset of Ruby.

36

u/qlqropi Sep 27 '09

But good C code is not a subset of good C++ code.

13

u/pbiggar Sep 27 '09

I agree.

But it makes sense to ask for C/C++, if you're interested in how people write programs well in systems programming languages.

10

u/qlqropi Sep 27 '09

C++ isn't a good systems programming language, mostly due to its complex and completely unstandardized ABI.

Sure, due to its heritage C++ has a good C foreign-function interface, but so do a dozen other languages.

5

u/aveceasar Sep 27 '09

C++ isn't a good systems programming language

Jean-Louis Gassée would disagree...

3

u/ooffoo Sep 27 '09 edited Sep 27 '09

Given the failure of BeOS I'm not sure that's a great reference for C++ being a good systems language.

C++ operating system API's cause issues when new versions of the OS come out. Adding virtual methods to OS classes will break application's that used the original class version. So new OS updates will require application recompiles to work.

BeOS worked around this IIRC by having a bunch of dummy 'reserved' virtual functions in each OS class that did nothing in case they had to add features later. Not very elegant.

17

u/aveceasar Sep 27 '09

Given the failure of BeOS

Now, that's loaded word - "failure"... if you evaluate software solely on its commercial success, MS Windows has to be the greatest software ever... ;)

Adding virtual methods to OS classes will break application's that used the original class version.

So, don't add... if you need to add features, derive...

2

u/[deleted] Sep 27 '09

BeOS is still alive and kicking. http://www.haiku-os.org/ In fact they just had an alpha release as somebody finally got GCC4.3.3 bootstrapped on their OS.

In answer to the question tho: C is a very different beast than C++. C is a procedural language while, C++ is object oriented AND procedural. So best practices in C contradict with best practices in C++. However, learning to program well in a particular languages lends to a firm grasp on the fundamentals, so as someone has probably pointed out: http://www.parashift.com/c++-faq-lite/index.html is your first stop.

2

u/ooffoo Sep 27 '09

So, don't add... if you need to add features, derive...

This usually is not possible when releasing new versions of operating systems. If you want to add new functionality that all Window objects can use then you want to add the functionality to the base Window object. Adding a new derived class is no good since existing applications don't derive from that new class.

What usually happens in C++ projects is they implement some other object system on top of the C++ one. Often a COM style, or factory driven approach.

I used the word 'failure' in the sense of proving C++ as a viable language for operating systems. BeOS went through at least two releases which required applications to be rebuilt from source. This killed the application user base and made it hard to consider it as a realistic target for applications.

BeOS 4.5 and on was better in that they were more careful about breaking backwards compatibility.

0

u/aveceasar Sep 27 '09 edited Sep 27 '09

If you want to add new functionality that all Window objects can use then you want to add the functionality to the base Window object.

If the application wants to use new functionality, it has to be recompiled anyway...

EDIT: ok, I wasn't really paying attention... <sheepish grin/> I see what you mean...

1

u/GNUGeist Sep 27 '09

I think this thread will agree:

BeOS is a good C++ example that would answer the proggit (or whatever they call it).

1

u/sebnow Oct 19 '09

C++ is not a superset of C. Your comparison is invalid.

0

u/pbiggar Oct 19 '09

Of course its not a perfect superset of C. Its a "very-close-to-perfect superset of C". Though I suppose my comment could be read as "a superset of C which is close to perfect", but that would be ridiculous.

1

u/scottious Oct 19 '09

It's not even a superset. C has some features that C++ does not have. They are two distinct languages which happen to have a similar syntax.

1

u/pbiggar Oct 19 '09

They are two distinct languages which happen to have a similar syntax.

That, my friend, is ludicrous. The major major controlling influence on the design of C++ was to keep it source compatible with C (citation: Design and Evolution of C++, Stroustrup). Obviously, they deviated a little bit, but many large C programs are compilable with a C++ compiler (example: GCC 4.5).

1

u/scottious Oct 21 '09

I don't have GCC 4.5, but I remember writing a c program that was ridiculously easy and having it not compile with the GCC C++ compiler. It was something like this:

enum a { F1=0x1, F2=0x2 }; int main() { int b = F1 | F2; }

I know that the languages are meant to be similar but I know that all the C code I work on for my job won't have a chance in hell of compiling on a C++ compiler. It'd definitely need some porting (albeit probably not a whole lot)

1

u/pbiggar Oct 21 '09

There are differences. In your example, C++ doesn't allow you do bitwise stuff on enums. But if you think the languages are only incidentally similar languages, you are many times wrong.

1

u/scottious Oct 21 '09

Fair enough.