Bloat

I understand the usefulness of C++ object programming and getting base classes to do only what they need, extending functionality out into lower down classes as needed. I also understand that if the one class was to contain what it required then the same code would need to be copied from class to class, and that doing this will increase compile times. But to me it is like a spaghetti of confusion, where what should be all together in one class becomes separated out into a confusing hierarchy, and any loss of time compiling is more than made up for in time saved not being confused. The question I have is, does it make any difference to the executable size? After all, either way gets the program to do the same things.

… ? more code = more binary.

If you’ve ever had to deal with a 63,000 line of code PlayerController, you would probably appreciate separating things by their concerns more :slight_smile: … and i’m told that before I got to be the person handling this 63,000 line of code PlayerController, it was actually 150,000 lines before someone had started splitting it out to other classes.

Personally, for new code I am writing, I prefer to keep things as simple as possible, and make sure that each file does as little as it needs to to perform a very specific function.

Using components and subclasses well will prevent you from ever having to deal with a 63,000 line of code class.

2 Likes

There is a tradeoff between functions/classes being too long and too short. If they are too long then the scope of variables is too large (unencapsulated) and so reading it becomes like untieing a big complicated knot. If they are too short then you end up jumping around from file to file and function to function too much (too much indirection).

My rule of thumb is that a (non-one-liner) function should be about one screenful. A source file (class) should be between 300-1000 lines. Those sizes seem to be the best balance in my experience.

Beyond raw size, it’s also helpful if the separation of concerns is actually sensical. They say that naming things is one of the three hardest problems in computer science. :slight_smile:

Thank you for the answer. More code, more binary is succinct. I would have thought however that the compiler/linker process would strip away all non used stuff when setting up the function tables. Not more code, more binary, but more used code, more binary. In which case the multiple inclusion of the same function signatures (purposes) would be pared.

If it’s in a different code unit, it’s different code.

I think what you’re talking about is a family of optimizations called “dead code elimination”, where machine code that is determined not to be reachable is stipped out of the program (or dynamic library). See Dead-code elimination - Wikipedia

If you’re curious how C++ compilers and linkers work there is a good site called Compiler Explorer at https://godbolt.org where you can enter some C++ code and see what the actual machine code output is. It’s generally considered a good idea for a programmer to study this topic (at least a little).

Thank you guys. That answers the question. Buckle up I guess.