3 ms·
When I was teaching at General Assembly, I always tried to encourage students to read more code. Could you imagine someone studying writing in college without
by tibbon 10y ago
When I was teaching at General Assembly, I always tried to encourage students to read more code.
Could you imagine someone studying writing in college without reading a TON of books, analyzing, and critiquing the writing other others? Where all they did was simply write, but never read anything from great writers? No, clearly not.
Yet, that's what I see most new programmers doing. There's an urge to do it all on their own, without reading the works of others, and that is a surefire way to setup for failure.
- DarkTree 10y agoIt isn't necessarily analogous to compare the study of writing in college to that of learning to program. For one, by the time you are in college, you have been speaking, reading, and analyzing (via communication) your native language for ~15 years. Because we already have such a vast foundation to work from, we can venture into deeper parts of the language and meaning and interpreting how authors use it to create meaning. I think programming is more analogous to learning a new language. Now I can imagine studying Spanish (a non-native language for me) without reading a ton of books, and analyzing and critiquing other Spanish writers, because I simply can't and it isn't that beneficial - from the beginning - to start that without have some foundation to work from. It is only after you learn some vocabulary, grammar, semantics, sentence structure and some cultural significance, that you venture into analyzing and critiquing their writing. I think for beginner programmers, it isn't supremely beneficial to read a bunch of others code, because you don't really know what is good and what isn't or even what to really look for. However, when a programmer creeps into the intermediate level of things, I think reading other's code becomes amazing beneficial, because from that foundation, you can understand, analyze, critique, and maybe most importantly, internalize what that programmer is really trying to achieve and how you can use their methods going forward. Now in reality, we don't really know the point we hit that intermediate level, and we will inevitably be reading other's code at the beginner level. I think it's just a matter of shifting the weight that we put on reading other's code as we mature as programmers.
- tibbon 10y agoThe other side to it is that you'll rarely be writing completely new code from scratch in the majority of development positions (especially as a new developer). Being able to dive into someone else's code, think about it, an add something to it, is probably more important on a day-to-day basis than being able to create something from scratch.
- grey-area 10y agoI think programming is more analogous to learning a new language. I think your analogy is spot on, and yet reinforces the OP's point - one of the best ways to learn a new language is to read a lot of good writing in that language - you absorb idioms, subtle points of grammar, and vocabulary, all of which would be a struggle if you just try to focus on the basics or use a grammar textbook to help. Immersion in the right environment is by far the best way to learn. The current situation with most people programming is that they spend years looking at code written by other beginners (stackoverflow, tutorial articles), and very little time studying real production code written by people who have been doing it for a long time. That is as the OP points out a huge mistake and one it takes many people decades to remedy if they ever do. It's like trying to learn a language by putting 30 beginners in a room with a textbook and making them speak to each other. I'd honestly recommend absolute beginners to spend at least some time reading simple code by someone they admire, if only to demistify the process, and make them realise it is not as complex as they fear. Beginners almost never do this though.
- tracker1 10y agoHowever, it's not really a story you can read through end to end so often as a choose your own adventure with thousands of routes to move through. Though there are smaller projects you can work through and better, or less organized structures... it depends.
- noshbrinken 10y agoReading code becomes beneficial at the moment your biggest concern is no longer "does the application work?" but "how does the application work?" I ceased to be a beginner when I became confidant that I could make something work. My concerns ceased to be "can I make this work?" and became "is there a better way to make this work?" Confidence to answer the former question is gained through writing code. Understanding to answer the latter question is gained through reading code.
- deleted 10y ago[deleted]
- clusmore 10y agoI'm not sure this is a fair comparison, because books are written to be read, but code is (usually) written to be executed. As much as we all agree that code readability is important, the simple fact of the matter is that executability matters a helluva lot more. We're paid to write code that executes, and many people stop there. The result is that there is a lot of code out there that executes just fine but is completely terrible to read, and I wouldn't advise learning from. And the problem is, especially for beginners, it's difficult to determine which is which. Add to this the fact that code changes over time, in more ways than one. 1. It physically changes, i.e. we commit changes to it, so a codebase that is well written today may be dramatically changed tomorrow. 2. The context changes, i.e. the circumstances that make one tradeoff better than another change all the time, and if the context isn't captured effectively, it's difficult to understand when good decisions change to bad ones. 3. The language changes, i.e. new versions or even entirely new languages come out which fix mistakes in old versions or languages, so good code today may be difficult to learn from in the future if the language is no longer well understood. We need more collections of repositories that are agreed upon as "good quality" that everybody agrees are worth reading and learning from.
- nullc 10y agoCode is primarily for communicating between programmers, if it were mostly meant to be executed it would just be object files. Being a great coder means being adept at reading code however its structured. Reading unclear code is important because it helps you avoid making the same mistakes... it's also an important skill in practice: You will often encounter unclear code and have to understand it. The idea that there is one true good form is a misconception often held by novices. An expert should be able to read and write in a multitude of styles so that they can conform to whatever codebase they're working on and apply the best practices within that context.
- clusmore 10y agoThese are all great points, and I'm realising now I have missed the point a little. Reading arbitrary code is good, because you learn something regardless of whether the code is "good" or "bad" -- you learn how to understand more styles of code, and which patterns tend to be easy to understand and which tend to be hard. I guess the part that was tripping me up is that you shouldn't necessarily try to emulate all code you read.