Example 1

Example 1 demonstrates the principle of least surprise as follows:

#include <iostream>

int sub(int a, int b)
{ return a + b; }

int main(void)
{
std::cout << "The answer is: " << sub(41, 1) << ' ';
return 0;
}

As shown in the preceding example, we have implemented a library API that adds two integers and returns the results. The problem is that we named the function sub, which most developers would associate with subtraction and not addition; although the API functions as designed, it breaks the principle of least surprise because the API's name is not intuitive. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset