I think a good learning trick that's an intermediate point between the two approaches he discusses (copying the sample code vs. closing the book and write the code yourself) is to write a different program based on what you can see in the sample code. That's how I learn tech myself--I already have some other project in mind, so I rarely sit down to build 'sample' apps with any sincerity. I try sample code to the extent needed to run it without errors, then integrate patterns from it into some other code I was meaning to write in the first place.
To use the Benjamin Franklin analogy, it would be like Franklin having the articles he was seeking to understand open before him, but adding new paragraphs in between the original text, or re-purposing the article into a speech.
This is a very common pedagogical approach of course--hence the cliche phrase 'left as an exercise for the reader.' The subtlety is that this is usually a hypothetical exercise on top of an already hypothetical problem, whereas if I have my own project in mind, I am far more motivated to figure things out.
One of my go-to projects for learning a new language is implementing Conway's Game of Life (while trying my best to follow the idioms of the language).
I feel that project is easy enough to not get terribly frustrating, but not entirely trivial, forcing you to think a bit about how you are going to structure it.
What I like about languages with REPLs is that I can just copy-paste stuff and then muck about at various points of execution to get an idea of how things work, or prod at the different parts.
To use the Benjamin Franklin analogy, it would be like Franklin having the articles he was seeking to understand open before him, but adding new paragraphs in between the original text, or re-purposing the article into a speech.
This is a very common pedagogical approach of course--hence the cliche phrase 'left as an exercise for the reader.' The subtlety is that this is usually a hypothetical exercise on top of an already hypothetical problem, whereas if I have my own project in mind, I am far more motivated to figure things out.