Do I need to call base.METHOD after overriding it, and if so – when is it the right time ?
Well, I’m glad you’ve asked; I had some chat with my co-worker about the subject when he did some overriding to the Form (WinForm) methods and he wasn’t sure about when is it smart to call the base method and if so, should he call base.METHOD before implementing his code, or maybe after. To clarify, quick example:
public class MyForm : Form
{
protected override void OnLoad(EventArgs e)
{
//OK, what here ???!!
// Custom code here ?
//base.OnLoad(e); // should I ?
// Or maybe – Custom code here… ?
}
}
After thinking for a few seconds, I came up with this explanation – sort of my “best practice” to the issue:
When do I call base.METHOD:
- If you don’t know the implementation in the method you’ve just override: assume that it does some “magic” stuff which is important to the correct flow of the application; But This doesn’t mean that you must call the base method.
- If you want to stop the logic flow, it makes sense to override the appropriate method and write your code which will change the flow according to your need. In this scenario there is no need to call the base method.
- If you don’t want to change the logic flow, or you’re not sure – call the base method.
- If you want to stop the logic flow, it makes sense to override the appropriate method and write your code which will change the flow according to your need. In this scenario there is no need to call the base method.
- If you know the implementation in the method you’ve just override: no “magic” stuff so this is an easy call – if you need the code in the base method, call it – otherwise – leave it out.
Do I call base.METHOD after my custom code, or maybe before:
This is a tricky one and there is not straight answer – my way of thinking and tackling this question is simply by Explore, Run & Learn “process” – meaning, try to Explore about the base method original purpose and try to figure out the logic; This will give you some logical direction whether you need to call the base method or not. For example – if you override the Render method, you can tell (by MSDN or by Reflector) that its rendering the entire object graph so you probably should call it (eventually). Now that you have some feeling about whether you need to write your code before or after the base.METHOD call, Run it and exam the results:
- Does the page behaves as you expected?
- Can you mess it up so it won’t work in some scenarios – how can you deal with it ?
- Grab the nearest programmer available, can he think about something which will mess it up ? if so – how can you deal with it ?
According to the results and the answers about the given questions, you should now have a clear vision about your decision. Make the required adjustments, place some important remarks(to describe your thinking at the moment) and feel good about your code, you did your best to get a good result. And most important – Learn from the process so it will be more natural on the next time you’ll have to face this decision.
I would argue that if you don’t know what the method does, you’ve no business overriding it.
In any case in Win/Web forms, the On*** methods just call the event handler for the method.
I would add to Rahien that most of the WinForms "magic" methods hold a clear documentation and examples.
and this will teach you the H-D (Human-Debugger) Lesson #69, add your comments clearly as possible, pay attention and read them again. (in the code review – the reviewer should read them too) and don’t forget to give us some example (if needed).
Ayende, Shani – I specifically wrote "But This doesn’t mean that you must call the base method.".
Sure, there are times that it’s required, but the programmer (and the one that reviews the code later) should look very carefully on the base method before making a clear decision about it. About the event handler – thanks for mentioning it (I forgot to do so).
If you´re overriding you should know where to place the call to the base method.
If you have access to the source code of the base, then you just know.
If not – there should be clear documentation as to where (and whether) to call the base method.
So this is a good practice – if you´re writing a virtual method, make sure to document very well how you want people to override it.
And if you dont have access to the source, and its not documented – I would not override it at all, that is just a straight path to hell.
Love and kisses.
Me, excited –
Yo Bitz, good to hear from you man !
This post is all the way from South America ah?? Nice that you have time to read some technical bullshit instead of playing some soccer with Rikelme or Tebez (send them my best regards btw).
Me, answering your comments –
Great remark which I love to enforce(code reviews) – every "virtual" method\property written MUST be well documented about the correct usage of it. This is absolutely vital for a sane development of an application(& maintenance) and even more important – sane developers.