wxFrame - 4000+ lines of code (A question of style)

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Ishtar
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon May 20, 2013 6:33 pm

wxFrame - 4000+ lines of code (A question of style)

Post by Ishtar »

Dear All,

My project is now getting relatively large. In total, it has about 50 classes. The problem I am having is that my initial wxFrame class now has over 4000 lines of code (not including comments or spaces) and is increasing. Recently, I have split this class into three cpp files (all sharing the same header). For example, one cpp file has the event handlers, the second has the main logic and the other all the UI creation functions. Still, it's the same class with a lot of code.

Considering the fact that my main UI (wxFrame) class controls the user interface, it, of course, has many private wxWidgets object variables which need to be in scope (I don't really want to be passing large numbers of wxWidgets objects into other classes). So, what is the best way to refactor key parts of this code into separate classes? Should I use friend classes or is there a better way?

Many thanks
Amanda
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxFrame - 4000+ lines of code (A question of style)

Post by doublemax »

Without knowing about the project itself, it may be hard to make any suggestions. One big "God" class that does most of the work usually indicates a bad program structure. Ask yourself if you can divide the work into smaller classes.
Use the source, Luke!
Ishtar
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon May 20, 2013 6:33 pm

Re: wxFrame - 4000+ lines of code (A question of style)

Post by Ishtar »

Thanks Doublemax,

Yes, creating a "God" object is what is concerning me. However, the class mentioned only handles the main user interface and all of its associated controls. For example, drop-down menus instantiate and invoke other classes which then handle their own specific job and other functions are passed into separate threads etc.

What I would like to do is split up the main UI class into at least two other classes but am concerned as to the best way to do this. I have a lot of wxWidgets objects (wxTextCtrl, wxComboBox, wxCheckBox etc) which will need to be accessed by the event handlers and other functions. All of which are related to the main UI and not logically disconnected from the main UI.

I have been considering creating one or more friend classes to handle the events and UI creation etc. Is this a good idea?

Kind Regads
Amanda
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxFrame - 4000+ lines of code (A question of style)

Post by doublemax »

I'm not a big fan of friend classes, actually i never use them. And in this particular case, i think it would just hide the issues, but not really solve them.

I guess you can't show a screen shot of the application?

Ask yourself if there are parts in the GUI that logically belong together. I.e. can you group some controls in a wxPanel? Then the control creation would happen in that panel (which you would subclass) and the rest of the program would communicate with the controls only indirectly through that panel.
Use the source, Luke!
Ishtar
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon May 20, 2013 6:33 pm

Re: wxFrame - 4000+ lines of code (A question of style)

Post by Ishtar »

Thanks for your help Doublemax.

I can post a screenshot but I am not using the right computer at the moment. I could post one tomorrow. However, you have got me thinking more clearly now about the structure. I have most of my controls on the right-hand side of my UI which change depending upon the configuration. Of course, these dynamically changing controls add a lot of code. All these controls are already in a top level wxPanel. Perhaps I could subclass this panel and then do as you suggested. Sounds like a good idea...

Many thanks
Amanda
RP__
Earned some good credits
Earned some good credits
Posts: 124
Joined: Tue Jan 20, 2015 5:53 pm

Re: wxFrame - 4000+ lines of code (A question of style)

Post by RP__ »

Hello Amanda,

Could you also perhaps share a screenshot of the interface when it is running?
Perhaps I could give you some advise on usage and placement of the controls, if you'd like.
You can also send me a private message.
MagickPanda
Experienced Solver
Experienced Solver
Posts: 81
Joined: Wed Oct 19, 2016 1:41 pm

Re: wxFrame - 4000+ lines of code (A question of style)

Post by MagickPanda »

i am kinda having the same problem - one big monolithic wxFrame class that contains everythign and anything, though the good side is global variable usage are somewhat reduced to minimal, the drawback is this god class is too big/hard to maintain sane.

Any suggestion/improvement is appreciated.

My class is like

class MyFrame
->m_Loader (resource and i/o load/save)
->m_Trader (handles connection to trader API)
->m_Quoter (handles connection to quoter API)
->m_SubFrames (contains sub-window that are children of MyFrame)
RP__
Earned some good credits
Earned some good credits
Posts: 124
Joined: Tue Jan 20, 2015 5:53 pm

Re: wxFrame - 4000+ lines of code (A question of style)

Post by RP__ »

MagickPanda wrote:though the good side is global variable usage are somewhat reduced to minimal
Why not construct a child control/object and pass the 'global' as a pointer?
Keep it alive in the parent class to keep the global usage to a minimum.
At some point you have to make concessions to achieve your goal.
Ishtar
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon May 20, 2013 6:33 pm

Re: wxFrame - 4000+ lines of code (A question of style)

Post by Ishtar »

Fortunately, the vast majority of my controls are in a wxScrolledWindow so I plan on subclassing this entire control. Doing this, I should be able to farm out about 60% of my code into another class. Should work, finding the time to do this is another thing. For now, it'll have to stay a big class. :(
MagickPanda
Experienced Solver
Experienced Solver
Posts: 81
Joined: Wed Oct 19, 2016 1:41 pm

Re: wxFrame - 4000+ lines of code (A question of style)

Post by MagickPanda »

RP__ wrote:
MagickPanda wrote:though the good side is global variable usage are somewhat reduced to minimal
Why not construct a child control/object and pass the 'global' as a pointer?
Keep it alive in the parent class to keep the global usage to a minimum.
At some point you have to make concessions to achieve your goal.
I think it will be problematic if i put the stuff inside main frame class as child, since many child-class has reference to the global mainframe class pointer, the key problem is the inter-class communication. Though i found it's quite hard to make child-class completely independant from mainframe class, since alot of processing/GUI-updating needs to be done in mainframe or signals must be spawned from mainframe class.
Post Reply