Templates and generic programming
T.143
Don't write unintentionally non-generic code
Reason
Generality. Reusability. Don't gratuitously commit to details; use the most general facilities available.
Example
Use != instead of < to compare iterators; != works for more objects because it doesn't rely on ordering.
for (auto i = first; i < last; ++i) { // less generic
// ...
}
for (auto i = first; i != last; ++i) { // good; more generic
// ...
}
Of course, range-for is better still where it does what you want.
Example
Use the least-derived class that has the functionality you need.
class Base {
public:
Bar f();
Bar g();
};
class Derived1 : public Base {
public:
Bar h();
};
class Derived2 : public Base {
public:
Bar j();
};
// bad, unless there is a specific reason for limiting to Derived1 objects only
void my_func(Derived1& param)
{
use(param.f());
use(param.g());
}
// good, uses only Base interface so only commit to that
void my_func(Base& param)
{
use(param.f());
use(param.g());
}
Enforcement
- Flag comparison of iterators using
<instead of!=. - Flag
x.size() == 0whenx.empty()orx.is_empty()is available. Emptiness works for more containers than size(), because some containers don't know their size or are conceptually of unbounded size. - Flag functions that take a pointer or reference to a more-derived type but only use functions declared in a base type.