How do you understand a large chunk of code?

前端 未结 5 639
梦谈多话
梦谈多话 2021-02-04 09:51

I am a fresh college grad student that just started my job. In my ramp up period, I need to learn a lot of product code. There are some design docs but they do not help much.

相关标签:
5条回答
  • 2021-02-04 10:00

    Run it through doxygen. This will generate html documentation which will be helpful even if the code does not have proper doxygen-style comments.

    Another good advice is to look through the unit tests, if there are any. If there are no unit tests, a good way to understand the code is to write your own unit tests. The effort to do this will pay for itself many times over.

    0 讨论(0)
  • 2021-02-04 10:03

    I think there have been a few good responses already. My 2c worth...

    Not sure what you class as huge (10 KLOC, 1000 KLOC, 10000 KLOC, etc), but one would hope that this is broken down in some way and is not a monolithic single program. Perhaps your management has some guidance on which 'module(s)' you are most likely to be spending time in at the moment. Hopefully this can help break down the problem scope.

    Firstly, before you try to understand the code try to understand the product. What does it do? Then how does it do it? What does it interact with? Then how does it interact? etc...

    When getting to the code try to understand the high level design and philosophy first, and work on the breadth before the depth. I agree with some of the above re fixing some bugs, but I also strongly suggest you continue to get a handle on the high level even if you need to get into the details to fix some bugs.

    I also agree with the above in terms of generating some diagrams for yourself if you can't find any already in existence. And then share them, perhaps a team/product wiki? I'm curious as to why the existing doco does not help very much. Typically this is because this type of doco was generated from the early concepts and the product no longer bears any similarity, but if this is not the case then what can you contribute to this issue. One assumes that where you are today someone else will be in short enough order, and you are in an ideal position to know what essential doco is missing!

    If the product is actually 'huge' then you have to accept that you will never be able to hold all of it in your head, so the best thing you can do is be familiar enough to know where to start looking (comes back to understanding the product, and approaching code breadth first).

    0 讨论(0)
  • 2021-02-04 10:10

    Some people will tell you to start with the data structures, but in a large system even that's not terribly helpful much of the time. I can think of four major points:

    • Take your time. Often, it's more like a whole series of gestalt shifts than it is a single, linear, gradual understanding. So be patient.

    • No matter how big it is, you should be able to put a breakpoint in and walk it in a debugger. Even in a large, complicated, multi-threaded system, you should be able walk through and see what's happening.

    • Ask for bugs, and start fixing them, no matter how crazy they seem. It's akin to dropping yourself into a foreign country; you'll pickup the language eventually.

    • Find a mentor. A jungle guide is invaluable.

    0 讨论(0)
  • 2021-02-04 10:21

    Use every method available to you (in no particular priority):

    1. Use the product itself and understand what it does
    2. Talk to the devs that maintained it or have worked with it previously
    3. Debug through it and see how data flows and how classes interact ("when I click this button, what exactly happens, who is responsible?")
    4. Look at architecture, UML, or class diagrams
    5. One of my favorites: create your own diagrams of class hierarchies, class interactions, general control flow, high-level components, process/DLL interactions, object lifetimes and management
    6. If they're not totally out-of-date, read the dev/test/user specs (goes well with #1)
    7. Read the documentation on it

    Most of all: be tenacious and persistent. If you don't put in the work, don't expect to understand it. If you don't understand something, dig and dig until you do. Software is not magic, it's just hard work :)

    0 讨论(0)
  • 2021-02-04 10:22

    This is obviously a pretty common question, and it's similar to this one (and the questions related to it): How to understand the design and code flow of any product quickly?

    Dig through some of those answers / comments, for starters. Else, we'll just end up repeating them. :)

    0 讨论(0)
提交回复
热议问题