API Styles

API is a broader term used for programmatically using a class, library or an application. The finest element of an API is a function which provides the value to its caller. A function in an object oriented language is composed of name, parameters, return value, body and exception. In this chapter we would look at different styles of function and their application. These function styles differ from each other based on their usage parameter, return value and exception.

Classical Function

Multiple parameters, return value and throwing exception. This enforces type safety on the caller in statically types languages and communicates errors via exceptions.

Coarse Grained Function

Single parameter, return value containing the exception, doesn't explicitly throw exception to the caller instead uses custom approach. While it isn't expected to throw exception but unforeseen system errors and bugs can result in exception. This is useful when there are multiple errors and the caller is aware of these errors conditions.

Application Function

It is similar to coarse grained function in the way parameter, exception and return value are used. In addition to that it doesn't use any primitive other than string for parameter or return value. This also ensures that the system exceptions are trapped provided in the return value. Such functions are useful for other systems using the application. The use of string helps in loose binding between the caller and provider. This allows for supporting multiple versions of function.