Methods or skills needed to read the code of others

Currently, I started work on a very large project, Legacy, and in a situation in which I was always afraid of Reading and Understanding Other People code , I knew that this was a necessary skill that was required, but did not develop it, since it was not required before the date and Now it’s like a need to develop this skill, not a hobby, and therefore I would like to learn from SO Readers about:

  • How did you overcome the obstacle to reading other people's code?
  • What methods or skills have you developed to polish your art of reading and understanding other people's code?
  • Are there any books or articles that you referred to, or in general, how did you develop reading and understanding skills of other people's code?

I would really appreciate helpful answers to these questions, since now I can understand how one would feel when trying to understand my code.

+4
source share
8 answers

Practice. Practice. Practice.

I overcame the obstacle by interacting with people in open source projects. Discussing my contributions with others, and seeing how their suggestions and ways to look at things really opened my eyes.

I suggest you find a project that suits you, check the source and contribute (whatever is important). Over time, the ability to read code should occur naturally. Some projects even offer mentors specifically to help new entrants.

+4
source

Michael Feathers Effectively works with Legacy Code - an excellent resource that contains a large number of methods for working with old code.

+2
source

Practice, practice, practice.

If you can, talk to someone who wrote the code or has an idea about it. Draw a lot of photos and ask them to explain great things to you while you write comments.

The fastest way to find your way is to get lost. Immerse yourself in the code and put things together. See if you can change int to string or something else.

+2
source

Patience: Understanding that reading code is more difficult than writing new code . Then you need to follow the code, even if it is not very readable, since it does its job and in many cases is quite efficient. You need to give a code of time and effort to understand this.

Understand architecture: Best if there is documentation on this. Try talking to people who know more about this, if available.

Check this out: You need to spend some time testing and debugging the code so you know what it does. For those parts that you understand, write some unit tests, if possible, to use them later.

Be unpretentious: Many times template names are used incorrectly. Classes have names that do not indicate their purpose. Therefore, do not think about them.

Learn refactoring: The best book I've found on this topic, Refactoring: Improving the Design of Existing Code - Martin Fowler. Working efficiently with legacy code is another amazing option.

+1
source

I don’t know how to do this, I participated in several projects where I have to decide how they think and reach this solution, so I can do it too.

That's why every time I can, I recommend that if you are a developer, try to comment on the code, everything that you can, because, you don’t know if it will find it useful (I think I'm praying)

If you are not a developer, you should find someone to support you.

0
source

How do you learn to read what other people have written? You write yourself very well and try to make the person as good as a writer as much as possible (suggest things that they could do better, for example, adding some comments). Unfortunately, the code is almost always read by someone other than the author. Practicing and getting to know some people may help other code. But whenever you spend more than 5 minutes figuring out what a particular line means, pay attention to how they could do it better, and make sure you never make the same mistake.

Good luck .: D

0
source

When I get closer to an unfamiliar code base, I like to start from the beginning. Find main () and write a summary of what main () does. Create a list of functions / methods called in main (). If you are visually, create a main () flowchart.

Once you have a list of methods called directly from main (), find these methods and repeat the process. When you find out what each of these methods does, write it in the JavaDoc format and paste it into the appropriate field in the flowchart. If this is an API call, indicate which API it uses and place a link to the appropriate API documentation.

Working recursively, you will create an application map and find out what the program actually does. Once you know what he is actually doing, you can find discrepancies between what he does and what he should do.

0
source

The answer depends on the tools and documentation available. If any documentation is available, I try to understand the general overview of the system - various modules, interfaces and interaction with the subsystem. This helps to divide and subdue the code into significant parts when you read the code. If any design patterns are commonly used in code, try to build your knowledge about it. Also, depending on the tools available, I can use any of the popular source code browsers (Source Navigator / Source Insight) to quickly view dependencies (class hierarchy, etc.). This speeds up code understanding. Also, if I have some kind of convenient unit testing system, try playing with the code - different inputs and the expected result. I also recommend using a debugger to perform selected complex functions in order to get code stream freezes.

0
source

Source: https://habr.com/ru/post/1333608/


All Articles