The best skill a developer can have is being able to read code well.
Reading and understanding code is the most important skill because you spend most of your time doing it already and you need to understand not only your own code but also others' code.
As Robert C. Martin has famously said "the ratio of time spent reading versus writing is well over 10 to 1". While I completely agree with his observation, Martin uses this fact to recommend making your own code as understandable as possible which I would also agree with too. However even more important is being able to proficiently read code.
You can't write all the code you will use or keep the rest of the world up to your high standard. In order to build anything, make modifications to code or even just discuss it requires understanding others' code. Learning how to understand bad code is the only way through this challenge.
Writing good code only goes so far, you'll still have to read your teammates' code, your framework's and library's code.
How can I get better at reading (bad) code?
Reading code is a muscle that needs to be trained. So far your brain is pretty well trained on reading mostly your own codebase. Like only listening to only one news source, you can fall into limiting yourself in thinking in only one way.
Mixing up what read allows you to get different inputs and see different solutions.
I'd recommend:
- Read how your framework (e.g. React, Vue.js, Rails, etc) is built to understand what you've already got to work with and how you can take advantage of it.
- Read how the libraries you use (e.g. Redux, lodash, Rake) are structured and modularised.
- Read how similar applications are built and how they are solving a lot of the same problems you have in different ways (e.g. Sound-Redux, vue-hackernews).
What codebases would you recommend reading?
Top comments (2)
I recommend reading a lot of garbage, hard-to-comprehend, awful code. Then rewrite it. Maybe port the logic to a different programming language in addition to refactoring in the code's original language.
The most important thing to find out when reading code is what happens with the state. Individual lines of code are not important at all. The only thing that matters is understanding the state and how it changes - and if there are leaky holes in the code logic when the state updates unintentionally, incorrectly or the state update is skipped.
The greatest challenge is not with just one snippet of code: sometimes you can have whole app which has multiple levels of badly constructed code. Issues may include: poor naming conventions, varying naming conventions, datatype mismatching, only handling the success path (ignoring any unexpected or error code paths), over-engineering (code having features that are made in case they might be needed, or unnecessary levels of abstraction), comments that do not reflect the code... it is all human stuff.
Trust input and output. Do not trust code. Human is always the error. You are probably wrong, too, until you give in and understand that you are wrong. You can only trust yourself if you do not trust yourself. Hardware failures are almost never the reason for bugs.
Thanks Vesa, I completely agree. Understanding the movement and mutation of state is key and getting a view of the big picture. Thanks again