Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have heard a few people say that one of the best ways to improve your coding ability is to read others code and understand it. My question, as a relatively new programmer, where do I go to find good source code examples that are not too far over my head?

share|improve this question

closed as not constructive by gnat, Glenn Nelson, MichaelT, Yusubov, Martijn Pieters Feb 21 '13 at 21:22

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

7  
+1 for an excellent question –  Gary Rowe Jan 22 '11 at 8:00
    
This has been asked on StackOverflow: stackoverflow.com/questions/3083525/… –  nikie Jan 26 '11 at 16:25
2  
I just look back over my old code. –  Paul Mar 7 '12 at 15:07
    
Paul, that is not going to help the OP is it? Obviously they don't have good code already written in the past. sheesh. –  junky Mar 7 '12 at 15:09
1  
@junky hopefully they have some sense of humor though :) –  Konrad Morawski Mar 7 '12 at 15:17

5 Answers 5

up vote 26 down vote accepted

You can browse open source projects on repository sites like GitHub, Codeplex, Google Code, or BitBucket. You'll find projects of different complexity levels, so you should be able to find something that both interests you and doesn't go over your head too much at first.

Another option is Scott Hanselman's Weekly Source Code blog posts.

I recommend starting with an established, active project to lower the odds of starting to read code that hasn't been through use and scrutiny yet. Ideally, find something that interests you and that you can use. Using the app will help you understand the source code. Another benefit of choosing an open source project is that you may be able to contribute some fixes or features, which will help make reading through the code more interesting.

Staring at a bunch of someone else's code can be intimidating, so start with the main function (or equivalent) and work your way through from there.

share|improve this answer
    
-1: a beginner can't tell between good and bad code, so 'browsing' projects isn't going to help. You kind-of covered this by recommending 'established' projects, but I've seen awful code in projects we've all heard of. I don't have a better answer, though. It's actually a hard question, needing an answer tailored to the individual's skill levels, interests, and filtered through a mentor's knowledge. –  Cris Aug 29 '12 at 22:06
1  
@Cris I don't disagree, but I will note that there's a lot to be learned from reading bad code as well. Arguably, reading and following bad code is even tougher than diving into a properly organized project. (And this is before we get into trying to figure out what "good" code is. :)) –  Anna Lear Aug 29 '12 at 22:11
    
true enough. But for most of us non-geniuses, self-education has limits. Most beginners (in all fields) need exposure to "the good" to get a feel of what is good. And "the Internet" is a world-clamour of "I'm good!", which doesn't help. –  Cris Aug 29 '12 at 22:23

Very few people write good source code on their first try. Good source code is often produced by a series of revisions. Thus, if you can find source code that has been peer reviewed multiple times, and fixed multiple times, you're probably in a better location. Some open source projects (and some portions of those) are particularly well reviewed. Code coming from companies that have mandatory review cycles (e.g., Google but there are many others) may fit the bill.

That being said, I'm not sure your goal should be to find "great code". It should be to look at different styles of code (such as those written by your coworkers), and learn to identify the good and bad points about it. The more bad points you identify, the more you will strive to make your code better and know how.

In particular, I believe that a very good approach to get a sense of good code is to use the interactive debugger to trace through complex code, following the chains of invocations. For example, go to one of your company's main files, put a breakpoint, and start figuring things out from them.

After a few times you get disoriented by 100-line functions with ten level of indentations and dependencies on globals, and a few times that you breeze through well decomposed code, you'll improve your own programming.

share|improve this answer

I found that the code of the libraries that come with your programming language of choice are often a good start to see what's thought to be best practices and good coding style.

Though you don't want to start with places like sorting algorithms or complex container classes.

Another place for interesting insights in writing code is Project Euler (http://projecteuler.net/). Slight disadvantage there: You must solve the problem first to get access to the forum where others posted their solutions (interesting challenges for all levels of experience). But once done you'll find examples for nearly all major programming languages. And since you've solved the problem already, it will help you to understand other peoples code.Plus you get to see code of languages you don't know yet, but may find interesting.

share|improve this answer

I really enjoyed reading Beautiful Code. It has short, but very nice code examples with detailed explanations.

...leading computer scientists offer case studies that reveal how they found unusual, carefully designed solutions to high-profile projects. You will be able to look over the shoulder of major coding and design experts to see problems through their eyes.

...The authors think aloud as they work through their project's architecture, the tradeoffs made in its construction, and when it was important to break rules.

This book contains 33 chapters contributed by Brian Kernighan, KarlFogel, Jon Bentley, Tim Bray, Elliotte Rusty Harold, Michael Feathers,Alberto Savoia, Charles Petzold, Douglas Crockford, Henry S. Warren,Jr., Ashish Gulhati, Lincoln Stein, Jim Kent, Jack Dongarra and PiotrLuszczek, Adam Kolawa, Greg Kroah-Hartman, Diomidis Spinellis, AndrewKuchling, Travis E. Oliphant, Ronald Mak, Rogerio Atem de Carvalho andRafael Monnerat, Bryan Cantrill, Jeff Dean and Sanjay Ghemawat, SimonPeyton Jones, Kent Dybvig, William Otte and Douglas C. Schmidt, AndrewPatzer, Andreas Zeller, Yukihiro Matsumoto, Arun Mehta, TV Raman,Laura Wingerd and Christopher Seiwald, and Brian Hayes...

share|improve this answer

Instead of finding great codes, Look into General Programming Books.

e.g. Code Complete, Writing solid code, Design Patterns (I am sure there are lots of other books around in another question and answer in this site)

Those books are describing the philosophy what considered as good code. Readability, performance, maintainability, bug-detection etc.

Which serves a even better resources and more efficient than trying to figure out what the author trying to achieve.

P.s. Good Software design is what you should be looking into as well. Which would be hard to recognize solely from observing codes, given the project is large enough.

share|improve this answer
    
I'd like to mention "Clean Code" as a good resource. –  mri Feb 21 '13 at 14:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.