good or bad: to make giant struct to avoid using globals / properties of a struct / lots of paramerters

StackOverflow https://stackoverflow.com/questions/9145327

  •  22-04-2021
  •  | 
  •  

Question

I'm kind of a beginner, and I have gone off written a 10000 line program which mostly uses globals and void functions, in c++.

Anyways, my program doesn't have a GUI, so I am making one for it using Clutter. So in clutter, you use a signal handling function to connect button clicks, motion events, etc.

The signal handling function can only accept one user data parameter. However, many components of the GUI, hundreds need to be accessed by different functions. So I'm putting all my GUI objects in a single struct, and passing it from every signal handling function.

So my program as it is now, (console program) prints press some letter to do something. If you press that letter, launch a certain function. If I eliminate the use of global, I would need to pass as parameters some of these variables.

If I directly insert my code into the GUI, then the signal handler function will launch the appropriate functions, but can only pass a single user data parameter, which as it is now, is already used as a struct with hundreds of GUI members.

Sorry If this all sounds crazy. I'm just trying to re-write my code to use better practices, but with the 10000 long code, and my lack of understanding of some things, I feel pretty overwhelmed.

I'm just looking for some advice about where to start and how to deal with this issue I am perceiving with connecting to the GUI.

And for my question about structs. I am interested in knowing if there is a maximum number of elements that can be inside a struct. If you have an array inside struct, is the access time for that array going to be slower? Is there a difference in how memory is handled for a struct.

Thanks.

Was it helpful?

Solution

I think your question has a hidden question: How to write GUI application/framework?

Clutter seems very low level from what I quickly Googled. If you want skip a lot coding just use something like Qt Framework which also renders most stuff using OpenGL (or GTK+, depends what kind of license you want).

There's no simple answer if you want to write it more or less from ground up and I'll try to just go over it at a very high level and just highlight the very very basic idea how it usually works.

Since you tagged it with C++ tag, then proper C++ way is to use classes.

You would have classes for things like Sound (MP3), with methods such a decode, encode, etc. Then you would have classes for GUI elements, with methods such as onPaint, onClick, etc.

Let's say you wanted a button. It would be a class with variables such as x, y, width, height, text, clickHandler, enabled and so on. It would have some low level event handlers such as onPaint, onClick (this one would check if the mouse is over the button and if it's enabled, and if it all those checks pass would it would call the clickHandler). It might also have some methods like isEnabled(), disable(), enable(), etc.

Now you're free to make as many button instances as you want, each one has it's own state (enabled or disabled, text, width, height, all of those). You would set a clickHandler for each button.

There are going to be some globals, but most of internal stuff is inside the classes. If you have some sort of hierarchy such a window has many buttons then your Window class would just have a list of buttons. And don't forget classes should each be in a new file unless there are multiple very closely related.

But really you don't want to write anything of this as there are huge libraries already doing it.

As for how to glue it all together you'd probably want to follow the convention used by the specific framework you choose. If you're on Windows and want to make Windows only application then use WPF (or Winforms). Qt is nice for cross-platform stuff and it's quite modern, but the license is a bit constraining if you're making a commercial app.

OTHER TIPS

Accessing an element inside a struct is always slower by the cost of having an additional reference to perform, and thus an extra read to RAM. Try grouping objects into structs so that they will be used together so that these memory requests can be cached in faster RAM/onboard cache.

There isn't really a limit to struct members and they are often useful when you exceed the maximum number of parameters a function should take, I was taught (in 2002) the magic number is 7.

Ultimately, when grouping members into structs you should be able to identify which functions use them and instead of passing the struct you can make the function a member and do away with it having to dereference the struct to get at that parameter's members. I think at this point you can probably award the structs the equivalent C++ label, class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top