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?
closed as not constructive by gnat, Glenn Nelson, user40980, EL 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.
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.
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.
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.
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.
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...