Understanding Big Code

The question of understanding big code has answered well before. But I feel that I have to ask this question again in order to ask the problems that I am facing.

I just started working with a student. I am a beginner programmer and just found out about classes two months ago. However, at work I was given a code that is part of a lot of software. I understand what this code should do (in order to read the file). But, having spent several weeks understanding the code and changing it to achieve the desired results, I came to the conclusion that I need to understand every line of this code. The code is about 1300 lines.

Now, when I start reading the code, I find that, for example, a variable is defined as:

VarType VarName 

Now VarType not an int or float . This is a user-defined type, so I need to go to the class to find out what the type is.

In the next line, I see the function being called, for example points.interpolate(x) ; Now I need to go to another class and see what the interpolation function does.

This happens very often, which means that even if I try to understand a small part of the code, I need to go to 3 or 4 different classes and remember them every time without losing the main goal, and this is difficult.

I may not be an experienced programmer, but I want to be able to do this. Can I offer some suggestions on how I should approach this?

Also (I will sound stupid when I ask it), what is a debugger? Hope this gives you an idea of ​​where I am standing (and need to ask this question again).: (

+4
source share
6 answers

In any case, these functions and classes should have at least some documentation to describe what they do. You do not need to know how they work in order to understand what they are doing. When you see the use of interpolate , don’t start watching how it works, otherwise you will find yourself in a deep search for depth in the code base. Instead, read its documentation, and this should tell you everything you need to know in order to understand the code that uses it.

If there is no documentation, I feel for you. I can offer two tips:

  • Make general assumptions about what a function or class will do on its own behalf, such as returning and arguments and the surrounding code that uses it, until something happens, which contradicts these assumptions. I can make a pretty good guess about what interpolate does without reading how it works. This only works when the names of functions or classes are sufficiently self-documenting.

  • If you need a deep understanding of how some code works, start from the bottom and work up . This means that you do not have to remember where you were in some high-level code when searching the code base. Get a good understanding of the fundamental low-level classes before attempting to understand the high-level application of these types.

    It also means that you will understand functions and classes in a general sense, and not in the context of the code that led you to them. When you find points.interpolate(x) , instead of wondering what interpolate does with these specific points with this particular argument x , find out what it does in general. Later, you can apply your new knowledge to any code that uses the same function.

However, I would not worry about 1300 lines of code. This is basically a small project. These are just more than college examples and assignments. If you consider these tips, this code should be easy to manage.


A debugger is a program that helps you debug your code. The general functions of debuggers allow you to step through your code sequentially and observe the change in the values ​​of variables. You can also set breakpoints in the code you are interested in, and the debugger will tell you when it hit them. Some debuggers even allow you to modify code at runtime. There are many different debuggers that have different sets of functions.

+6
source

Try to make assumptions about what the code does based on its name. For example, suppose the interpolate function interpolate your point correctly; just re-encode this bit of code if the output looks suspicious.

+5
source

First, consider creating an editor / IDE that has the following functions:

  • matching parsers / brackets / braces
  • collapsing / expanding blocks of code between curly braces
  • backlight type (in tooltips)
  • macro extension (in tooltips or in a separate window / panel)
  • function prototype extension (in tooltips or in a separate window / panel)
  • quick navigation by types, functions and classes and vice versa.
  • opening the same file in several windows / panels at different positions.
  • look for all references / uses of a particular type, variable, function or class and presenting it as a list
  • call tree / graphing / navigation
  • regular expression search in addition to simple search
  • bookmarks?

Source Insight is one such tool. There must be others.

Secondly, consider annotating the code when you go through it. When doing this, note (write down) the following:

  • invariants (which is always true or should always be true)
  • assumptions (which may be incorrect, for example, the absence of checks / validations or unreasonable expectations), think "what if"
  • goals ( what ) of the code snippet
  • implementation features / details (for example, whether exceptions are thrown and which error codes are returned and when)
  • simplified call tree / graph to see code flow
  • do the same for data stream

Draw charts (in ASCII or on paper / board); I sometimes take pictures of my paper or board. In particular, draw flowcharts and state cars .

Work with code at different levels of abstraction / details . Zoom in to see the details, zoom out to see the structure. Collapse / unlock code blocks and tree branches / call graphs.

Also, provide a checklist of what you are going to do. Check out the items you made. Add as needed. Prioritize work items if necessary.


A debugger is a program that allows you to execute your program step by step and check its status (variables). It also allows you to change state, and this can be useful sometimes.

You can use the debugger to understand your code if you are not very familiar with it or the programming language.

Another thing that may come in handy is writing tests or test data sets for your program. They can identify problems and limitations in terms of logic and performance.


In addition, documentation and people should not be neglected! If there is something or someone who can give you more information about the project / code, use something or someone. Ask for advice .


I know this sounds a lot, but in any case you will end up doing some of it. Wait for a big enough project. :)

+1
source

Basically, you can understand what the function of the called function is at first, and then understand what the input and output of this function is, for example, if you really need to understand how to make interpolate , you can then go to the details. As a rule, the name of a function is self-evident, you can get an idea of ​​what the function does on its behalf if the code is well written.

Another thing you can try is to run some sample toys to get through the code, you can use some debuggers or IDEs to help you navigate through the code. Understanding large-scale code takes time and experience, just be patient.

0
source

"Try the debugger approach"

[Update: debugger is a special program that allows you to pause a running program to check the status of the program (variable values ​​/ which function is running / Who is the parent function, etc.)]

The way I do this is the code debugging step for which I want to understand.

If you are using the Advanced / Mordern IDE, then setting breakpoints at the entry point (for example, main () or the point of interest) is pretty simple. And from there, just enter the function you want to learn or go beyond the function.

To give you a step by step approach

  • Set a breakpoint in the start expression main () (start points).
  • Run a program with active debugging
  • The program breaks at the breakpoint.
  • Now, if you go over until you come across a function / expression that seems interesting. (e.g. your points.interpolate(x); ) function
  • Enter the function and study the state of the program, like variables and the function stack, in real time.
  • Avoid complex system libraries. Just go / go. (Example: Avoid something like MathLib.boringComputaion() )
  • Repeat until the program exits.

I learned that this learning method is very fast and gives you a quick understanding of any complex / large software.

Use Eclipse, or if you cannot try GDB if its C / C ++. Every popular programming language has a decent debugger.

Understand basic debugging operations such as benifit:

  • Setting a breakpoint.
  • Stop at the breakpoint.
  • Check / watch variables.
  • Checking the function stack (function call hierarchy)
  • One step is to go to the next line in the code.
  • The step is to function.
  • Exit function.
  • Go to the function.
  • Go to the next breakpoint (attraction).

Hope this helps!

0
source

Many great answers have already been given. I thought to add my understanding as a former student (not so long ago) and what I learned to help understand the code. This especially helped me because I started a project to convert a database that I wrote in Java many years ago to C ++.

 1. **Code Reading** - Do not underestimate this important task. The ability to write code does not always translate into the ability to read it -- and reading it can be more frustrating than writing it. 

Take your time and carefully study what each line of codes does. This, of course, will help you to avoid assumptions if you do not come across code that you are familiar with and can mask it.

 2. Don't hesitate to follow references, locate declarations, and uncover definitions of code elements you are reading. What you learn about how a particular variable, method call, or class are defined all contribute to learning and ultimately to you being able to perform your task. 

This is especially important because detective work and effective detective work are integral parts of being narrow, in order to understand small pieces of code, so that in the future you can more easily cover large parts.

Others have already posted information about what a debugger is, and you will find that it is an invaluable asset in tracking code errors and, I think, helps with reading code, gaining knowledge and understanding so that you can be a successful programmer.

Here is a link to a debugger tutorial using Visual Studio, and can give you a strong understanding of at least this process.

0
source

All Articles