According to Why define a Java object using interface (e.g. Map) rather than implementation (HashMap), I know I should declare most abstract type when possible, for example, suppose I'm using an UI engine which has BlinkLabel that has a speific method setBlinkTime(), and extends from Label, they used to display some text on the screen:
Original version:
BlinkLabel* label1=BlinkLabel::create("text1",32);
label1->setColor(Color.RED);
label1->setBlinkTime(0.5);
this->addChild(label1);
BlinkLabel* label2=BlinkLabel::create("text2",32); //use default blink rate, so no need to call setBlinkTime()
label2->setColor(Color.BLUE);
this->addChild(label2);
"Declare most abstract type" version:
BlinkLabel* label1=BlinkLabel::create("text1",32);
label1->setColor(Color.RED);
label1->setBlinkTime(0.5);
this->addChild(label1);
Label* label2=BlinkLabel::create("text2",32); //use default blink rate, so no need to call setBlinkTime()
label2->setColor(Color.BLUE);
this->addChild(label2);
which label2 doesn't need to call setBlinkTime(), so it should declare as Label instead of BlinkLabel, fulfilling "Declare most abstract type" guideline. However, according to Does auto make C++ code harder to understand?, I should use auto when possible:
auto* label1=BlinkLabel::create("text1",32);
label1->setColor(Color.RED);
label1->setBlinkTime(0.5);
this->addChild(label1);
auto* label2=BlinkLabel::create("text2",24); //use default blink rate, so no need to call setBlinkTime()
label2->setColor(Color.BLUE);
this->addChild(label2);
The problem is, when using "auto", and then I call the setBlinkTime() at label2 unnecessarily (eg: copy code from label1 but forgot to delete it):
label2->setBlinkTime(0.5);
The "auto" version would compile, which is the thing that "declare most abstract type" wants to avoid : avoid call subclass methods unnecessarily by avoid compiling successfully.
So my question is, "use auto" and "declare most abstract type", which guideline has higher priority?