I really advocate folks reading as much source as they can because you become a better writer by reading as much as writing. That's the whole point of the Weekly Source Code - reading code to be a better developer.
Reading code in Open Source projects is a good way to learn, especially if the project has been around a while and been successful, or if you already respect the team of people working on it. Less reliably, you can find snippets of code by searching and sharing code.
There's basically three players in the "Code Search Engine" space. These are the ones I bump into all the time. There could be others.
Searching the web is 99% free text search, as you know. Every once in a while, I'll use an advanced technique like searching with "filetype:ppt" or with a numeric range like "$1500..$3000" but seriously, that's like once in a 100 searches or less. I challenge you if you say you use it more. Free text searching almost always gets it right, unless you're searching for homonyms or something general.
Because free text is so good, while I like the idea of a code search engine and I do use them, I'm not sure if I need anything more than a "code-specific free text search." I personally believe that being really specific in your search query is a really good way to filter your way out of the ONE result you need.
Sometimes, however, when searching code you will occasionally want the advanced techniques.
Here's some examples:
If you're looking for a specific implementation of something, like an MD5 hash or a BTree, these search engines can be really useful. They can't tell you anything about quality though.
I started using SyntaxHighlighter on my blog for all my code snippets and I'm bummed I didn't start earlier. The best aspect of it is that all my snippets are inside of <pre> tags. That means they're easily indexed and not littered with markup. The syntax highlighting is added on the client side by JavaScript. I use the PreCode plugin in Windows Live Writer and wrote up on I do it on my blog. It's fast becoming THE way to post code inline. I even convinced ScottGu (by doing it for him without asking) to use SyntaxHighlighter when I converted the Nerddinner PDF to HTML.
You know by know that pasting code in an IM window is a recipe for pain, and misplaced emoticons. I use code-pasting services for this instead.
I really like using Josh Goebel's Pastie for sharing code, although he doesn't formally support C# or VB. I'm slowly moving to Gist.Github.com though.
The best social-code-sharing snippet sites are:
I've had an interesting conversation or two about making sharing code easier with Jeff. We'll see where that goes.
Kind of unrelated, but still fun...I think that every developer should have a blog, or at least an outlet for writing. Those that don't, often use Code Comments to express themselves.
There was a great post at StackOverflow asking for the "best comment in source code you have ever encountered." This, of course, turned into a list of the worst comments ever found in source code, because that's how programmers work, right? Best == Worst. ;) A lot like the Daily WTF.
You can find some interesting stuff if you use the code search engines to search for stuff that shouldn't ordinarily be in code. This guy searched the Linux Source Code for swear words and graphed them over time.
Some other non-explicit examples...
// mind numbing: let caller use sane calling convention (as per javadoc, 3 params), 439 // OR the 2.0 calling convention (no ptions) - we really love backward compat, don't we?
# God, I hate DTDs. I really do. Why this idiot standard still # plagues us is beyond me.
case 'H': horrible++; break;
(c.query_gender().equals("male") ? "He" : (c.query_gender().equals("female") ? "She" : "It")) + " is " + ((c.query_level() == client.WIZ_GOD) ? "the Almighty God himself\n\rBeware of his wrath if you don't follow his laws!" : ((c.query_level() > client.MORTAL) ? "a powerful immortal" : "a puny mortal")))+ "\n\r"
if isinstance(real_child, SilentMock): raise TypeError("Replacing a mock with another mock is a profoundly bad idea.\n" + "Try re-using mock \"%s\" instead" % (name,))
my $db = delete $access->{db}; # This is pure evil. $db->DESTROY;
"Stimpy-drool", "poopy", "poop", "craptacular carpet droppings",
I'm sure if you search, you'll find lots of great stuff in comments, much more colorful than this. For example, the Greatest Code Comment Ever (Line 107) hit tip to Cam Soper:
uint32 sign=[fh readUInt32BE];uint32 marker=[fh readUInt32BE];uint32 chunklen=[fh readUInt32BE];off_t nextchunk=[fh offsetInFile]+((chunklen+3)&~3);// At this point, I'd like to take a moment to speak to you about the Adobe PSD format.// PSD is not a good format. PSD is not even a bad format. Calling it such would be an// insult to other bad formats, such as PCX or JPEG. No, PSD is an abysmal format. Having// worked on this code for several weeks now, my hate for PSD has grown to a raging fire// that burns with the fierce passion of a million suns.// If there are two different ways of doing something, PSD will do both, in different// places. It will then make up three more ways no sane human would think of, and do those// too. PSD makes inconsistency an art form. Why, for instance, did it suddenly decide// that *these* particular chunks should be aligned to four bytes, and that this alignement// should *not* be included in the size? Other chunks in other places are either unaligned,// or aligned with the alignment included in the size. Here, though, it is not included.// Either one of these three behaviours would be fine. A sane format would pick one. PSD,// of course, uses all three, and more.// Trying to get data out of a PSD file is like trying to find something in the attic of// your eccentric old uncle who died in a freak freshwater shark attack on his 58th// birthday. That last detail may not be important for the purposes of the simile, but// at this point I am spending a lot of time imagining amusing fates for the people// responsible for this Rube Goldberg of a file format.// Earlier, I tried to get a hold of the latest specs for the PSD file format. To do this,// I had to apply to them for permission to apply to them to have them consider sending// me this sacred tome. This would have involved faxing them a copy of some document or// other, probably signed in blood. I can only imagine that they make this process so// difficult because they are intensely ashamed of having created this abomination. I// was naturally not gullible enough to go through with this procedure, but if I had done// so, I would have printed out every single page of the spec, and set them all on fire.// Were it within my power, I would gather every single copy of those specs, and launch// them on a spaceship directly into the sun.//// PSD is not my favourite file format.
Enjoy your search and read more code!
Ads by The Lounge