I’m currently tasked with cleaning up, bug fixing and optimising a Form in winforms (3000 lines of code in one .cs file, it’s getting a bit ugly!). I’ve noticed a few obvious bad practices and some redundant calls already which I could sort out relatively easily.
However there’s one that is popping up a lot which seems to me like bad practice, but I can’t actually back it up with any documentation. I could be completely wrong.
private void datePicker_DateChanged(object sender, EventArgs e)
{
tabControl_SelectedIndexChanged(sender, e);
}
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
tabControl_SelectedIndexChanged(sender, e);
}
My first concern is that the method will have the sender object that is the date picker or the combo box, but does this matter? I asked myself, what is the sender object there for? Perhaps this is why it’s there? I also find EventArgs by itself is pretty useless as far (as I’m aware) unless the class is inherited.
I know that neither the sender or EventArgs are used in the tabControl_SelectedIndexChanged method, so the code works fine. What about possible future implications when some code is changed?
Should I change these to 3 different event handlers that all point to a simple void loadCurrentTab() method? Or perhaps I should get all 3 controls to call the same event handler, such as loadCurrentTab(sender, e)? Or just leave it as it is? Is it that important?
This would actually be my preference, in this scenario. This makes the intent very clear – all three event handlers are routing to one set of logic which (by design) doesn’t pay attention to the sender or
EventArgs.