How to divide a project? (For readability)
-
My QMainWindow project has more than 2000 lines code and it's getting bigger day by day. But this situation causes complexity and prevents readability. What should i do for this case. How can i make my project more readable. I need suggestions from specialists.
-
Did you put everything in your MainWindow.cpp / main.cpp? So you have 2000 lines in one file?!
2000+ LOC are not a problem if it is necessary, but normally you should avoid that and separate your functions from, for example, your class headers, divide your program into classes, which are given specific tasks (if you haven't done it already) and bring structure to your project.
It helps, if you write down first, what you are planning to do and which classes, variables and connections you need to realize that.
(In text form or create an UML class diagram)Did you understand the basics of object-oriented programming?
What kind of program do you work on? Are you new to programming in general or did I get your question wrong? If I did, I'm sorry for that :)
-
@Pl45m4 You got my question right. I have some seperate classes like chart class and clickable label or qthread classes. Totally i have over 4000 lines. Like i said i have over 2000 lines in mainwindow.cpp. It's simple desktop project that shows camera feeds, some database stuff, chart, logging , other windows etc.
Is it possible to write member functions in another file. like:
void MainWindow::someMemberButtonClickedFunction() { // all my these types of member functions are in the mainwindow.cpp file }
Is it a good practice to seperate them into another file, is this possible?
By the way, i am not well experienced at programming. I am currently trying some languages and trying to do projects with them. I liked C++ and i want to improve myself with C++ and GUI.
-
So your 2000 loc are mainly event handler functions for buttonClick events or text inputs?
Maybe you can find a way to split your MainWindow class to outsource some tasks to their own class. This maybe saves a few hundred lines.
Or you take all your slot / eventhandler functions and put them into an extra file, "mainwindow_slots.cpp" or something.
I wouldn't recommend that, because of 2 things:First, there is a reason why these slot functions are in the mainwindow.cpp. If you separate them, you have to tell your linker, where your slots are and this can cause more trouble and confusion.
Second, if the main part of the 2000 lines are slots / events, you just move the problem to another file ;)
Better try to thin your code wherever you can.
In addition, you can hide blocks of your file by clicking on the down-arrow next to the line number (Qt creates those blocks automatically).In C# it is possible to define #regions of code, which you can name as you want and set the end of one region wherever you want.
You can close those regions afterwards and save some lines (it just hides the code).Dont know if this is possible in Qt / C++ as well.
-
@Pl45m4 That's what i want to hear: advices. Thank you for any of them.
I think that's a good point for programming, especially for medium scaled and above projects. If anyone write some advices, suggestions just under here it will be good for later readers. I think i can give an advice too :)
- Don't ever never start a project before you designed the project with all its details and keep it flexible for later changes.
This is the lesson that i extracted from my experience on this project.
- Don't ever never start a project before you designed the project with all its details and keep it flexible for later changes.
-
ok, I just found out, that the code management comes with your IDE. The #region tag only works in Visual Studio :(
What you could do, is redeclaring your namespace. You can hide all (or at least some) functions, you do not have to work on anymore.
In case of style, it is not the best solution but it can help in your case, keeping your code readable (for you).foo.h
namespace bar { class foo { public: foo(); ~foo(); } }
foo.cpp
// includes.... namespace bar { // Constructor, Destructor } namespace bar { // Stuff you want to hide } //
It's not a good solution, but it can help.
I think that's a good point for programming, especially for medium scaled and above projects.
Yeah, in some cases it helps a lot, if you write something down first or create an UML diagram.
I had some projects for school / university, where I thought, I dont need any plan because I'm gonna use only 1 or 2 classes anyway.
After writing headerfile no. 7, I wished I had made a diagram before ("Well, that escalated quickly") ;-) -
Seperation of Concerns (SoC):
https://en.wikipedia.org/wiki/Separation_of_concernsThere are design approaches that go further, but SoC is a good place to start.