I am in the process of cleaning up my code and making it easier to maintain. I am doing this by turning my 5000+ line file into separate smaller files.
I have successfully created separate source and header files for obvious things like utilities and other components that are not tightly integrated with the application.
I am now thinking it would be good to break up the rest of my application into smaller files also. But the issue mostly concerning me is that everything that is left uses a bunch of Structs that are Globally declared.
So this is the choice moving forward (happy to hear other suggestions also):
1) I could leave everything in the same file and that way I could just define all functions as static (which has its benefits, and is often recommended). It is then a simple choice to leave the Globals at the top of this large source file.
OR
2) I could separate into smaller files. It is relatively simple to work out which parts should go into which new files. It is also not too difficult to work out the structural hierarchy (which parts call which parts). But I am uncertain on how best to deal with all these Globals. Where should they be placed, since every function uses them in some way?
NOTE: I am aware that the use of Globals should be avoided. However this is an embedded device with limited memory. Globals are the best way to reduce memory usage, and the more memory I save, the more I can perform a certain task.
4
I would recommend typedef-ing all the structs, and putting those declarations in a separate include file. Then define all your actual instances of the structs in whatever your “main” file ends up being. Then, simply include the header and declare the needed instances extern
in the other files. The linker will take care of matching up all the references, it shouldn’t be any problem at all.
2
There is a third, hybrid alternative, which is to put each global into its own module and write functions to support it. This gives you the OOP-y benefit of all code relating to the variable being isolated in one place and much easier to re-use elsewhere if needed. Putting the support functions in the header so they can be inlined instead of forcing calls gives you the benefits of having everything in one big file.
For example:
// foo.h
typedef int FooType;
extern FooType foo_global_do_not_use_directly;
static inline void foo_init() {
foo_global_do_not_use_directly = 0;
}
static inline void foo_set(FooType new_foo) {
foo_global_do_not_use_directly = new_foo;
}
static inline FooType foo() {
return foo_global_do_not_use_directly;
}
.
// foo.c
// Always include the header even if you're not using a
// typedef so it gets compiled at least once.
#include "foo.h"
FooType foo_global_do_not_use_directly;
You will, of course, have to go through the rest of your source and adjust every use of your globals to use the functions instead of referring to the symbols directly. In the process, you may find other things that are done repeatedly and deserve to be their own functions, which may have benefits down the road if you ever need to change how those operations work.
7
I’d go with your Option 1: leave the globals in a single file where it’s easy to find them.
As you’ve noted in the comments, this is not OOP. The “rules of OOP” (while very good in the general sense) don’t necessarily apply to a smallish project on an embedded device.
5000 lines in one file is a pain to work with. 5000 lines in a solution is pretty small. If you’ve already moved the other stuff out, chances are what’s left isn’t all that big. If you split it up you’ll just spend a lot of time searching for the one global you want to change.
Remember, the goal is to keep the code organized so you can clearly follow the structure, make needed changes (and understand their impact) and separate “stuff that changes” from “stuff that doesn’t”.
OOP is the common way of meeting that goal on modern general purpose computers. It’s not the only way to do things, and it may not be the best way on a memory-limited embedded device.
1