It strikes me as odd. We’ve heard the same messages:
Write clean code. Write readable code. Write meaningful code.
I agree with these sentiments. But all the talk about writing code overshadows another critical skill: the ability to read it. This is odd to me because so much of our time as programmers is spent reading code:
- Debugging your code or someone else’s.
- Picking up a project mid-stream.
- Receiving some starter code for a project or problem to complete.
More to the point, reading code can help you become a better writer of it.
1. An Important Skill
When I began writing articles, I repeatedly got the same advice for self-improvement: write a lot and read a lot. That is right read a lot. So that’s what I did (and still do). I read, a lot. I read to learn. I read to better understand grammar. I read to understand how to structure and design a sentence, paragraph, and article.
Now, as a programmer, I read code for similar reasons. I read code to enhance my programming knowledge and learn how to design better programs. As I see it, in both cases, reading is just part of the job. You need to continually read to continually improve your skills as a writer of prose or code.
2. Reading Vs. Doing
Writing code and reading it are two different skill sets. Athletes make the distinction clear. You’ll often hear about athletes who watch their game films. At first it seems like a basketball player isn’t getting any better when glued to the television watching last night’s game. After all, they’re watching the game, not playing it.
However, there’s a good reason why they do it: to get better at reading the game. As a result, they start seeing patterns. They see the opening to take the three-point shot, which they passed up at the end of the quarter. They pick up tricks from the all-star point-guard on the opposing team. In short, they evaluate their own performance and see room for improvement. The “reading” informs the “doing.” So, when the next practice or game rolls around, they’ll apply what they learned from “reading” the films.
3. Reading Informs Writing
Reading code is like an athlete who watches their game films. There’s a lot to gain when you become a vast reader.
a. See patterns
Train your mind and eye to spot situations to use a particular algorithm, function, or data structure. If you encounter a problem that uses reduce (), there’s a good chance that you’ll encounter another problem that’ll use it. When you read code, you familiarize yourself with such situations. So, when you work on a related problem in the future, you’ll recognize the pattern and know what to reach for.
b. Learn new strategies to solve the same problem
I solved one problem using a list of dictionaries. Then I read the code of another programmer who solved the same problem, but he used defaultdict. It turned out to be a much better approach. Same problem, new trick.
c. Improve code design
It’s been said that easy reading is hard writing. That’s true for writing both prose and code. When you read code written by a great programmer, see how easy it is to read. Variables are well named. Each function has its purpose. It’s effortless reading in part because it’s a well-designed program. It’s something to strive for.
d. Spot bugs better
We’ve got to find a bug before we can fix it, which can be tricky. And here’s where reading code comes in: the more efficiently you can read and understand code, the more quickly you can find and correct the errors. That’s because, as a vast reader of code, you anticipate what each line of code should do and what it should return. You’re familiar with the errors that can occur and can read and understand them, too. This helps you zero in and find what’s causing your issue.
4. Become a Better Reader
Consider using these three ways to get better at reading code.
a. Follow Ben Franklin’s Lead
I’ve written before on a learning tactic that Ben Franklin used to become a better writer. I’ve modified it and used it to become a better programmer. It’s a favorite of mine because it emphasizes reading, studying, and emulating quality code. Here’s how it works.
- Solve a problem.
- Find a programmer who’s better than you and who’s solved the same problem.
- Study their solution. This is a crucial step: read each line of code and type a comment in your editor to explain it.
- Re-solve the same program after some time has passed. Use the comments you typed out as hints to guide you along the way.
- Compare your program to the one you studied.
The benefits of this practice are many. You’ll expand your programming knowledge. You’ll see good code design. You’ll train your eye to see patterns to use certain algorithms for a given type of problem or when it makes sense to use one data structure over another. In short, expose yourself to good habits. You’ll learn a ton and your programming skills will grow.
b. Solve Problems on LeetCode
Solving problems on the website LeetCode offers an important benefit: it provides in-depth solutions to problems. Oftentimes there are multiple solutions with explanations. This is hugely beneficial when it comes to reading code. For one, you’ll see multiple ways to solve the same problem. That way, when you come across a related problem (and you will), you’ll be armed with several different approaches. The solutions may be written in a language you don’t know, and that’s okay. Try to figure it out. Reading code in a language that you don’t know can be a benefit, not a detriment. It’s a practice dealing with ambiguity, which is what we do each day as programmers. Reading code in a foreign tongue gives you that practice.
c. Recycle Your Own Problems
There have been times when I’ve been confused by my own code. (Did I really write that?!) I use it as a learning opportunity. Here’s what this practice looks like. Solve a problem and make a note in your calendar to re-solve in a few days or weeks. Then re-solve it and compare your solutions. Just like tennis players re-watch their matches to study their serve, you’re studying the solutions you wrote. Read both solutions carefully. What were you trying to do originally? What did you do better this second time around, and why? What changed? What would you do differently if you were to solve the problem yet again? The goal here is self-improvement: read your solutions to understand what is better and why.
5. Be a Better Programmer
A programmer does a lot of things. Writing code is one of them. So is reading it. In the end, being able to read code can help you to write it. It’s circular. Reading code matters and we ought to strive to get better at it.
— 0o0 —
As one of leading software development outsourcing companies in Vietnam, Titan Technology always use cutting-edge technologies to deliver all the best outcomes to our partners in over the world along with providing above good environments for any developer will need to explore themselves in this market.
In case you just grew your interest in working with Titan Technology, we are opening for many positions that suit with all your potential.
For your own feeling about Titan, feel free to check our website and submit your CV to us if you already have interest to become a Titaner :
- Get inspired with Titan’s environment here: https://www.titancorpvn.com
- Reach to our incredible Recruitment Team: email@example.com
Source: By Amy M Haddad