56

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?

6
  • This has been asked on StackOverflow: stackoverflow.com/questions/3083525/…
    – nikie
    Commented Jan 26, 2011 at 16:25
  • 3
    I just look back over my old code.
    – Paul
    Commented Mar 7, 2012 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
    Commented Mar 7, 2012 at 15:09
  • 2
    @junky hopefully they have some sense of humor though :) Commented Mar 7, 2012 at 15:17
  • this is the question i was going to ask but its my good luck that i found it .i thought this is only my problem that i dont know where to find codes
    – Dhananjay
    Commented Aug 29, 2012 at 16:55

5 Answers 5

30

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.

3
  • 4
    -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. Commented Aug 29, 2012 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. :))
    – Adam Lear
    Commented Aug 29, 2012 at 22:11
  • 2
    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. Commented Aug 29, 2012 at 22:23
11

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.

4

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.

2
  • 1
    I'd like to mention "Clean Code" as a good resource.
    – mhr
    Commented Feb 21, 2013 at 14:06
  • @Zekta Chan, please pay attention that you are answering another question. The question you answer seems like "What are the good rules to write good code," but TS asks about these rules applying. The difference is like between a code of laws and their judicial application. Commented Jan 3, 2021 at 21:38
3

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.

3

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...

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