In a library, there could exist three types of functions. The first are those which are visible to the user i.e. their declarations are installed in the library's include directory. The third are those which do the core work of the function by e.g. getting into the numerics in a highly detailed fashion. The second are those which provide complex functionality by calling several functions of the third kind, and managing their outputs. I like to call these interface, bridge and implementation functions, respectively. If these are commonly referred to by other names, please tell me them.
Now an interface function could look like this (pseudo-code in C/C++):
void compute_something(const double* data, const std::size_t& size)
{
bridge_function(data, size);
}
The above bridge function could look like this:
void bridge_function(const double* data, const std::size_t& size)
{
const auto sum = impl::function1(data, size);
impl::function2(data, size);
impl::function3(data, size);
// ...
impl::functionN(data, size, sum);
}
Finally, the first of those above arbitrary implementation functions could look like this:
namespace impl
{
double function1(const double* data, const std::size_t& size)
{
ret_val = double{};
for (auto index = std::size_t{}; index < size; ++index)
{
ret_val += data[index];
}
return ret_val;
}
} // namespace impl
I've read often that every function should check its own preconditions. However, the above functions could share many — if not all — preconditions. In this simple example, it could be that the following are the only preconditions:
if (data == nullptr)
{
// throw exception
}
if (size < 1)
{
// throw exception
}
Now, here's the question: at which places should the perfect software developer check the preconditions? Possible answers: (a) only at the interface level (b) only at the implementation level (c) on all levels (d) at the interface level and within a debug section at the implementation level.
I look forward to hearing well supported/reasoned answers (also gladly outside of the above options) and appreciate your participation in this discussion.