January 2020
Intermediate to advanced
454 pages
11h 25m
English
Example 6 demonstrates the principle of least surprise as follows:
#include <iostream>int add(int a, int b){ return a + b; }int Sub(int a, int b){ return a - b; }int main(void){ std::cout << "The answer is: " << add(41, 1) << '\n'; std::cout << "The answer is: " << Sub(43, 1) << '\n'; return 0;}
As shown in the preceding code, we have implemented two different APIs. The first adds two integers and returns the results while the second subtracts two integers and returns the results. The issue with the subtract function is two-fold: