• 110 likes • 120 Views
Learn the principles of interface design and how they contribute to code organization, information hiding, and consistency. Discover how to create interfaces that are easy to understand, use, and maintain.
E N D
Presentation November 2nd, 2009 Quoc K. Le CS 265- 001
Interface • Interface is the detailed boundary between code that provides a service and code that uses it. • It defines: What some body of code does for its users, how functions can be used
Generally, good interfaces must follow a set of principles: • Hide implementation details (Info hiding) • Choose a small orthogonal set of primitives • Don’t do secret stuffs. • Be consistent
Hide implementation details: Other terms for this kind of organizing principle: Encapsulation, Information hiding, abstraction, modulization. Example: Classes such as string, vector, deque… It’s easy to implement them without knowing how they do
It’s recommendable not to make data publicly visible. • Users might change variables.
2. Choose a small orthogonal set of primitives. • An interface should provide as much functionality as necessary but no more. • A large interface is harder to build and maintain. • It also makes users harder to learn. • Narrow interface are preferred.
Example: C standard I/O library provides several ways to write a single character to an output stream: • char c; • putc(c, fp); • fputc(c, fp); Not necessary!
3. Don’t be secret • A library function should not write secret files and variables or change global data • Example: function strtok in the class string It functions to split string into tokens. However the function writes NULL bytes into the middle of its input string
4. Consistency: In other words, do the same thing the same way everywhere. Consistency makes functions easy to understand and use without much studying. Example: The loops are presented quite the same ways in several languages such as C++, Java, Perl…