After 20 years, I've recently switched from csh to bash (so I can use rvm), and I've been applying this neuro-hack to less-used command. When you copy-and-paste, there's a kind of finger-learning that you miss out on.
Learning to play a guitar, for example, is difficult and yet crazily creative for this reason. It is (almost) completely behind finger-learning and thus in many original forms each with a signature of the individual who created the piece.
I wholeheartedly agree. I spent my first few years doing this, and it helped expand my framework vocabulary faster than my co-workers at the same level.
I agree with this article, typing the code out (as long as you are focusing on it and not just typing absentmindedly) really forces you to read all of the code, which should help you understand what is happening. Just as in the article I have also found myself unable to write my own code at times due to working off of other programs or snippets. When I type it out though I gain a much more thorough knowledge of the api or language.
When I was in school, I found that the most important part of the notetaking process was manually writing the notes during lectures. Reading them afterwards rarely helped me remember or learn more. Reading notes written by someone else was next to useless.
I used to write notes and then throw them straight in the bin. My handwriting was effectively illegible and if I needed to know something, I'd look it up again and write new notes... and throw those away too.
Cognitively speaking handwriting and typing are different tasks and thus have different results on the learning process, which may or may not vary individually.
I think the best method to learn coding highly depends on the learning style of the person.
Couldn't agree more. Never download the resources that books put online (code samples and such) - it's tedious writing out a load of code but you learn a hell of a lot faster.
Even more important is to go over every line and don't go on to the next line until you are sure you understand. Just like you can read without understanding you can type without understanding as well. Forcing yourself to understand will make you a much better programmer.
Well, in one sense, I agree. Make sure you know what each line does on its own. But sometimes a single line doesn't make sense, in a larger sense, without something that comes later. So you have to have some tolerance for not understanding each line before you move to the next. It could be put in terms of knowing the "what" vs letting the "why" be uncertain a while.
This is very much in-line with the way the Learn to Code The Hard Way books are written. There's even a call to action at the beginning of each book imploring the reader to avoid copy-paste:
In some ways I disagree with this: I think that it depends a lot on the type of person writing the code. In the past I have found when I copy the code it is essentially a copy-paste level of thinking (i.e. none at all) that takes longer. There are times when I feel that when I am typing out example code I pay less attention than when I copy paste. When I copy/paste, I at least usually try to look for the important parts of what is going on, but when I am typing it out, I am too focused on making sure I have a comma instead of a period, or matching parentheses, etc. And not to mention I tend to leave out comments that probably should be there in the interest of being sick of typing.
To be fair the case I am imagining myself in is the times I have looked up something relatively simple like opening a socket or something pretty mundane, so this may not apply to all situations.
Don't have time today to provide citation but I believe the act of writing and transcribing involves a different part of the brain than reading...this is my layman's theory as to why writing and reading is so much more stimulating than just reading, even when you can easily comprehend the code on sight
it's odd but in my experience I have found it to be the opposite. Perhaps because I've grown up with keyboards I find typing something out to be quite stimulating. When writing things down I find there is a huge disconnect between the funny scribbilous motions of my wrist and the meaning of the words they produce. Although perhaps that's a left-handed thing.
I totally agree with the central thesis of this article, typing out the code for open source projects I like has been my secret weapon for learning technologies faster than most people think possible.
Also: use a REPL. I typed all the examples from the Well-Grounded Rubyist into irb, played with the examples to test edge cases, and ended up learning Ruby faster than I thought possible.
When learning code out of a book for a new language, one trick I found that worked really well was to read the code, then close the book and try to type as much of it from memory as I could. I would futz with it for several minutes exploring various ways of making it break, seeing if I could 1) predict how I was breaking it, and 2) use the error messages to fix my mistakes.
Then I would open up the book again and compare it to what I had typed, examine the differences between them, and see if/how they explained the errors I was getting.
Interesting concept. Especially if you consider that better memory for plausible situations is one of the differentiators between experts and beginners. Be it in programming or chess. Because experts have developed mental shorthands for common situations.
Reimplementing standard library functions is great for Haskell too. The biggest of the "big breakthrough" experiences I had was from trying to reimplement the standard monadic State datatype.
I'm a high school computer science teacher, and this is a WONDERFUL idea. I'm totally going to steal it and create a few assignments based on the concept.
Please do teach them the value of data representation. I don't think this is obvious but it can make the difference between having to write a mountain of code or finding a simple solution.
Super-simple silly example: You have to write a program to work with four colors: Red, Green, Blue, Black. Naive representation will use strings: "red", "green", "blue", "black". A little smarter would be an enum type where red=0, green=1, blue=2, black=4. And, depending on context, it might even make sense to use individual bits to identify each color: red=01h, green=02h, blue=04h, black=08h. And, if the goal is to extend to other colors, it might make sense to represent them as RGB vectors.
You get the idea. I find that a lot of newbie programmers are too focused on the mechanics of writing code. They forget (or don't know) that investing some time in optimizing the representation of the data or problem they are trying to solve could make a world of difference.
OK, I pulled colors as an example out of thin air but I'll see if I can make it work.
Let's say you have to write a routine that does something based on a color as the input. You have a few choices in terms of how to represent the colors:
- The name of the color in a string
- A typdef enum for your colors (integers)
- A color-per-bit scheme (U8, U16, U32)
- Channel-per-bit scheme (U8)
- 4 or 6 bit packed RGB values (U16 or U32)
- 8 bit packed RGB values (U32)
- 8 bit unpacked RGB values (struct of three U8)
- 16 or 32 bit unpacked RGB values (struct of three U16 or U32)
- Unpacked RGB floats (struct of three floats)
- and more...
I won't go into the implications of each of the above. Some of it is highly dependent on both the system and the objectives of the work being done.
Say, for example, that you choose to use the names of colors stored in strings as your color representation. Now you have to compare strings in order to identify the colors:
if(strcmp(input_color, "red") == 0)
{
// Do something with red
}
else if (strcmp(input_color, "green") == 0)
{
// Do something with green
}
else if (strcmp(input_color, "blue") == 0)
{
// Do something with blue
}
... etc
Regardless of language the strings need to be compared character by character. Even if a language or OO framework allows you to say something like if(string1 == string2) you have to keep in mind that what is going on behind the scenes is pretty much exactly what strcmp() has to do. Which means that the above is, at the very least, slow.
And, of course, it isn't very portable. What happens if the input has to be in German or Japanese?
The typdef enum representation gives you the ability to use a far more efficient construct to identify your colors:
switch(color)
{
case COLOR_RED:
// Do something with red
break;
case COLOR_GREEN:
// Do something with green
break;
case COLOR_BLUE:
// Do something with blue
break;
... etc
This is much, much faster. It is, at the core, an if/else-if structure that is only comparing integers, which is a single machine language instruction. Fast and clean and language-portable by means of the proper text-to-integer function somewhere to deal with different languages.
If you are on an embedded system that can do bit testing in machine language it might make sense to encode one color per bit or one color channel per bit. for example, in some embedded C dialects you might be able to do something like this:
if(color.0) // Select and test bit 0
{
// This is red
}
else if(color.1) // Select and test bit 1
{
// This is green
}
... etc
At this level the advantages of doing this are tightly linked to the platform and the goals of the application.
If, for example, one needs to be able to expand the available range of color inputs beyond what can be described with simple words a discrete RGB representation might be the best choice. This is also the case if you wanted to future-proof the program and be ready for when more colors arrive.
Here you have several choices, two of which are to represent each channel with an 8 bit value or choose floats instead.
The 8 bit values can be packed nicely into a U32, making it very efficient.
You could also create a struct to facilitate access to the components and let the compiler optimize for you.
The float example is interesting because the conversion from float to whatever (if necessary) can be of any bit width. So, for example, if the color needs to ultimately be mapped to an 8-bit-per-channel display device you can translate from float to 8 bits on output. All of your intermediate math and color manipulation would be done in full-resolution floats which means that you are not going to accumulate errors. This, for example, is important if you are applying ...
I think it depends on a lot more of the circumstances than this. For example, in many languages, you can intern the strings so that a string equality test is just a single machine instruction. And if these color representations are crossing some interface that needs to be kept stable, it's a lot easier to add new colors if what's crossing is "red" or "#ff0000" than if it's "2". And it may be that what you're doing with the colors is just generating HTML, rather than doing multi-way switches, in which case the enum implementation has no advantage over the string representation; it just increases code duplication.
The probably more important consideration is that with an enum, your compiler can catch misspellings. Depending on your runtime environment, this can be a huge killer advantage. In particular, if your runtime environment can't do much beyond blink an LED to report errors, compile-time checking is really really important.
I think we are slicing this a little too thin. My original point is that it is important to understand that the choices made when representing data can be important. My off-the-hip example was not meant to be definitive.
I think it is important to learn enum's but it is a bad example. You really should use the built-in types when they're available. Color is a particular example (in both .Net and Java) where you should never re-invent the wheel.
In general teaching enum's is very important and something I've not really seen schools do (although their "learn to program" programs are typically 101 to newbie, I've never seen a program that really builds on the basics).
While I generally agree with what you are saying, it is very important to understand that built-in types are not magical. This is where coming from a low-level embedded C or assembly background can be very useful.
As an example, there are a number of sites showing the performance hit you take if you use some of the NS types in Objective-C. If I remember correctly, in some cases you are talking about NS types running 400 times slower than alternative code.
In my mind, the question of data representation could also include this very choice: Do I use an NSArray or do I do it "by hand", allocate memory and use a "simple" array?
Color.Bleu will give a compiler error. "Bleu" will not. And is "White" a valid color? No idea, need to check the docs. Try Color.White and the compiler or editor will tell you.
One thing that has really sped up my my learning over the past few months on Code Academy -- complete a lesson, then in another tab open their scratch pad feature and try to retype the code not only from memory, but also while thinking through the logic.
Has really been effective for me as I feel like I now have a pretty good (beginner) grasp on JS and Python
Another +1. When I've used this method I've noticed that the tendency at first is to try to just remember the code and copy it out of your memory. There comes a point, though, when you don't remember it well enough to "copy from memory" and this naturally segues into a focus on the logic and syntax of the code so you end up reconstructing working code without fully remembering the original.
This is exactly how I handle learning languages/frameworks. I wrote a little something on it as well [0]. I value thorough comprehension over "how fast can I finish this book?". I try to get my hands as dirty as possible. I want to see the warts of this thing I plan on using to build projects. Trying to rebuild book examples from memory forces you to consider the requirements and the end goal. This was particularly useful when I first learned about linked lists and queues in C.
Even with a computer science degree I still do this. If I read a tutorial or a book about a new library or language, I type all the examples. By the time I'm done, I know what I'm doing.
That's also how I learn. As I never met anyone who learns that way I always considered this behaviour weird. Nice to know that I am not the only one :).
I don't have the same experience as most people here. I usually just study by reading or trying to recreate the scene/code/history in my read, and works greatly to understand and memorize. When i write it takes time and feels like wasted time.
Just out of curiosity, how many people took their first steps in programming by typing out code from a book or magazine?
- When I started out, including a floppy disk with a book or magazine was a rarity, so BBC Basic, Spectrum and PCW-9512 had to been typed in by hand, with frustrating hours trying to figure out why they didn't work (usually typos)
> This website uses CloudFlare in order to help keep it online when the server is down by serving cached copies of pages when they are unavailable. Unfortunately, a cached copy of the page you requested is not available, but you may be able to reach other cached pages on the site.
I'm probably being significantly thick-headed, but what exactly is Cloudflare even doing if it's not properly caching pages before traffic hits? And why would they want to advertise that fact on the error page?
If the site goes down before anyone even hits it, I imagine that CloudFlare can't exactly get a copy. Given that CloudFlare is a free service, I suspect that it doesn't automatically cache every page, either- it might wait until a page gets to a certain level of traffic before doing so. I have no idea, though.
That may be an effective way to learn (ie. it works), but I have my doubts that it's a very efficient one (ie. you could learn more in the time you spend retyping that code) or a very entertaining one (ie. other ways of learning to code are more interesting).
You could create more, but whether you'd learn more is another matter. I know that whenever I've gone down the "copy a bit of code and modify it" route it's never sunk in as well as manually retyping it all.
I've found that it's easy for me to space out and just go through the motions if I'm simply reading or copying and pasting. Typing the code manually (or writing full notes in a university course, et cetera) forces me to focus enough that it actually drastically increases my efficiency.
So in my case, it's more work to type out code than to copy and paste, but it saves work in the long run.
I created http://typing.io primarily to help programmers practice typing, but it also allows users to explore open source code like jQuery and Rails by typing through instead of just reading.
183 comments
[ 5.3 ms ] story [ 233 ms ] threadAfter 20 years, I've recently switched from csh to bash (so I can use rvm), and I've been applying this neuro-hack to less-used command. When you copy-and-paste, there's a kind of finger-learning that you miss out on.
Learning to play a guitar, for example, is difficult and yet crazily creative for this reason. It is (almost) completely behind finger-learning and thus in many original forms each with a signature of the individual who created the piece.
When I was in school, I found that the most important part of the notetaking process was manually writing the notes during lectures. Reading them afterwards rarely helped me remember or learn more. Reading notes written by someone else was next to useless.
I think the best method to learn coding highly depends on the learning style of the person.
When I come across something I don't fully understand, I'll take a few minutes to investigate that bit of code to learn even more.
http://learnpythonthehardway.org/book/intro.html
Do not copy and paste code when you're trying to learn
Always type it out
To be fair the case I am imagining myself in is the times I have looked up something relatively simple like opening a socket or something pretty mundane, so this may not apply to all situations.
Then I would open up the book again and compare it to what I had typed, examine the differences between them, and see if/how they explained the errors I was getting.
Suggestion: code fragment on your interactive whiteboard. Ask students what each line does, how the loop works &c.
Then blank the screen and get them to replicate....
Super-simple silly example: You have to write a program to work with four colors: Red, Green, Blue, Black. Naive representation will use strings: "red", "green", "blue", "black". A little smarter would be an enum type where red=0, green=1, blue=2, black=4. And, depending on context, it might even make sense to use individual bits to identify each color: red=01h, green=02h, blue=04h, black=08h. And, if the goal is to extend to other colors, it might make sense to represent them as RGB vectors.
You get the idea. I find that a lot of newbie programmers are too focused on the mechanics of writing code. They forget (or don't know) that investing some time in optimizing the representation of the data or problem they are trying to solve could make a world of difference.
Let's say you have to write a routine that does something based on a color as the input. You have a few choices in terms of how to represent the colors:
I won't go into the implications of each of the above. Some of it is highly dependent on both the system and the objectives of the work being done.Say, for example, that you choose to use the names of colors stored in strings as your color representation. Now you have to compare strings in order to identify the colors:
Regardless of language the strings need to be compared character by character. Even if a language or OO framework allows you to say something like if(string1 == string2) you have to keep in mind that what is going on behind the scenes is pretty much exactly what strcmp() has to do. Which means that the above is, at the very least, slow.And, of course, it isn't very portable. What happens if the input has to be in German or Japanese?
The typdef enum representation gives you the ability to use a far more efficient construct to identify your colors:
This is much, much faster. It is, at the core, an if/else-if structure that is only comparing integers, which is a single machine language instruction. Fast and clean and language-portable by means of the proper text-to-integer function somewhere to deal with different languages.If you are on an embedded system that can do bit testing in machine language it might make sense to encode one color per bit or one color channel per bit. for example, in some embedded C dialects you might be able to do something like this:
At this level the advantages of doing this are tightly linked to the platform and the goals of the application.If, for example, one needs to be able to expand the available range of color inputs beyond what can be described with simple words a discrete RGB representation might be the best choice. This is also the case if you wanted to future-proof the program and be ready for when more colors arrive.
Here you have several choices, two of which are to represent each channel with an 8 bit value or choose floats instead.
The 8 bit values can be packed nicely into a U32, making it very efficient. You could also create a struct to facilitate access to the components and let the compiler optimize for you.
The float example is interesting because the conversion from float to whatever (if necessary) can be of any bit width. So, for example, if the color needs to ultimately be mapped to an 8-bit-per-channel display device you can translate from float to 8 bits on output. All of your intermediate math and color manipulation would be done in full-resolution floats which means that you are not going to accumulate errors. This, for example, is important if you are applying ...
Thanks for sharing!
The probably more important consideration is that with an enum, your compiler can catch misspellings. Depending on your runtime environment, this can be a huge killer advantage. In particular, if your runtime environment can't do much beyond blink an LED to report errors, compile-time checking is really really important.
In general teaching enum's is very important and something I've not really seen schools do (although their "learn to program" programs are typically 101 to newbie, I've never seen a program that really builds on the basics).
As an example, there are a number of sites showing the performance hit you take if you use some of the NS types in Objective-C. If I remember correctly, in some cases you are talking about NS types running 400 times slower than alternative code.
In my mind, the question of data representation could also include this very choice: Do I use an NSArray or do I do it "by hand", allocate memory and use a "simple" array?
Has really been effective for me as I feel like I now have a pretty good (beginner) grasp on JS and Python
[0]: http://vertstudios.com/blog/how-to-read-programming-tutorial...
- When I started out, including a floppy disk with a book or magazine was a rarity, so BBC Basic, Spectrum and PCW-9512 had to been typed in by hand, with frustrating hours trying to figure out why they didn't work (usually typos)
> This website uses CloudFlare in order to help keep it online when the server is down by serving cached copies of pages when they are unavailable. Unfortunately, a cached copy of the page you requested is not available, but you may be able to reach other cached pages on the site.
I'm probably being significantly thick-headed, but what exactly is Cloudflare even doing if it's not properly caching pages before traffic hits? And why would they want to advertise that fact on the error page?
http://tommy.authpad.com/don-t-copy-and-paste-other-people-s...
If you want to read it elsewhere
CloudFlare doesn't cache HTML by default, though you can enable that with Page Rules. http://blog.cloudflare.com/introducing-pagerules-advanced-ca...
And the origin needs to be responding long enough for us to keep a copy in cache, once the Page Rule is enabled.
It also means I've now heard of Cloud Flare.
So in my case, it's more work to type out code than to copy and paste, but it saves work in the long run.
http://tommy.authpad.com/don-t-copy-and-paste-other-people-s...
We're restarting our server because we weren't quite prepared for #1 on HN server load
Understanding the data structures, not so much, but when you're trying to learn a new language the greatest initial friction is the syntax.
I have to admit, it greatly aided my ability to recall things and get "fluent" with the language.