Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
Function design is the correct design of a function. Exercise #2: correct function declarations is an exercise in correct function design.
The most important to get correct is the function declaration.
Here are some rules:
The name of the function should say what it does
- Do use names like Swap, GetDistance, StrToInt
- Do not use names like DoIt, Transmogrify, Stuff
- Exceptions: Transmogrify is a function name commonly used in the literature to denote a function you are not aware of what it is doing
Make a function responsible for one thing
- Do not use names with 'And' in it, like SwapAndGetMean
- Do use two functions instead, Swap and GetMean
- Exceptions: some mathematical functions can cooperate and so improve runtime speed: MeanAndStdDev executes faster then calling the seperate functions for Mean and StdDev
The return type is expected from the name of the function
The function arguments are expected from the name of the function
The function must specify the called what input it can't handle
- Do use assert and exceptions to make clear to the client which input the function cannot handle. For example, the square root of a negative number does not exist
- Do not use error codes as return types
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.
