Join 3,433 readers in helping fund MetaFilter (Hide)


Source code for reading?
March 26, 2012 2:44 PM   Subscribe

What Python and PHP source code can I read to become a better programmer?

Some commentary as to what the source code or project does particularly well would be helpful, as well as what level of skill or experience you think is required to really benefit from the exercise.
posted by jsturgill to Computers & Internet (17 answers total) 6 users marked this as a favorite
 
Google Code hosts plenty of Python projects. Stick to finding projects that interest you and don't worry so much about whether your skill is sufficient.
posted by inauthentic at 3:00 PM on March 26, 2012


In terms of benefit per unit of time spent, the only code that you can read to become a better programmer is your own.

Programming isn't something you can really learn effectively by reading, you need to be doing.
posted by toomuchpete at 3:31 PM on March 26, 2012


toomuchpete has a point, but you can always look at the PHP source code for WordPress and study it, although you may get tripped up by some of its proprietary functions. It's been a while since I've looked at it but it uses a function like _ or _e to output text; I believe it's something to do with translation and globalization. For example instead of echo "Welcome" you see stuff like __("Welcome"), so be aware of that.

For my money, you're better off learning PHP. It's much more widely supported and documented, and if I allow myself to editorialize here, a much better language in general.
posted by MattMangels at 4:05 PM on March 26, 2012


This question has been asked for Python and PHP over at Stackexchange and there are answers there.

It's hard to say what is great code to read. The best I've seen in C++ was an API that a company I worked for had written. But part of that was because I had to use to the API and needed to understand how it worked and so had to read and understand it deeply.

Books like 'Effective C++' are really good for C++. Something similar might be worth finding for Python. This question has also been asked over at stackexchange.
posted by sien at 4:18 PM on March 26, 2012


Look at the code from the Python Standard Library.
posted by aroberge at 4:21 PM on March 26, 2012


You can then check out the "most watched" python and php repositories on github to see what everyone's interested in these days.

Also search around for videos from stuff like google tech talks or conferences like PyCon. There's a list of python related videos at pyvideo.org.
posted by musicismath at 4:25 PM on March 26, 2012


Toomuchpete, I've consistently heard many programmers assert that reading others' code is both an essential skill to have and an important way to learn new techniques and approaches to solving problems. You think that is wrongheaded?

MattMangels, I know both PHP and Python already, for certain values of know. That's why I singled 'em out in the question.
posted by jsturgill at 4:54 PM on March 26, 2012


I've consistently heard many programmers assert that reading others' code is both an essential skill to have and an important way to learn new techniques and approaches to solving problems.

I actually think your best solution is to meld the approaches of reading others' code and working on your own projects by heading over to Github, finding some open source projects that interest you, looking at their issues lists, and contributing patches that solve real problems or add extra features. Go forth and fork!

You'll learn to read code written by others well enough to work on projects together, and how to write code that is well-integrated with an existing project.
posted by 168 at 7:46 PM on March 26, 2012


I came in here to say what toomuchpete did. Now that it's been said (and you've asked for clarification) I'll say that for a beginning-to-intermediate programmer, that advice just holds true all the way -- set yourself a challenge, try to solve it a time or two, and then maybe go see how other people have solved it before you.

The problem with just reading source code is that you won't be able to read all the code they took out. All the bugs, all the poor architecture decisions that they tried and then regretted, and all the pieces they wrote and then refactored out. The only real way to see THAT stuff is to do it yourself or be part of a team that did.

It's not what you asked, but if you want to study something to become a better programmer, I would recommend a book or three on design patterns. Many (though not all) of the patterns will be usable in whatever language you end up spending time in, and literacy in frequently used patterns will make your eventual reading of real source code faster and more productive as well.
posted by contrarian at 8:03 PM on March 26, 2012 [1 favorite]


If you're just reading some code "cold", the problem is that it might give you some knowledge but it will be quickly forgotten. If you're writing a lot of code yourself, that's quite a bit better because you can go over it and figure out ways to make it more modular, extensible, clearer, simpler - it's an enjoyable process and it really teaches you techniques that you'll remember like the back of your hand. Still, the problem is that you won't even know some advanced techniques because they simply won't occur to you.

The best approach is to do both in a connected way - work on some project, the look at similar or related code; for instance I do a lot of django work and when I'm in the stage where I'm polishing and refining my projects, I will go into django sources of modules that my projects use and see if I can take some classes or modules out and customize them to fit my use cases better. One example is django admin which is really great insofar as it can be customized but once you hit the limit, it can be very tricky to go beyond simple tweaks, so you'll read the sources and try to see if the part you need has too many dependencies on other things and if it's worth playing with.

Another example is the new class views.. First you might just use them as-is, then try to mix and match and further customize them.

And it doesn't even matter all that much if a particular module is well written or not, a bad example can be very educational, too!
posted by rainy at 8:18 PM on March 26, 2012


