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.
The following is a code example that uses three.js to create a light:
var light = new THREE.PointLight(0xffffff);
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]