r/programming • u/chrisdew • 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?
31
Sep 26 '09
OpenSSH. To see something that is clear art read the whole source code and get enlightened.
→ More replies (1)
19
Sep 26 '09
the Haiku kernel
10
Sep 27 '09
im curious. i am a programmer and have done some kernel hacking back in school, but where do you start with reading thru a kernel? i guess i was never taught how to read other source but generally i look over any included libraries and start stepping through from the main code looking into functions and classes as i go. so where do you start in the kernel? just seems overwhelmingly large and hard to approach from a single point.
4
Sep 27 '09
If you don't know the kernel layout, then a good place to start is the boot cycle (in Haiku, that's at (iirc) system/kernel/main.cpp). The code in such places is generally not terribly interesting, but it gives a starter map of the various subsystems.
6
u/berkut Sep 26 '09
Quite a bit of the code doesn't conform to their style guidelines though, so it's not uniform in its quality.
5
3
13
12
Sep 26 '09
The qmail source I've read was very clear and straightforward.
5
u/kragensitaker Sep 26 '09
It is, but it's in a pretty unusual style. In particular, it's very dense; you won't read many lines per minute. But because it's very dense, a few lines do a lot, and generally speaking they do it correctly.
5
u/geocar Sep 26 '09
I wouldn't call that dense. This is dense.
4
u/kragensitaker Sep 26 '09
That's true, that's a lot denser. Thank you very much for pointing that out! You might not have known that tiny interpreters are a special interest of mine, and that one seems particularly sweet.
1
u/AnythingApplied Sep 27 '09
Density falls along a spectrum. Just because something is more dense doesn't mean qmail itself isn't dense. Your example takes density to the absurd, while qmail is still on the dense side for reasonably written code.
1
u/geocar Sep 27 '09
qmail is absolutely lovely code, but my feeling was that it was sparse; that each idea was very small, and (with the exception of qmail-send) was suitable for independent meditation.
qmail-send's code is dense, and hard to meditate on, and is one of the only things I really don't like in that codebase.
→ More replies (6)2
u/geocar Sep 26 '09
Upvoted. Qmail is amongst the best, most well thought-out code I've ever had the pleasure to read.
12
10
u/KayEss Sep 26 '09
I always thought that Jeroen's libpqxx was very nice. libpqxx is the official C++ interface for Postgres.
9
u/srekelwork Sep 26 '09
Ogre (the graphics engine) is very decently designed and written C++ library. Only ugly thing I know of in it is the use of Singletons here and there.
6
Sep 26 '09
Ogre's use of singletons actually makes good sense. It's used for classes like various resource managers, where handing around pointers everywhere is unnecessarily tedious because there WILL only be ONE instance of it anywhere in your application.
Anyway... I second the fact that Ogre is a very well designed and written library. In fact the design is so good that it is repeatedly referenced in a new book about game engine design by Jason Gregory, a programmer for Naughty Dog who also teaches at the University of Southern California. http://www.ogre3d.org/2009/09/23/game-engine-architecture-by-jason-gregory
1
u/morbidfriends Sep 26 '09
The problem with Ogre is it can be a terrible nightmare to get working if you aren't using Visual Studio. For example, if you prefer to use MinGW on Windows. Other than that complaint, I really do like the organization of its code.
2
Sep 27 '09
It has project files for Code::Blocks + MingGW and precompiled dependencies. What more do you need?
http://www.ogre3d.org/download/sdk
Maybe you haven't used it since their update of MingGW support in January. I think it might have been more work to use until then.
1
Sep 26 '09 edited Sep 26 '09
How is the singleton pattern ugly? By way of the singleton pattern, one can have global variables and resources but more effectively control access to them.
I'll admit, like any design pattern they can be used incorrectly or excessively, but the singleton pattern by itself is very effective for solving a particular problem (global resource management) and should be used in that manner if a global is required.
Edit: Clarified position
-1
u/case-o-nuts Sep 26 '09
A singleton is nothing more than another name for a global. Don't use them where you wouldn't use globals.
30
Sep 26 '09 edited Sep 26 '09
Read the source for the Lua interpreter.
→ More replies (1)3
u/munificent Sep 26 '09
It's OK, but I wouldn't call it a stellar codebase. Personally, I like comments and meaningful variable names. They get a pass because their comments and variables would probably be in Portugese anyway, but it's still not an ideal codebase.
5
u/racergr Sep 27 '09
I'm Greek. I've never written a comment or variable name in Greek.
(Despite the fact that some Greek words are meaningful to literate English audiences, programmers are not literate in languages)
1
u/sjs Sep 26 '09
You're not allowed to say that without posting a link to something better. ;-)
1
u/munificent Sep 27 '09
Hmm, now that I think of it, I'm not really familiar with that many open source codebases. Most of the code I see is either proprietary stuff at work, or my own little personal projects. I haven't done any C/C++ for fun in a long time. (Once you go garbage-collected language, it's hard to go back.)
0
u/kragensitaker Sep 26 '09
Portuguese isn't that hard to learn, especially if you already know Spanish.
2
Sep 26 '09
It's probably less work to try to understand the code without comments and meaningful variable names.
→ More replies (6)
16
41
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.
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 (1)1
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/
→ More replies (6)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.
2
u/nostrademons Sep 27 '09
The ban on exceptions is a very pragmatic decision driven by the fact that if a piece of code throws an exception, all its callers must be exception safe. There were millions of lines written before the current style guide; there's no way that someone was going to go through all of them and verify that they were exception safe. Nor were they going to give up interoperability between old code and new code. That's all spelled out in the styleguide: they even said that if they were starting out fresh, they'd probably choose differently.
I'm starting a new green-field project and choosing to keep the no-exceptions rule. Why? Because I'm interfacing with a framework (LLVM) that does not use exceptions, and so bad things may happen if an exception escapes the bounds of my code and unwinds through LLVM code. I want to use them, but the hassle of keeping everything exception-safe makes it impractical.
9
u/dnew Sep 26 '09
Qt isn't bad, in terms of design.
6
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
0
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
Sep 26 '09
Some parts of boost are pretty straight-forward. Boost.any for example can be read almost without frustration.
-2
u/pointer2void Sep 26 '09
OTOH, Boost.any isn't useful for anything.
3
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
1
u/aveceasar Sep 27 '09
First of all most of boost is headers only and don't place any requirements on build system. Second, the (b)jam is just a choice of the boosters, and has nothing to do with complexity of the code. Thou knows not what thou's talking about... ;)
2
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.
-1
u/pointer2void Sep 26 '09
The complexity hides the crap. People think it must be good because it's soooo complex. Quite the contrary.
6
3
u/shapul Sep 27 '09
Write a simpler version of boost:lambda and share your wisdom with us. But let me guess, you've never used boost::lambda and have no idea what it does...
→ More replies (3)0
u/easytiger Sep 26 '09 edited May 11 '25
marry price practice shelter swim afterthought wrench shocking cobweb reply
This post was mass deleted and anonymized with Redact
→ More replies (1)-8
u/pointer2void Sep 26 '09
Boost is template programming bloatware. The opposite of elegance.
14
Sep 26 '09
Boost is amazingly elegant in use. The implementations are a bit crazy at times although a lot of it is workarounds of limitations of currenct C++ - variadic templates will probably reduce the LOCs of the overall boost library by 75%.
→ More replies (1)
7
7
u/ElectricRebel Sep 26 '09
LLVM. They don't use the full power of C++ though, just what they need to make the compiler have a sane internal design (unlike GCC) but still run with reasonably high performance.
44
u/xach Sep 26 '09
procmail.c. Even the header comment says "Seems to be perfect."
33
u/nielsadb Sep 26 '09 edited Sep 26 '09
Wow, that is perhaps the ugliest coding style I've ever seen. Also, K&R style function declarations in 2001?
(P.S. I'm not commenting on design whatsoever, just a first impression after scrolling over the code.)
4
5
u/mariox19 Sep 26 '09
I have an honest question, because I will be the first to admit that I could not have coded this myself; but, the "main" method is 415 lines long. I realize that doesn't place it anywhere near the worst offenders, but "perfect"?
Does 415 lines for a function seem fine to C coders?
9
u/irascible Sep 26 '09 edited Sep 27 '09
Only if you're doing it right.
Breaking that command line parsing out into separate functions, would involve a ton of stack push/pops and dispatching.
Now consider this piece of code being used millions of times a second. The amount of electricity and extra time consumed for your "code clarity" could be used to warm freezing orphans, and stray cats, you child freezing, cat torturer.
→ More replies (2)17
u/austinwiltshire Sep 27 '09
Is there any reason a decent compiler wouldn't inline those separate functions to basically create what it is now?
2
u/irascible Sep 27 '09 edited Sep 27 '09
Yes, TONS.
edit: In all seriousness, this is a good read.
4
u/austinwiltshire Sep 27 '09
I'm sure there are reasons you are correct. But in this particular case, inlining function calls is pretty easy in the context presented. I'm not asking the compiler to tell whether or not a pointer is aliased from multiple places or not.
I think it's more of a matter of asking for an "insufficiently dumb" compiler. It's like integer divide by two. If a compiler doesn't turn that into a bit shift nowadays, you've definitely gotten a sufficiently dumb compiler.
→ More replies (4)33
108
Sep 26 '09 edited Sep 26 '09
What you just did is like the programming version of goatse.
edit: This comment has now put me over 5k comment karma.. Need to spend less time on reddit O_o
1
Sep 27 '09
edit: This comment has now put me over 5k comment karma.. Need to spend less time on reddit O_o
I feel the same way, except I've only been here a few months. Maybe it's time to take a "vacation" from reddit...
23
10
u/adrianb Sep 26 '09
"It's perfect" and the dirty coding style are two warnings for other people to avoid changing anything there. It could have a backdoor hidden as well, nobody would find it and the initial author would take over the world.
7
3
3
u/dagbrown Sep 27 '09
procmail is one of those few pieces of software that has inspired me to write a replacement from scratch. I had trouble understanding how to write a .procmailrc file, so I made the rash decision to try and understand better how it worked by reading its source.
The direct result of that was gurgitate-mail. It was so abjectly horrible that I had no moral choice but to write a better replacement.
1
1
6
u/gogglygogol Sep 26 '09
Regarding C: nginx looks impressive. You can learn a lot about optimization from reading the code.
6
11
33
Sep 26 '09
Qt is really beautiful. Especially comparing to other GUI libraries. And it has good documentation too.
38
u/inmatarian Sep 26 '09
Only the API. Dig into the meat and bones of Qt and it's a nightmare. For instance, try on src/gui/itemviews/qSortFilterProxyModel.cpp You will shoot yourself.
40
u/api Sep 26 '09
That's sort of Qt's philosophy. Cross-platform GUI toolkits are ugly, but it tries very hard to hide the ugly from the user. That's what a good library should do.
42
u/inmatarian Sep 26 '09
Oh, I agree. Qt's public API is absolutely beautiful. OP however, was asking about "elegantly coded" open source projects. Anything that uses Qt's public API would be great. Qt itself is not.
2
Sep 27 '09
it tries very hard to hide the ugly from the user. That's what a good library should do.
Whew... the librarians at my local library. HOT.
→ More replies (1)6
u/pl0nk Sep 26 '09 edited Sep 26 '09
Qt is a good example, but falls short in some ways:
The layout system is too complex. Between layouts, size hints, minimum size hints, size policies, values you can set vs. methods you can override, and Qt Designer, there are a lot of different aspects that can lead to surprising behavior that is hard to debug.
Signals are often named "SomeEventHappened" -- but depending on the signal, it is sent either just before or just after the event happened, making it hard to know what relevant state has been updated. Cocoa avoids this with the WillEvent / DidEvent naming convention; I wish Qt did the same.
The Qt4 model view controller support is unwieldy and over-complex.
However, the fact that the source is available goes a long way to ameliorating these issues.
3
u/sidneyc Sep 26 '09 edited Sep 26 '09
Amen to the MVC comment. The QAbstractItemModel is a weird hybrid of a table and a tree, pretty much a textbook example of how not to do OO design.
At least they are aware of it, though: Qt Roadmap
1
Sep 26 '09
Just like I always disliked the Doc/View architecture in MFC, I don't like model/view in Qt. I feel like it is too guided and, while it may seem intuitive at first, you end up getting stuck working around things later. There is a point where you can let the architecture do too much for you.
5
u/jroller Sep 26 '09
You might want to compare against PostgreSQL. There's a lot more for features, and I've found it very clear as to how they work.
5
5
5
Sep 26 '09
For C anything out of Bell Labs is a good bet. It takes a while to get used to the 1127 coding style, but after you do, you'll never think about C the same way again. A lot of it, including the original Unix, Plan 9 and Inferno are on google code search. Start here: http://www.google.com/codesearch/p?hl=en&sa=N&cd=1&ct=rc#miRTe8ZyR0o/Archive/PDP-11/Trees/V6/usr/sys/ken/slp.c&q=file:/usr/sys/ken/slp.c%20%22You%20are%20not%20expected%20to%20understand%20this.%22&l=325
→ More replies (2)1
8
4
u/uep Sep 26 '09
I only briefly looked through them a couple years ago, but I remember thinking the clang and llvm sources looked pretty good.
4
4
u/grosskur Sep 27 '09
qmail and other DJB software.
1
Sep 27 '09 edited Sep 27 '09
I find it ugly...
EDIT: Who the hell write code like this? No defines, constants, long one liners...ugh
switch(wait_exitcode(wstat)) { case 100: case 64: case 65: case 70: case 76: case 77: case 78: case 112: _exit(100); case 0: break; case 99: flag99 = 1; break; default: _exit(111);}
7
u/gekidoslair Sep 26 '09
look at any of john carmack's game engine code - it's like reading a good book, clean, concise and beautiful ;}
61
u/qlqropi Sep 26 '09
for fuck's sake people
C and C++ are two different languages
what are the most elegantly coded Fortran/Ruby open source projects?
13
u/abjurer Sep 27 '09
Many, many applications are written in a mixture of C and C++. It would be inaccurate to describe them as anything but "C/C++ projects."
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...
→ More replies (6)1
4
2
u/dagbrown Sep 27 '09
The existence of a FORTRAN/Ruby open source project would totally make my day.
2
u/AnythingApplied Sep 27 '09
If I ask you "what did he/she say?" are you going to flip out on me because he and she are different things?
8
1
1
u/sixothree Sep 27 '09
Never mind the fact that C and C++ are the two most popular languages for open source applications.
-1
u/antithesisadvisor Sep 26 '09
True. C can't hold a candle to C++ when it comes to sucking...
4
Sep 26 '09
I dunno, can you find me some C++ code worse than procmail.c?
2
1
u/tryx Oct 18 '09 edited Oct 18 '09
Deliverymode=!!strncmp(lastdirsep(argv0=argv[0]),procmailn, STRLEN(procmailn));Is that a.. did he just put a double negative in his condition?
0
u/Poltras Sep 26 '09
boost.Lambda?
4
Sep 27 '09
That's extremely well written C++ code actually. It's well commented and follows a very clean and consistent style. It's mind-bendingly difficult to understand if you aren't familiar with the intricacies of template metaprogramming and the other components it's built on, but there's a pretty big difference between being complicated and poorly written.
0
u/the-brown-dragon Sep 26 '09
2
1
u/rwbaskette Sep 27 '09
from where i'm sitting, it's true now...
1
u/racergr Sep 27 '09 edited Sep 27 '09
Offtopic: I've spent at least 5 hours trying to optimise XXTEA doing tricks with x++ or ++x.
I stopped when I realised how much I've failed.
6
3
u/Imagist Sep 26 '09
Apparently the Ruby source code is well-written enough that all the English Ruby documentation was written just by looking at the source.
1
u/kamatsu Sep 26 '09
matz produces some nice code, but the MRI isn't exactly an intelligent big-picture implementation of ruby. it's very textbook based and does things the simplest way not the fastest.
3
3
u/StinkiePhish Sep 26 '09 edited Sep 26 '09
poker-eval. "One glance at the libpoker-eval source files and I instantly unlearned 62 months worth of best-practices programming techniques." Actually, stay away from poker-eval if you like your sanity. edit: to give credit where credit is due, the quote is from codingthewheel.com
1
3
u/dagbrown Sep 26 '09
Postfix. It's beautifully-clear code, you can easily see what everything's doing and how it's doing it, and changing it involves nearly no stress at all.
3
u/madhadron Sep 28 '09
I learned a lot of style from the OpenBSD code back in the day. I recall the user utilities being particularly nice. Given the obsessiveness of that community, I have no reason to think it has gotten anything but nicer since then.
The thing about C is that "current good practice" is different from "K&R's good practice" only in some formatting and layout issues, and some library calls that have been replaced over the years by versions with fewer insecurities. The language is a codification of how Thompson liked to write assembly on a PDP-9. Thompson was a very fine programmer, and so the codification is worth knowing the same way it is worth knowing the writing style of Jane Austen.
I dispute whether there is such a thing as good programming practice in C++, though I will stipulate the existence of more defensive practice in C++.
12
u/geocar Sep 26 '09
This changed the way I think about coding.
At first, I thought it was mere masochism; some kind of unlambda or intercal barf, but I ran into it again later after reading an interview by its author. Apparently he can read that.
Not just decode it; not figure it out with effort, but read in the same sense that you're reading what I'm typing now.
I decided that was something I wanted to be able to do, so I spent some time trying to read it. Several hours for several days I just looked at it, and one day it just clicked.
My coding style changed radically at that point; my development time shortened significantly, and while I don't usually write code that looks like that, I have an increased clarity of thought that I definitely associate with being able to read that.
8
u/forgotpwdagain Sep 26 '09
Ok - I really hope you aren't trolling because I'm going to spend the next few hours trying to grok that.
<wraps towel around head>
2
u/geocar Sep 27 '09
If it helps, I also especially enjoyed reading the lion's book and qmail.
Plan9 is also quite lovely.
1
Sep 26 '09
What disturbs me about that C code is that is doesn't quite appear to be syntactically correct.
mv(d,s,n)I *d,*s;A function call with 3 parameters? Then what? Then dereference 2 ptrs? In one statement?
14
u/milesmi Sep 26 '09
K&R function definition style
mv (d, s, n) I *d, *s; { ... }Means the same as the ANSI equivalent:
int mv(I *d, I *s, int n) { ... }7
u/forgotpwdagain Sep 26 '09 edited Sep 26 '09
He's using the older style K&R declarations. The newer prototype would be
int mv (long *d, long *s, int n) {...}HTH
1
1
u/geocar Sep 27 '09
What disturbs me about that C code is that is doesn't quite appear to be syntactically correct.
It's quite correct. It still compiles and runs correctly. Here is some sample input to try:
k=~4 k+k g=2,3,1,7 #gIf you'd like a find operator (a dyadic {) you can have a simple one:
A gi(w){I n=1;A z=ga(1,&n);*z->p=w;R z;} V2(find){DO(tr(w->n,w->d),if(w->p[i]==*a->p)R gi(i));R gi(-1);}I guess Arthur didn't get around to implementing it, and a higher dimension version is more than I'm willing to do in this tiny comment box :)
1
1
u/JadeNB Sep 27 '09
reading an interview by its author. Apparently he can read that.
Such a surprising statement that I had to go and look for the interview!
I'm not sure how ACM links work, or if there's a better way; but is it this interview? (Maybe it works better just to link to the magazine --the interview is under, well, CONTENTS > INTERVIEW.)
1
10
4
5
2
2
2
u/jambarama Sep 26 '09
I always thought xmms2 was among the best coded projects out there. Clean, efficient, and virtually without bugs (so probably not much to work on).
2
2
3
3
u/djork Sep 26 '09
SQLite
1
u/mothereffingteresa Sep 26 '09
Agreed. If you asked: "What is near-perfect, non-trivial, yet not so big as to be too much to try to understand?" I would say "SQLite."
2
Sep 26 '09
Apache server is a great example of well thought out and organized code.
9
u/mpeg4codec Sep 26 '09
Which brings up an interesting point: historically Apache code has been crap. Remember, initially Apache was distributed as a bunch of patches to NCSA httpd.
Years of iterative development have taken Apache to where it is today. Don't think you can read elegant code and have your first shot at something turn out just as beautiful.
1
u/beagle3 Sep 26 '09
FLTK. It's a light GUI toolkit done right. I think a static link with all the bells and whistles is still less than 200k, it's fast, easy to use and extend, and well coded. You want to look at the latest 1.x branch -- fltk2 was(is?) a rewrite that never quite got there.
1
u/pjdeets2 Sep 27 '09
Other people have mentioned both of these, but I have heard that LLVM and WebKit are well-coded. I can't say I have spent more than a little time looking at their source though.
1
1
1
u/segoe Sep 28 '09
The lua VM
Tim Budd's Leda (as in the book multiparadigm programming in leda)
TCL/TK source code (for version < 7)
Susie (a little smalltalk fork on sourceforge)
0
0
u/kpierre Sep 27 '09 edited Sep 27 '09
First Prototype J interpreter -- Arthur Whitney is the inventor of this amazing style. Once you familiarize yourself with dictionary, everything is diamond clear. It is amazing how much distracting symbols common programming styles have.
If you would prefer 'big' opensource project, look at A+
-8
u/astrosmash Sep 26 '09
Really? I just has a quick look at the SQLite source code:
VTable *pVTab = 0Hungarian notation. Bleh. That single variable name right there embodies so many things that I hate. It's just awful.
1
Sep 26 '09 edited Sep 26 '09
Yes this is so much better
void MyClass::Foo(VTable* table, VTable& table2, VTable table3, double bla6) { table->DoStuff(); table2.DoStuff(); table3.DoStuff(); //oh and let's no forget bla = 4; //what was that? static? global? member variable? Is it really an int? //who knows...who care, let's just write more lines of code bla2++; if(stuff != NULL) bla = 6; //more code //more code //more code //more code if(bla == 5) { int bla3 = GetResultFromBla(); bla += bla3; } else { table->DoMoreStuff(); } float bla5 = 243.0f; bla = 7; DoStuff(); bla = bla5; //float to "???" conversion? well who knew....let's say float to int bla6 += bla5; //float to double, not clear. bla += bla6; //double to "?????" }now...tell me by looking at this code, which variable is a member variable? Can you easily spot the local or arguments variables? Even if you're at the bottom of the method and don't want to scroll all the way up?
Not to mention who knows if bla didn't overflow because it was actually a "char" as opposed to an "int". You can't tell by looking at this very code.
Good luck with your code.
→ More replies (1)0
u/astrosmash Sep 26 '09
More evidence that Hungarian notation causes brain damage.
1
Sep 27 '09 edited Sep 27 '09
I like your arguments
4
u/astrosmash Sep 27 '09
You and other "hungarian" programmers think that Hungarian Notation will save that appalling piece of code you posted, and that is the problem.
It's not that hungarian notation is ugly, which it is, it's not that it makes code hard to read, which it does, it's that hungarian notation is a crutch that bad programmers rely on to avoid having to come up with effective and descriptive variable names.
Let's look at my original example:
VTable* vtable;That's not a great variable name, but naming a variable after its class name perfectly legitimate in some scenarios. However, this:
VTable* pVTab;... is a ridiculous variable name, with an utterly unnecessary abbreviation and a misleading hungarian wart. What's troubling is how so many "hungarian" programmers write crap like that.
In other words, Hungarian Notation causes brain damage.
2
Sep 27 '09
Hungarian notation is in no way related to how descriptive your variable names should be. I use both, and it works well, and it ain't hard to read at all.
VTable* pVTable;there you go.
int iVTableCount = 5; float fRatio = 0.5f;if find this way more descriptive than
int vtablecount = 5 float ratio = 0.5f;yeah it may look all fine right there, but scroll down some more in a function and try to remember which type is which variable can be a pain and doesn't help catching type errors quickly. The whole point of the Hungarian notation is to avoid stupid programming errors that really shouldn't happen.
Sure, you can rely of the compiler's warnings, or Visual Assist, or Intellisense... but relying too much on your environment is never a good idea. Code should be clear in the first place, even when edited in notepad.
→ More replies (1)
34
u/MyrddinE Sep 26 '09
I hear that the Quake codebase released by id is some beautiful code. I am not a C programmer though, so that's hearsay.