Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using "base" bad practice even though it might be good for readability?

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?

like image 679
AJ. Avatar asked Jun 26 '09 15:06

AJ.


4 Answers

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.

like image 99
Randolpho Avatar answered Oct 17 '22 02:10

Randolpho


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.

like image 22
Rui Craveiro Avatar answered Oct 17 '22 01:10

Rui Craveiro


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.

like image 6
Martin Harris Avatar answered Oct 17 '22 02:10

Martin Harris


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.

like image 4
Adam Luter Avatar answered Oct 17 '22 00:10

Adam Luter