To clarify, I'm constantly working on my own projects and freelance jobs. And on the freelance stuff, I read plenty of other people's stuff. Unfortunately, I'm usually fixing someone else's horrible duct tape and twine hack job on osCommerce or WordPress by employing some slightly less terrible duct tape and twine. Reading the osCommerce source code is necessary upon occasion, but rarely enlightening except as what feels like a counterexample.

So yes, writing is more important than reading. But I really would like to set an hour or two aside every week to study, with purpose, someone else's code that is exemplary for some positive reason. You really don't need to convince me to write code as well.
posted by jsturgill at 8:43 PM on March 26, 2012


Extending/patching an open source project is a great idea of how to combine the reading and the writing. I'd appreciate specific suggestions for that as well, if anyone has any. Particularly if the project is small and well written.

(I will head over and use musicismath's links to find my own suggestions... but still. A nudge toward this project or another would be welcome!)
posted by jsturgill at 9:13 PM on March 26, 2012


Maybe a cheeky answer, but the code I read that makes me better is my own, 2-3 months after I wrote it and the clever afterglow has faded. A lot of decisions get revisited in the cold light of day, and you'll know why you made them and perhaps what to do differently.

Also, talk to people about solving problems. Arguments in issue comments on github are a great place for this.
posted by migurski at 9:42 PM on March 26, 2012


jsturgill - I know where you are coming from - I came in here to see if there was something I could use.

Most of the time, I am thinking in set patterns, making some design decisions that seem sub-optimal. To break that cycle, reading code with good documentation from people who are better programmers may teach us new/elegant techniques.

An analogy would be that an apprentice working on his own would never become a master craftsman [although the analogy sounds cliched/cheesy to me].

I am sure you would have read it, but in case you didn't, The Pragmatic programmer series is a good example of this kind of material.
posted by theobserver at 11:58 PM on March 26, 2012


Read through requests, and the python hitchhiker's guide.
posted by tmcw at 7:02 AM on March 27, 2012 [1 favorite]


I am a professional software engineer with multiple years of experience and a relevant college degree. I understand your frustration with being unable to read poorly written code. Reading poorly written code is always difficult, no matter how much experience you have.

Reading code and writing code are not two separate skills. Computer languages are not like real languages, they are logical / mathematical, and follow rules. Once you see the mathematical purity underlying all computer languages, no matter what code you are looking at, it will make sense(assuming said code is not deliberately opaque and follows best practices).

Thankfully, becoming a better programmer is just about the most deterministic process there is. All you have to do is execute the following pseudo code:
1) Study your bibles(Algorithm books, programming language specific books written by experts, etc)
2) Write code
3) Go to step 1

I am a C/C++/Assembly programmer, and though I know php and python well enough to get paid to work in those languages, take the following suggestions for bibles with a grain of salt.

Programming Python by Mark Lutz has an introduction by Guido von Rossom, the creator of python. This is a tremendous endorsement. Read and memorize this entire book.

Programming PHP is written by Rasmus Lerdorf, creator of PHP. Read and memorize this entire book.

Take notes on these books, learn and memorize all the nuances of your languages of choice. Pay special attention to notes on optimization, and anywhere you see the words "best practice" or "idiom" or "pattern."

Get a good Algorithms book as well, the Art of Computer Programming is great, though there are probably algorithm books for python which might be more valuable for your specific purposes.

In addition, studying processor architecture at a low level will take your programming / thinking skills to the next level, even if you never actually learn an assembly language.

It would be more valuable for you to REREAD one of those books, then it would be to read someone else's code just on a lark. Those books will be filled with best practices, and if you steep yourself in the wisdom of the gurus day in and day out, you WILL become a better programmer.

Now, with regards to step 2 above, what you really want to do, rather than 'read code' as just an exercise, is read code for the purposes of writing code. Finding open source projects to contribute to is a great idea.

Though honestly, if all you ever did was read your bibles and work on your own projects, you would naturally become a better programmer.
posted by satori_movement at 9:17 AM on March 27, 2012 [1 favorite]


I'm surprised at the pushback to this question.

I can't imagine any craft that doesn't benefit from seeing someone more skilled tackle the same problems you are tackling in a different or more efficient manner.

I can't imagine being a mathematician and not dedicating a portion of your study at some time to reading others' proofs of complex problems and learning from them.

Didn't the very concept of design patterns spring from, in a sense, reading code? They came from patterns of code that existed already in codebases, were already implemented and working, that were eventually recognized as common methods of dealing with common problems. They didn't come from a bible, they came from active professionals interacting with diverse code bases and seeing the same solutions coded again and again.

Why does this cherrypy project use a directory structure so different from this other cherrypy project? Why does this project have fat models and that project have fat controllers? Where does validation live in this project versus that project? How do these two similar projects differently tackle the challange of translations? Which one looks easier to extend or maintain? Looking at the code, and understanding it, seems like one great way to augment other methods of study and practice.
posted by jsturgill at 9:48 AM on March 27, 2012


« Older What should I look for/value i...   |  I recently got a wok (I've nev... Newer »
This thread is closed to new comments.