tags:

views:

320

answers:

5

I know this is a subjective question, but I'm always curious about best-practices in coding style. ReSharper 4.5 is giving me a warning for the keyword "base" before base method calls in implementation classes, i.e.,

base.DoCommonBaseBehaviorThing();

While I appreciate the "less is better" mentality, I also have spent a lot of time debugging/maintaining highly-chained applications, and feel like it might help to know that a member call is to a base object just by looking at it. It's simple enough to change ReSharper's rules, of course, but what do y'all think? Should "base" be used when calling base members?

Thanks in advance.

+15  A: 

The only time you should use base.MethodCall(); is when you have an overridden method of the same name in the child class, but you actually want to call the method in the parent.

For all other cases, just use MethodCall();.

Keywords like this and base do not make the code more readable and should be avoided for all cases unless they are necessary--such as in the case I described above.

Randolpho
+1. Where's the multiupvote button?
Adam Robinson
"do not make the code more readable and should be avoided" ... well at least the subjective tag is set
Matthew Whited
@Matthew Whited, good point, what's readable or not is highly personal... there actually are people who insist PERL is readable.
Michael Meadows
Downvoted for the comment about 'this.' The 'this' keyword dramatically improves code readability when dealing with instance and static methods.
Jeff Sternal
Isn't the problem with overuse of base more that just readability? In my opinion it is really an issue of maintainability and comprehensibility in the larger sense. For example, if you have base everywhere, you have to make changes in potentially many places if, for example, you add an override because now you really don't want base. And the compiler's not going tell you there's a problem. Are you really going to remember this every time???
Mr. Putty
It's kind of a side point, but I think it's important to stress that `this` should be avoided whenever necessary for *methods. For class fields and properties however, many guidelines indicate that it shoudl be used in all cases.
Noldorin
At risk of commenting long after anyone cares, if you follow the typical convention of putting an underbar in front of private fields, there's little benefit to using "this." as well.
Steven Sudit
+3  A: 

It's really a matter of personal preference. If you like seeing "base." at the beginning of your members, you can easily turn off the rule (Go to Options>Inspection Severity>Code Redundancies>Redundant 'base.' qualifier). Don't let non-behavioral static code analysis rules affect your preferred coding style.

EDIT

One thing to consider is that the static code analysis in FXCop and R# are there to provide rules for all possible needs. To actually adhere to all of the rules simultaneously is a little onerous. You should define your preferred coding style (if you're working in a team, do it collectively), and stick with it. Modify your rules to match your coding standards, not vice versa.

Michael Meadows
I wouldn't say that this is coding style or personal preference. A call to base.SomeMethod() vs a call to SomeMethod() may give the same effect in some cases but the two calls may be fundamentally different.
BlackWasp
In that scenario, you're correct. I was referring to the cases where static code analysis flags the "base." as being redundant. In your example, it would not have been flagged.
Michael Meadows
+4  A: 

I think generally you should use base only when overriding previous functionality.

Some languages (C# does not) also provide this functionality by calling the function by it's base class name explicitly like this: Foo.common() (called from somewhere in Bar, of course).

This would allow you to skip upwards in the chain, or pick from multiple implementations -- in the case of multiple-inheritance.

Regardless, I feel base should be used only when needed to explicitly call your parent's functionality because you are or have overridden that functionality in this class.

Adam Luter
+3  A: 

Another important point to take into consideration is that while you haven't currently overridden that method that doesn't mean you won't ever in the future and by prefacing all of your calls with base. you won't get the new functionality without performing a find and replace for all your calls.

While prefacing calls with this. will not do anything other than decrease / increase readability (ignoring the situation where two variables in scope have the same name) the base. prefix will change the functionality of the code you write in many common scenarios. So I would never add base. unless it is needed.

Martin Harris
+12  A: 

I am not really sure using this is a bad practice or not. base, however is not a matter of good or bad practice, but a matter of semantics. Whereas this is polymorphic, meaning that even if the method using it belongs to a base class, it will use the overriden method, base is not. base will always refer to the method defined at the base class of the method calling it, hence it is not polymorphic. This is a huge semantic difference. base should then be used accordingly. If you want that method, use base. If you want the call to remain polymorphic, don't use base.

Rui Craveiro