-
Oct 6th, 2009, 10:06 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] What's more efficient?
Say I have two buttons on a form. Both buttons perform relatively the same function, which is displaying a Modal Dialog form. However, each button sends a different set of parameters to the Dialog form to perform different actions.
So, my question is this. Is it more efficient to have one click event handle both buttons (using sender to determine which button was pressed to send the appropriate parameters), or is it better to have two click events (one for each button). I guess another question would be, is there a difference at all?
I know it might look a little cleaner to have one event handling both buttons, would make it a tiny bit easier to modify the code in the future since both buttons would be in the same spot, but is there any advantages to using one method over the other?
This is more for personal knowledge then anything as I personally can't see any performance gain from using one event handler over two.
-
Oct 6th, 2009, 10:15 AM
#2
Re: What's more efficient?
Both are same and there is no question of performance gain or loss. It is just a matter of personal prefrence. If both of them have a lot in common, I would put them in same event handler code and differentiate one from the other using Sender argument. Of they have a very little in common to share, I would prefer coding them as two separate event handlers.
-
Oct 6th, 2009, 10:18 AM
#3
Re: What's more efficient?
Things like this are usually not a matter of efficiency (even if there was a difference it would likely be too small to even measure) but more a matter of maintainability.
Let's say you have not two buttons but 15 buttons doing roughly the same thing, calling some method X, and you handle each of them separately. Now, much later in the development, you have to change something and do something else before you call method X. If you handle the 15 click events separately, you have to change 15 event handler methods. If you handled them in one method, you only have to change it once.
-
Oct 6th, 2009, 10:20 AM
#4
Thread Starter
Fanatic Member
Re: What's more efficient?
Yah, i figured there wasn't much to gain using one over the other. Although using the one event handler over the other I get to write like 4 lines less of code. Really the only difference is one parameter which determines an account type. Other then that both buttons perform the same function.
Let's say you have not two buttons but 15 buttons doing roughly the same thing, calling some method X, and you handle each of them separately. Now, much later in the development, you have to change something and do something else before you call method X. If you handle the 15 click events separately, you have to change 15 event handler methods. If you handled them in one method, you only have to change it once.
Yup, definitely know this as I run into it a lot when handling radio buttons. Makes things a lot neater to be able to change the code all in one place then alter it in 15 different spots. Plus I'm slightly lazy and it's easier typing in the handles clause then generating a code stub for each radio button
-
Oct 6th, 2009, 10:37 AM
#5
Re: What's more efficient?
Technically, the two buttons will be faster because you don't need to change cases based on Sender, but you will never be able to show the difference in practical terms.
My usual boring signature: Nothing
-
Oct 6th, 2009, 10:39 AM
#6
Thread Starter
Fanatic Member
Re: What's more efficient?
interesting one would think that using slightly less code would be more efficient (even if it's not discernible to the naked eye). I knew that I wouldn't be gaining or losing anything major by choosing one way over the other, just a question that popped into my head when I was writing out the code for the buttons. Couldn't really come up with an answer so i figured someone on here should know
-
Oct 6th, 2009, 10:59 AM
#7
Re: What's more efficient?
I follow this general rule: If you have two or more objects with events that's got the same (or very similar) code, have them all in the same event handler sub.
If you have 2 or more spots where it's got the same code, move it to a Sub and call that sub from all locations.
-
Oct 6th, 2009, 11:05 AM
#8
Re: What's more efficient?
Total lines of code means little or nothing. Consider that when you turn Option Strict ON, you have to type more code to do all those explicit conversions, but your code will run faster. Loop unrolling, which is no longer really practised, was another means of increasing speed by typing more total lines.
My usual boring signature: Nothing
-
Oct 6th, 2009, 11:06 AM
#9
Thread Starter
Fanatic Member
Re: What's more efficient?
Yah, I'm a pretty big advocate of code reuse. I was really just in the dark about any possible performance gains or losses from using one method over the other. I wasn't really expecting to gain anything major, if anything at all.
Total lines of code means little or nothing. Consider that when you turn Option Strict ON, you have to type more code to do all those explicit conversions, but your code will run faster. Loop unrolling, which is no longer really practised, was another means of increasing speed by typing more total lines.
what about compile times? Technically wouldn't a program with more lines take longer to compile then code with less?
-
Oct 6th, 2009, 12:01 PM
#10
Re: What's more efficient?
Probably, but so what? If you managed to get a project of such size that something like this mattered, then you can goof off during that time.
My usual boring signature: Nothing
-
Oct 6th, 2009, 12:32 PM
#11
Re: What's more efficient?
Originally Posted by Shaggy Hiker
Probably, but so what? If you managed to get a project of such size that something like this mattered, then you can goof off during that time.
Ahh, imagine being able to sleep in front of your desk for 3 microseconds longer because you added 3 more lines to your code... What a life...
-
Oct 6th, 2009, 03:21 PM
#12
Re: [RESOLVED] What's more efficient?
It isn't how long it takes, it is how long you SAY it takes.
Edit: I should add that I think every computer-related comic strip has used this device for at least one strip, if not several.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|