Okay, Kaz and I posted at the same time, and he basically preempted the "we're still not done" portion of the show.
The VERY interesting implecation of all this is that...
Object-oriented programming roxxors and function-oriented code sucks it. WOOT! Go OO!
Okay, back to calm reason.
The thing I'm see is that if you start grouping methods together by object rather than by function, you start to see the natural emergence of objects, and therefore,
classes in the code.
Code:
class EditWindow {
protected:
std::string m_strCaption;
HWND m_hWnd;
public:
EditWindow( HWND hWnd, LPCTSTR caption ) {
m_hWnd = hWnd;
m_strCaption = caption;
}
void Init(void) {
Edit_SetText( m_strCaption.c_str(); }
EnableWindow( m_hWnd, TRUE );
ShowWindow( m_hWnd, TRUE );
}
};
...
BOOL InitLoginDialog( HWND hDlg ) {
EditWindow userName( GetDlgItem( IDC_USERNAME, "Enter username here" ) );
EditWindow password( GetDlgItem( IDC_PASWORD, "Enter password here" ) );
EditWindow confirm( GetDlgItem( IDC_USERNAME, "Confirm password here" ) );
}
Now, that's not really much better that the first round of optimization. If this were a Win32 C++ program without MFC, I would have left it at the first optimization.
We've started to reach equilibrium with regard to the EditWindow objects: Each window has a caption and a resource id that can be converted into an HWND; with every window we want to perform the same three actions: set text, enable, show. Any more gains with this program will probably be made at a higher level.
Let's go back to the InitLoginDialog() method. We're passing in hDlg, about which there are two significant things we observe: first, it's the handle to an instance of a dialog, and second, it's used to convert dialog resource ID's into HWNDs. Okay, that's where it comes from and what we use it for... but, so what?
Think about that first implication. It's the unique identifier of
an instance of a dialog
class. I said a few posts back that this system has 20 or so of these dialogs. What if we could turn our dialog function into a dialog class member function?
I'm going to stop here. This way lies MFC/ATL/Swing/TCL/Qt/wxWidgets or whatever windowing framework you care to use. You could take the parent function and make it a dialog method, bind the three controls to a custom class that inherits from an existing Edit class, etc. Eventually you end up with code where the binding of the resource id to HWNDs is done for you, and all OnInitDialog() has to do is call Init() on its member objects.
This code discussion optimized the code until we needed a Windowing framework to go any further. What I find interesting is that
having a windowing framework at hand does not necessarily lead you start with any such intelligently optimized code.
The code I posted first (option #1, grouped by method) was taken directly from my project today, from inside an MFC class. (I removed all the CDialog stuff to keep the snippet uncluttered.)
Iddint
dat inneresting....