-
I have this question that really bothers me, and it has come time in my app to ask myself this question one more time. Is it good programming technique to put a massive amount of code in the Click event of a button? For example, I am writing the save routine of my app right now, and I'm putting all the save code into filSave_Click, the Save button on the menu. Is that good practice, or should I create a Save subroutine? Or should I put it all in a module, global to all forms? Thanks for the input!
-
I put as much code as I can in Modules and Class Modules.
That way i can resuse a lot of the code in that project and other projects, also if your debuging it makes so you can isolate errors better.
-
What are you planning to do with your app? The more you expand it, the more you notice it's better to have it organized, especially save routines should be put either as a private sub in the form for simple SDI apps, or global for apps with several forms calling the save routine.
-
It has gotten rather large, and thus, I REALLY am thankful that I have it somewhat organized. It's an MDI app, so I don't want a global save routine, I don't think. I want each form to be able to save its own data, so I think the best way is to keep the Save routines as private subs. So it is good technique to keep code out of the contol events as much as possible?
-
Yes, that way you keep track of your methods if they are named according to their functionality rather than having a comment above the event. it's not wrong to have code in the events though, i usually have a lot of condition checks and maybe several calls in the event to keep the methods as general as possible and having the detailed parameters leaved to the events.
-
If you have alot of code that repeats itself in different forms then you should probably break them up into global functions to minimize your code and make it more readable. It also helps if something unexpected happens later, and you need to find a bug.
-
There are many factors that affect this:
1) The complexity of the App. If the App is the next MS-Word or Excel, there's no way I'm stuffing all the code in the Form. On the other hand, if I'm making a 'Hello Word' App, I wont put it in a module.
2) Resuable code. If I need to call a saving routine more than 2 or 3 times, it's easier and more organized to use a standard module and simply call the function from there.
3) Size of code. If I only need to call a function once, but it's over 200 lines long, I would rather place it in a module.
4) Orgainzed Code. If I need the coordinates of 5 rectangles, it's much easier to create a Class Module or UDT for them, than to have 20 variables.