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

前端 未结 5 1617
野性不改
野性不改 2021-01-11 13:24

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 bas

相关标签:
5条回答
  • 2021-01-11 13:35

    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.

    0 讨论(0)
  • 2021-01-11 13:37

    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.

    0 讨论(0)
  • 2021-01-11 13:44

    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.

    0 讨论(0)
  • 2021-01-11 13:44

    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.

    0 讨论(0)
  • 2021-01-11 13:53

    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.

    0 讨论(0)
提交回复
热议问题