4.5 Interface Design
• Good design of function interfaces is a somewhat nebulous topic, but there are some fundamental principles that are generally applicable.
• Functions should be self-contained and accessible only via well-defined interfaces. It is usually bad practice to expose function internals. That is, an interface should expose an algorithm’s purpose, not an algorithm’s implementation. Functions are an abstraction mechanism that allow code to be understood at a higher level.
• Function dependences should be avoided or minimised. That is, it is desirable to minimise the e ect that changing one function will have upon another. Ideally, a function can be altered, enhanced, debugged, etc, independently, with no e ect on the operation of other functions.
• A function should perform a single specific task. Avoid writing functions that perform several tasks; it is better to split such a function into several functions, and later combine them in a “wrapper” function, if required. Wrapper functions are useful for ensuring that a set of related functions are called in a specific sequence.
• Function interfaces should be minimal. It should have only the arguments necessary for its specific task, and should avoid extraneous “bells and whistles” features.
• A good interface should be intuitive to use.
4.6 The Standard Library
There are a large number of functions in the standard library (about 145), which provide many commonly used routines and operations. These functions are present on all standard-compliant systems; They are portable and correct, so use them before writing your own implementation. 7, Standard Library Function is a good example of the quality interface design. Note the use of short, descriptive function names, and intuitive, minimal interfaces.
It pays to become familiar with the standard library. Learn what functions are available and their various purposes. The following is a selection of particularly useful functions listed by category.
• Mathematical functions. sqrt, pow, sin, cos, tan.
• Manipulating characters. isdigit, isalpha, isspace, toupper, tolower.
• Manipulating strings. strlen, strcpy, strcmp, strcat, strstr, strtok.
• Formatted input and output. printf, scanf, sprintf, sscanf.
• File input and output. fopen, fclose, fgets, getchar, fseek.
• Error handling. assert, exit.
• Time and date functions. clock, time, difftime.
• Sort and search. qsort, bsearch.
• Low-level memory operations. memcpy, memset.
For example, the standard library of Tuper () will be correct for the character set of the implementation machine, on which the compiler remains. This section is not contrary to version 4.3, which we have introduced in section 4.3, which is wrong for machines, say, EBCDIC character sets.
No comments:
Post a Comment