CppCoreGuidelines icon indicating copy to clipboard operation
CppCoreGuidelines copied to clipboard

C.128: Make rule definition more clear

Open metal4people-zz opened this issue 11 years ago • 13 comments

Hi,

Here is the rule: C.128: Use override to make overriding explicit in large class hierarchies

Consider next code that folows this rule (see I have small class hierarchy, so why I should care about override):

c128_override

The output of a program is expected to be 21, but because of the bug it is 1. The bug is in 19 line instead of int there is unsigned. Bad: such things should be prevented at compile time.

Here is the smallest class hierarchy, however, omitting override leads to bug. Thats why I think this rule should be defined more clear, for example:

C.128 Use override in every virtual function override declaration.

metal4people-zz avatar Nov 22 '15 07:11 metal4people-zz

The rule does not say "do not use override in small hierarchies."

gdr-at-ms avatar Nov 22 '15 13:11 gdr-at-ms

But conversely, why mention large hierarchies when it's just as applicable for small ones?

bdeeming avatar Nov 22 '15 20:11 bdeeming

The formulation of the rule was to draw attention to the contexts where its effect is most noticeable. I don't think it was meant to be exclusive only to large hierarchies. I hope folks don't get too hung up on that one adjective.

gdr-at-ms avatar Nov 22 '15 20:11 gdr-at-ms

If we want to create nice code guidelines such details matter. Closing this issue wont make this guidelines better.

metal4people-zz avatar Nov 23 '15 11:11 metal4people-zz

I would agree that such details matter. Better to be clear than to simply hope everyone understands how much weight you meant them to give each word. Especially since (I assume) a significant number of readers will not be native English speakers.

fisherro avatar Nov 23 '15 12:11 fisherro

There has to be a rule about consistency somewhere? It seems inconsistent to sometimes use override but other times not. I think this should be an always rule.

Besides it always catches bugs, not just in large hierarchies.

Also, being that I think I saw a rule somewhere about not being unnecessarily verbose, I would say that when you use override don't use virtual as that is implied/redundant.

galik avatar Nov 23 '15 14:11 galik

The rule is very clear. Perhaps you want to open a different issue, like @galik mentioned. I also agree that virtual won't be needed then.

Something like:

C.128 recommend override always to increase consistency

But as is, there is nothing wrong with this guideline for specific.

villasv avatar Nov 23 '15 17:11 villasv

Thanks @villasv for clarification. This rule is clear, however, it recommends to use override in large class hierarchies and don't recommend to use in other classes. I think we should have recommendation to use override for every function declaration in order to increase consistency.

metal4people-zz avatar Nov 23 '15 17:11 metal4people-zz

@metal4people The rule didn't say "only in large class". The rule is consistent. And it does not have to say "always" to be consistent. At the end of the day, the guidelines still rely on the common sense of the reader -- it isn't something that can be regulated or ordered.

gdr-at-ms avatar Nov 23 '15 17:11 gdr-at-ms

@gdr-at-ms Thanks, fixed. Please, see updated comment. Regarding "the guidelines still rely on the common sense of the reader -- it isn't something that can be regulated or ordered". Agree we can't regulate or order something with these rules, however, I believe we can help. Moreover, I think if we are writing these rules in order to help programmers (considering beginners). So, these rules should be simple and at the same time useful that will help write good code. So, I think we should make current rule: C.128: Use override to make overriding explicit in large class hierarchies better and write something like this: C.128 Always use override to make overriding explicit in order to increase consistency

metal4people-zz avatar Nov 23 '15 17:11 metal4people-zz

I would go for something like:

C.128 Use override to make overriding explicit in order to catch bugs.

We don't need to restrict it to 'large hierarchies' and surely catching bugs is the critical reason for using override is it not?

class Base
{
    virtual ~Base() {}
    virtual void poly_func() { /* default action */ }
};

class BrokenDerived: public Base
{
    // oops function name spelled wrong so it never gets called
    virtual void poly_finc() { /* special action */ } 
};

class RecommendedDerived: public Base
{
    // compiler catches the bad spelling
    void poly_finc() override { /* special action */ } 
};

galik avatar Nov 23 '15 19:11 galik

I agree with @galik that what he said is a good guideline. But the reasoning is just different from what this C.128 was put there for: avoid unintentionally hiding base class methods (at least that's what I think is the reason) and this is a problem mainly for large hierarchies. Here the "typo" detection is just a bonus.

I believe that the Enforcement won't distinguish between large and short hierarchies when implemented (sounds overkill), so this discussion is merely about rewording the guideline.

@galik's suggestion is a different guideline (because it has different motivation). But because it will naturally cover the C.128, I'd say the easiest thing to do is change the existing. Building up on his example to cover all the reasoning:

C.128 Always explicit override in derived classes

Reason: Readability. Detection of misspelling on overriding methods and unintentional shadowing methods. Explicit overriding in large hierarchies.

Example

struct Base {
    virtual ~Base() {}
    virtual void poly_func(int) { /* default action*/ }
};

struct TypoDerived: public Base { // Catching typos
    // without override, compiles succesfuly and function won't be called from pointer to Base
    void poly_finc(int) override { /* do something */ }
};

struct ShadowDerived: public Base { // Catching shadowing
    // without override, compiles succesfuly and function won't be called from pointer to Base
    void poly_func(double) override { /* do something */ }
};

struct RecommendedDerived: public Base
{
    void poly_func(int) override { /* special action */ }
};

villasv avatar Nov 23 '15 22:11 villasv

@galik @villasv Nice suggestions. Absolutely agree with:

C.128 Always explicit override in derived classes

metal4people-zz avatar Nov 24 '15 08:11 metal4people-zz