Characteristics of Good Interfaces and Core Design Principles
The article outlines the essential traits of high‑quality interfaces, describes a step‑by‑step process for designing and implementing them, and presents fundamental principles for interface, class, and method design to ensure usability, maintainability, and robustness in software development.
1. Characteristics of a Good Interface
(1) Easy to learn.
(2) Easy to use, even without documentation.
(3) Hard to misuse.
(4) Easy to read and maintain.
(5) Easy to extend.
2. Process of Interface Design and Implementation
(1) Analyze requirements and consider better solutions.
(2) Design small interfaces for rapid, agile iteration; small interfaces are easy to modify and give confidence.
(3) Write high‑frequency, early interfaces and create self‑testing cases.
(4) Write Service Provider Interfaces (SPI) carefully, questioning support for multiple interfaces, extensibility, and correctness.
(5) Pay attention to normal exception handling and ensure interfaces meet all needs, even when exceptions occur.
3. Basic Principles of Interface Design
(1) Do one thing and do it well; function names should be self‑explanatory, as poor names often indicate poor design.
(2) If the first principle is not met, decompose the function.
(3) Never delete functions or interfaces; always add.
(4) Implementation must never affect the interface; do not expose internal details.
(5) Minimize access, prefer private members, and hide information.
(6) Provide documentation and comments; interfaces are not only for oneself.
(7) Improve performance where possible.
(8) Keep the interface platform‑independent.
4. Class Design
(1) Minimize mutability; e.g., avoid designing Date as a mutable class, prefer immutable alternatives like TimerTask.
(2) Use inheritance sparingly; explicitly forbid inheritance when a class should not be extended.
5. Method Design
(1) Avoid copying template code; let modules encapsulate functionality.
(2) Ensure atomicity within a single method.
(3) Apply the Fail‑Fast principle: return errors early and let higher layers handle them.
(4) Provide accessor methods instead of direct data access.
(5) Overload cautiously; be aware of pitfalls in multiple inheritance and consider whether overloading is truly needed.
(6) Pay attention to parameter and return types; be explicit, avoid unnecessary strings, prefer double over float for 64‑bit precision.
(7) Mark unchanged parameters as const.
(8) Keep the number of parameters reasonable; excessive parameters may indicate a flawed interface.
(9) Do not return values and exceptions simultaneously.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.