CodeGym /Java Blog /Random /Why Do You Need to Read People's Code and How to Do It Ri...
Lucy Oleschuk
Level 31

Why Do You Need to Read People's Code and How to Do It Right

Published in the Random group
If you'd like to become a good writer, it's not enough to know grammar. You also need to read lots of books that will show off how this grammar is used to create prominent texts. Similarly, if you'd like to become a good programmer, it's not enough to simply learn Java. You also ought to read lots of quality code samples written by other programmers, and most importantly, understand and learn from them. Actually, exposure to high-quality examples of codes is one of the key factors that influence how quickly and effectively you'll learn new skills. Why Do You Need to Read People's Code and How to Do It Right - 1

Importance of Reading Others' Codes

"I hate reading other people's code" is a common mistake that Java developers of all experience levels make. However, it's a very welcome skill, especially for those who'll be rolling onto existing codebases or are planning to work on big enterprise-level software. The thing is, most likely, you'll need to constantly modify other people's code to fix bugs or add new features to a myriad of already-existing ones. And all these tasks call for deep examination of the already-existing code base. But if you have the right strategy and the right tools to work with it, copying or modifying others' codes can be an enjoyable and advantageous experience. As a learner, you can get a fantastic experience by simply reading codes written by Java specialists. They will give you an understanding of how any part of the project works and provide you with useful insights about how it's done. This, in its turn, will help you significantly improve your knowledge and eventually develop your own style.

How to Dig into Code

When you're diving into others' code, you may feel more like an archeologist-investigator rather than a developer. There is nothing wrong with that as you've got lots of "shovels" at your disposal. Nowadays, you have access to a lot of metadata that will help you understand the code much easier. For example, you may use Git to help yourself when reading, copying, and pasting the code. Yet, some key points can help you understand the code better initially. First of all, you should ask the following questions:
  • Is there anything in the code that looks unfamiliar to you? If so, review those "blank spaces" and make sure you understand what they are used for.
  • Is there anything unnecessary in the code? Dead codes also exist, especially if we're speaking about large codebases.
  • Does the code have any dependencies? Sometimes it's better to remove that dependency by copying/pasting even more code.
  • Are there any errors that occurred after pasting?
Another recommendation is to find what the code does and trace those actions backward. For instance, if you know that the code you're viewing creates a file with a list of movie titles, try to figure out in what specific lines the code generates that file. Next, move a step backward to find out how the code places the info in that file. Then, move one more step backward to understand where the data comes from… You've got the idea. The said pieces of the code can be called a "chain of actions." which may give you a great insight into such things as:
  • how the body of code is actually built;
  • the style of coding;
  • how the programmer who wrote the code solves the problems.
Likewise, you may try the following 4-step process to learn more about the code you're working on:
  • Run the code and explore the results. Running the code will give you the essential information you'll need to understand it better.

  • Find its main function and the start point.

  • Run the code with the debugger (you may find the most efficient Java debugging tools here) to fully understand the code's mechanics. By doing so, you'll get an in-depth analysis of the inner functionality of the code you're reading.

  • Build a mindmap of the connections between different elements in the code. As any debugger will show you a connection between the elements, you'll be able to correlate different functions and understand how they interact with each other.

By investigating the code in the said ways, you'll eventually understand more and more of the specific code (and how its parts are connected). Naturally, the more you know about a code, the more you understand the entire codebase. In other words, if you explore and use high-quality code examples, the easier it will become for you to read and understand all other codes over time.

What's the Catch?

You may still wonder what's the point of reading and understanding other programmers' codes? Actually, it's an excellent opportunity to understand how ready-made modules work "under the hood" and learn something new. And if you feel that your brain is ready to process the information in other way than just reading theory and practicing, probably it's high time that you polish up your Java skills without much additional effort.

Other "Side" Effects of Using Other People's Codes: Boost of Confidence

Even if you've read the source code of some OSS (open-source software that is readily available to everyone) but didn't understand anything, don't worry. That's completely fine, especially when you're just learning. It may just mean you're not ready yet. However, if you've managed to understand the code, then you'll definitely get a boost in your confidence as a Java developer. When you can figure out exactly how some real-world project, software program, or app works, then you surely already know a lot about programming. Hence, there is no way to stop your learning process. That just being said, at CodeGym, we don't recommend lifting too heavy weights at the beginning. Don't dig into large applications or operating systems, as this experience might be pretty frustrating rather than inspiring. Instead, start small and refer to our "Help" section, where you can train to read other students' codes, give them hints or learn from their experience. Also, you can discover how your peers solve the same tasks. Another recourse that may catch your fancy is the StackOverflow community, where the codes written by other programmers are usually fairly structured, formatted, and already commented. Gradually, the habit of reading high-quality (yet not overly complicated) codes from your peers will bring you to a whole new level of programming.

What's More?

Reading codes regularly will also encourage you to use them later in your projects, and eventually modify and accustom them to your specific needs. Of course, if we're speaking about open source software codes where the license allows you to modify them. Such code will save you a lot of time on creating projects from scratch and open you to a new level of programming. As you see, reading, copying, and pasting code can help improve your skill and save significant time. And those extra time will help you solve the right problem and supercharge your velocity. Actually, sometimes copying and pasting an already-existing code is the best course of action, but always remember about the license terms and the full submersion into the code you're copying. GitHub, GitLab, FreeCodeCamp, or SourceForge are the best no-fault open resources that let you glimpse into other developers' codes.

Conclusion

No programmer can do without reading old code as part of writing new code. And the longer you're programming, the more different codes you see and easier it gets for you to understand them. Remember that when it'll be easier to read code samples, it'll be easier to write new ones. In a word, it's a fantastic self-perpetuating cycle where you gain the ability to understand others' codes quicker and more effectively. From this, you'll also see positive gains in your own coding. So, if you're after fewer pauses and more progress, don't neglect reading, understanding, and modifying already existing codes!
Comments (2)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Captain Anton. Pirate of Asteroids Level 2, New Nebula Galactic City, India
12 January 2022
Where can I find high-quality java code to read?
Luis Gentil Level 10, Sevilla, Spain
30 December 2021
Muchas gracias, Lucy, por tu artículo, realmente es muy interesante. Puede ser duro leer código, pero con tus consejos será más fácil.