Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding large code [closed]

Tags:

c++

The question of understanding a large code has previously been well answered. But I feel I should ask this question again to ask the problems I have been facing.

I have just started a student job. I am a beginner programmer and just learned about classes two months back. At the job though, I have been handed a code that is part of a big software. I understand what that code is supposed to do (to read a file). But after spending a few weeks trying to understand the code and modify it to achieve our desired results, I have come to the conclusion that I need to understand each line of that 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 is not a type like int or float. It is a user defined type so i have to go the class to see what this type is.

In the next line, I see a function being called, like points.interpolate(x); Now i have to go into another class and see what the interpolate function does.

This happens a lot which means even if I try to understand a small part of the code, I have to go to 3 or 4 different classes and keep them in mind all at one time without losing the main objective and that is tough.

I may not be a skilled programmer but I want to be able to do this. Can I have some suggestions how i should approach this?

Also (I will sound really stupid when I ask this) what is a debugger? I hope this gives you an idea of where I stand (and the need to ask this question again). :(

like image 449
detraveller Avatar asked Dec 15 '22 12:12

detraveller


1 Answers

With any luck, those functions and classes should have at least some documentation to describe what they do. You do not need to do know how they work to understand what they do. When you see the use of interpolate, don't start looking at how it works, otherwise you end up in a deep depth-first-search through the code base. Instead, read its documentation, and that should tell you everything you need to know to understand the code that uses it.

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

  1. Make general assumptions about what a function or class will do from its name, return type and arguments and the surrounding code that uses it until something happens that contradicts those assumptions. I can make a pretty good guess about what interpolate does without reading how it works. This only works when the names of the functions or classes are sufficiently self-documenting.

  2. If you need a deep understanding of how some code works, start from the bottom and work upwards. Doing this means that you won't end up having to remember where you were in some high level code as you search through the code base. Get a good understanding of the low level fundamental classes before you attempt to understand the high level application of those types.

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

Nonetheless, I wouldn't worry about 1300 lines of code. That's basically a small project. It's only larger than examples and college assignments. If you take these tips into account, that amount of code should be easily manageable.


A debugger is a program that helps you debug your code. Common features of debuggers allow you to step through your code line-by-line and watch as the values of variables change. You can also set up breakpoints in your code that are of interest and the debugger will let you know when it's hit them. Some debuggers even let you change code while executing. There are many different debuggers that all have different sets of features.

like image 195
Joseph Mansfield Avatar answered Dec 29 '22 04:12

Joseph Mansfield