One of the best techniques when learning to code is to reverse-engineer existing code. Code training company Treehouse explains how to “break” code to better understand how it works.
Photo by hackNY.
Nick Pettit gives an example on the Treehouse blog of wanting to learn about 3D programming in the browser and testing a line of a Javascript script, Three.js:
The following is a code example that uses three.js to create a light:
var light = new THREE.PointLight(0xffffff);
light.position.set(-100,200,100);
scene.add(light);
If you have some programming experience, you might be able to read this and figure out most of what’s happening here. If there are any blanks or assumptions you’re unsure about, you can test them with the following techniques:
- Delete one line at a time to see if it’s necessary for your goals.
- Delete one line at a time to better understand its function. Even if you think you know what a line does, try deleting it anyway to test your assumptions.
- Change variables and function arguments to see if you can manipulate them in a way that matches your mental model.
- Swap the order of various lines to see if things can be done “out of order” or if there’s some significance to the sequence of operations.
As you test each line, you’ll start to build a mental model and make corrections to other assumptions you made previously. Soon, the whole picture will start to make sense.
The key is to break one thing at a time and see what changes so you can understand the relationship between all the parts in the code.
Self-taught programmers may already know the great value of reverse engineering others’ code, but as Treehouse points out, this strategy doesn’t get enough emphasis. Thanks to generously shared open source code, you can learn just about anything by breaking it.
The Secret to Learning to Code [Treehouse Blog]
Comments
2 responses to “Learn To Code By Breaking Someone Else’s Code”
I learned a lot of Qbasic as a kid by doing that kind of thing. Even managed to remove the subroutine responsible for causing death when you run in to a wall in Snake.
It’s how i learned to write Javascript. Did a course after I had slogged by way through a couple of projects; most of the time i was bored, until the lecturer blew my mind with basic concepts I had never encountered.