News for hackers. Stuff that matters.

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

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 | 20 points | on HN )
Re:Ask YC: Could you point to examples of great and terribl... by bootload on Monday June 23, 2008 @05:42PM (#225626)
Good: Quake - cross platform, fast, clean & easy to read ~ http://www.idsoftware.com/business/techdownloads/

Bad: Sendmail - it works but is continually plagued with bugs ~ http://www.outpost9.com/exploits/sendmail.html

Re:Ask YC: Could you point to examples of great and terribl... by iamelgringo on Monday June 23, 2008 @07:30PM (#225727)
That's what I was looking for. Thanks.
Re:Ask YC: Could you point to examples of great and terribl... by gm on Monday June 23, 2008 @05:38PM (#225624)
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.

Re:Ask YC: Could you point to examples of great and terribl... by gm on Monday June 23, 2008 @05:50PM (#225638)
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.

Re:Ask YC: Could you point to examples of great and terribl... by iamelgringo on Monday June 23, 2008 @08:22PM (#225760)
Good ideas, thanks. I'll look into the Mono libraries.
Re:Ask YC: Could you point to examples of great and terribl... by Maro on Monday June 23, 2008 @05:55PM (#225647)
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.
Re:Ask YC: Could you point to examples of great and terribl... by chrisl99 on Monday June 23, 2008 @09:38PM (#225816)
Look at Wordpress (wordpress.org). Their code makes me want to throw up, yet it presents itself quite nicely to the end user.
Re:Ask YC: Could you point to examples of great and terribl... by swombat on Tuesday June 24, 2008 @04:52AM (#226026)
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).

Re:Ask YC: Could you point to examples of great and terribl... by mleonhard on Tuesday June 24, 2008 @07:26AM (#226081)
PHP and Perl
Re:Ask YC: Could you point to examples of great and terribl... by jackhack on Monday June 23, 2008 @11:39PM (#225891)
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.
Re:Ask YC: Could you point to examples of great and terribl... by chengmi on Tuesday June 24, 2008 @02:02AM (#225957)
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.
Re:Ask YC: Could you point to examples of great and terribl... by gm on Monday June 23, 2008 @05:54PM (#225645)
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.
Re:Ask YC: Could you point to examples of great and terribl... by ralph on Tuesday June 24, 2008 @06:07AM (#226061)
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
Re:Ask YC: Could you point to examples of great and terribl... by DaniFong on Monday June 23, 2008 @08:50PM (#225780)
A set of examples of great code: Almost everything written by Petr, in Topcoder Competitions, though it is instructive to see how his coding style evolved over time.

Here's an article on him, by the way. http://www.topcoder.com/tc?module=Static&d1=features&...

Re:Ask YC: Could you point to examples of great and terribl... by plinkplonk on Tuesday June 24, 2008 @12:55AM (#225928)
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)

Re:Ask YC: Could you point to examples of great and terribl... by LogicHoleFlaw on Tuesday June 24, 2008 @11:12AM (#226256)
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.

Re:Ask YC: Could you point to examples of great and terribl... by ashu on Monday June 23, 2008 @06:09PM (#225673)
libcairo (the new graphics layer for linux) has some amazingly well written and well maintained code.
Re:Ask YC: Could you point to examples of great and terribl... by cawel on Monday June 23, 2008 @10:11PM (#225839)
Chad Fowler and Bruce Williams came up with http://slickorslack.com/ which presents code snippets where people vote them 'slick' or 'slack'.
Re:Ask YC: Could you point to examples of great and terribl... by gm on Tuesday June 24, 2008 @11:10AM (#226252)
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.

Re:Ask YC: Could you point to examples of great and terribl... by ynd on Tuesday June 24, 2008 @01:00AM (#225932)
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.
Re:Ask YC: Could you point to examples of great and terribl... by hsuresh on Monday June 23, 2008 @11:24PM (#225884)
Good code: http://fit.c2.com/ Bad code: Just look around. Easy to find.
Re:Ask YC: Could you point to examples of great and terribl... by DanHulton on Tuesday June 24, 2008 @09:36AM (#226142)
Bad code: OSCommerce
Re:Ask YC: Could you point to examples of great and terribl... by dangoldin on Monday June 23, 2008 @06:12PM (#225676)
Do you have a particular language in mind?
Re:Ask YC: Could you point to examples of great and terribl... by iamelgringo on Monday June 23, 2008 @07:28PM (#225725)
Dealer's choice.