Solution to having to scroll all the time to find variables

After my program has reached a certain size, there are simply too many variables to remember. Especially variables in classes. So I constantly have to scroll back up to the class, copy a chunk of all the variables I will be using in a function of the class I am programming much further down in the code, paste them there and comment them out, so I can refer to them quickly when programming the function.

Is there any solution to not have to keep doing that? I am programming in Visual Studio.

When I programmed in Pascal to get around this problem I would put all the variables in a unit, which I would then import in the main program, then I could have two windows open, the unit and the main program, and I could display the variables I needed to lookup from the unit on one side of the screen and the main program on the other. I could do the same with C++ but is there a proper way to get around this problem?
Put your class declarations in a header file (.h) and just open it with notepad.

Alternatively ... print it out!
your IDE should assist with this. The better ones, if you type variable. it will offer a list of what comes after the . (object's members) and the best ones support a comment style that tells you what it is as well (assuming you commented it properly).
So it could be that you have reached a point where you need better tools.

if you develop a style, you will name things consistently and can 'guess' your own names as well.

... visual studio does this, by the way. 2019 at least, not sure about code (vs code and c++ are not well integrated, IMHO)>

this wont work for global variables**, but if you put your stuff in a namespace or 3 you can say namespace:: and here again it will happily give you a list to choose from.

**OK, technically it will with :: but there are about 100000000000000000 things in the global namespace esp if you pulled in windows.h or other microsoftisms (they redefine the entire language, eg BOOL and TRUE and such)
Last edited on
Hello ifStatement,

MSVS 2015, 2017 and 2019 all have the ability to set bookmark in the file. You can also set bookmarks in other files and jump between files.

A good variable can overcome a lot of problems, but I know that you would rather use "a", "b", "c", "x", "y" and all the other letters along with "1", "2" etc.

Using 2 screens or 1 large screen with 2 windows would work.

Andy
As a rule I implement everything in-line first. I'll separate the code into a header or multiple files only if it's required, once the feature is no longer in flux.

I suggest you do the same, since navigating through the codebase is a major time sink, especially when the important stuff is always in another file. So try to limit the number of source files in your project unless there is an overriding issue.

Besides that I split my editor into two panes, so that I can edit code in one while I navigate the code base in the other.

I don't use auto completion feature.

too many variables to remember
The pithy suggestion is to create fewer variables by writing less code.

Are you sure you need every single one? As you've noticed, each name is something new to remember.
Last edited on
If you need that many variables in a class, are your classes too complicated? Trying to do too much in one class? Is a code/design re-factoring required?
I ended up declaring all the variables in a header file and having that file on the left pane in Visual Studio and the main file open on the right. That way I can easily and quickly refer to any variable or construct by looking at the file on the left. I had come up with this solution over a decade ago when I programmed in Pascal and was wondering if there was a better way, but apparently not, at least not on large projects like a game engine.

And yes, the classes are large. I am programming a video game and the Character class especially has a lot of variables and functions because it needs to. Any character needs to be able to have the ability to execute these functions. Separating that into several classes defeats that purpose.

And of course I name things consistently but once you get multiple structures with multiple variables inside multiple classes, it's impossible to remember them all. You would waste more time trying to remember the name of a struct and the variables inside than if you just browse to the class and scroll through to find what you need.

Bookmarks sounds like I'll end up with a lot of bookmarks that I will eventually find myself having the same problem: having to scroll up and down to find the right bookmark that may or may not lead to the variables I'm looking for. Also I do not name my variables "a", "b", "c", "x", "y" and all the other letters along with "1", "2" etc. I name them logical and sensible words that give precise context and meaning to what I am programming, like:

struct consumableItemStruct {
int index;
std::string name, description;
int quantity;
iconStruct icon;
std::vector<Character::statusEffectStruct> statusEffects;
};
std::vector< consumableItemStruct> consumableItems;

Trying to find specific variables by entering the class name, let's say, and typing "::" and then scrolling through the list of all the variables in the pop-up menu is more time consuming compared to browsing to the class in the code and looking at the variables declared there. Because then you have structs and vectors that encapsulate other structs that encapsulate other variables and so on. So it is much faster to just browse to the code where they are declared.

I am programming my own 2d game engine from scratch and it will have many more classes and variables. So yes there are too many variables to remember and removing them is ridiculous as the program will not function at all. This is a game engine, not a small script that returns data to be used by an actual engine.

Thanks for the replies. It looks like for the size and nature of what I am programming, having one file in the left pane and one in the right, solves the issue as best as can be.
my current project over 7000 cpp files at 140MB and over 20k h files with 200mb more.
so yes, people here understand exactly what you are going through :) Its worse when you didn't write most of it yourself, on top of the sheer size. And I am not even sure how this compares to some bigger projects out there, but it serves as an example.
you just have to find your own way to handle it, but do try to let the tools work in your favor. If you don't like the autocomplete, see what else works for you and try stuff until you figure out what does work best for you.
I use a mix of tools for handling it, not just the autocomplete but also grep (cygwin's) to pull context (ok, I found the function/class but how to use it?!) and the class tool in visual and such.

Last edited on
@jonnin: Indeed! My current project is currently 6017 lines long, not including all the libraries that it uses, like SDL2. And there is much much more still to write. Fun stuff though.
Last edited on
ifStatement wrote:
And yes, the classes are large. I am programming a video game and the Character class especially has a lot of variables and functions because it needs to. Any character needs to be able to have the ability to execute these functions. Separating that into several classes defeats that purpose.


Are you using Design Patterns? How many classes are there in the 6017 LOC? It's possible that you have God Classes (Trying to do everything in 1 class)

The Characters with Functions sounds like a many to many relationship, there are design patterns that will help with that. Off the top of my head, I am not sure which ones. Maybe Mediator, or Observer / State?

I found this on the net, there are others:

https://cpp-design-patterns.readthedocs.io/en/latest/

Have you looked at how existing games do things?
Last edited on
your IDE should assist with this.

This. (I presume. Don't use IDE myself.)


There are tools like Doxygen. https://www.doxygen.nl/index.html
Examples: https://www.doxygen.nl/results.html

What it does is generate documentation from the source. Preferably you have comments in source with hints to Doxygen, but you do get something even from uncommented code.

When you have Doxygen output, you can browse it next to your IDE.
Topic archived. No new replies allowed.