Generality of a Function

◀ Reliability of a Function▶ Understand What the Function Should Do
Amazon How general is your function? Is the function written to deal with one specific situation or many? A program with too many functions can become difficult to understand and cumbersome.

For example, if a program needs to deal with five different requests issued by a user, it can call five distinct functions each of which taking care of one request. This example has been discussed in Chapter 4.5. However, it can also call one function, given the identity of the request. The latter way is generally more desired because it is possible that there are overlapping operations among some of the requests in which case you can reuse the same block of code.


Sometimes when the subtasks for your program are too different, you may not be able to generalize them. In that case just write distinct functions.
Also, a function that performs a general task (e.g. converting a double to a C++ string) may be highly reusable. A function that performs too specialized a task is low on reusability.

In addition, a function template is a good option to provide generality: One function works with multiple argument types.

Next let’s look at what we need to do to write a good function!

◀ Reliability of a Function▶ Understand What the Function Should Do

fShare
Questions? Let me know!