Ask YC: Could you point to examples of great and terrible code

20 points by iamelgringo ↗ HN
I have been a bit isolated and self-taught for my CS education (online at University of Maryland). One of the holes in my education is that I feel like I've missed out by not working with and looking at other people's code. I could use some examples of what people consider to be great code and what's terrible code.

I'd love to see what other people think is great code and/or terrible code. If you have access to code that you think is great or terrible and is freely available online, I'd love to see it.

24 comments

[ 3.1 ms ] story [ 61.0 ms ] thread
Start out by looking at open source projects... Linux kernel if you're into low level systems programming, Mono if you're into higher level systems programming, the countless web apps out there in your favorite language, etc...

The bigger the project the better because more people have looked at it and offered suggestions.

Look for great code, terrible code is everywhere.

Note: Use common sense, if you want to look at really good code, look at code in a critical part of a system... I bet that Linux's memory management routines are really interesting to see. Look at those, make a class diagram out of them (or entity-relationship diagram), and look at the design. Good code is borne out of good design. If the design is good, chances are the code is equally good and vice versa.

Why do I cite Mono? It's not becuase I am a Microsoft nut, but because Mono is a re-implementation of MS code. Mono people had a great reference for design, which allowed them to focus only on writing good code. What I have seen of Mono's implementation is really good code.

Good ideas, thanks. I'll look into the Mono libraries.
Another thing... Beside looking at good code, buy some books. Code Complete is excellent. Writing Solid Code another... They have the bonus of not only showing you good code, but telling you WHY it's good code.
David R. Hanson has a book called "C Interfaces and Implementations" that walks you through his C library (that in part replicates the standard library). If your're a novice (or a novice at C) you can learn a lot. The code is free to download.
libcairo (the new graphics layer for linux) has some amazingly well written and well maintained code.
Look at Wordpress (wordpress.org). Their code makes me want to throw up, yet it presents itself quite nicely to the end user.
Agreed. In fact, most PHP applications are full of nasties. Imho, that's because PHP is a language that makes it easy to write bad code. There is, of course, good PHP code too, but the majority is really quite bad - even when you look at leading projects like Wordpress.

I switched my blog over to typo because I just couldn't stand working with the writhing mass of spaghetti that is Wordpress (once you get under the hood, anyway).

Chad Fowler and Bruce Williams came up with http://slickorslack.com/ which presents code snippets where people vote them 'slick' or 'slack'.
The site has great potential... but it's essentially worthless as is. Several reasons:

1) Only short code snippets. It does not take much to write a brilliant 10 lines of code. Give me at least code for a well-designed class.

2) No comments. Of the top snippets, I only saw one that had comments.

So it's kinda useless to go to that site. The idea's really good though, lots of unrealized potential.

If you want to see some code abominations (with a healthy dose of ridicule and humor) check out http://thedailywtf.com/ Seriously, some of the worst code I've ever seen.
While thedailywtf.com is an excellent source of entertainment, I think it's more useful to study code that compiles, looks, and runs fine, but has some less-obvious deficiency like an XSS vulnerability, or poorly chosen algorithm (e.g. bubble sort). I like to skim through "best practices" books whenever I'm learning a new language or technology.
Great Code:

1) The code in the book "Paradigms of Artificial Intelligence Programming" by Peter Norvig. (on the surface this is a "good lsp style book" but in my opinion you can learn a lot about writing good code in any language from this book)

2) I second David Hanson's "C interfaces and implementations", reccomended by Maro in his reply.

3)The code for the HotDraw framework (smalltalk and java)

In terms of simplicity and architecture I think Ikarus Scheme and Rubinius are good reads. Both are compilers/VMs that have been expressed in their most beautiful form.
Kernighan and Plauger's _The Elements of Programming Style_ covers exactly this ground, showing in turn poor code and how it could be better. They use real-life bits of code they come across, not artificial constructions. Kernighan and Pike's later _The Practice of Programming_ also partially covers this. Both are must-reads. http://troff.org/pubs.html#style and http://troff.org/pubs.html#tpop
The source of Lua [http://www.lua.org/download.html] is extremely excellent. It's in super-portable strict ANSI C and is quite impressive in its ability to run pretty much anywhere. It's also very readable.

It's very instructive to read the source of the parser and the code generator for the VM. It's well commented and it just flows.