1e41f4b71Sopenharmony_ci# OpenHarmony API Design Specifications 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci**Change History** 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci| Version | Prepared By | Release Date | Description | 6e41f4b71Sopenharmony_ci| -------------- | ------------------- | ---------- | ---------- | 7e41f4b71Sopenharmony_ci| v0.1| OpenHarmony API SIG | November 2022| Trial version.| 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Purpose 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciAn API is a definition provided by a software implementer for developers on the programming UI to reflect the capability scope of a software entity. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciThe quality of APIs greatly affects developer experience. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciTo deliver a sound developer experience and promote the healthy development of the OpenHarmony ecosystem, the API SIG formulates the OpenHarmony API design specifications. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## Scope 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciOpenHarmony APIs are classified into public APIs (open to third-party applications) and system APIs (available only for system applications). 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciThe design specifications are effective for both types of APIs, regardless of the programming language in use. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciFor details about the API classification, see [OpenHarmony API Governance Charter](OpenHarmony-API-governance.md). 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci## API Design Objectives 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ciA good API design must meet the following requirements: 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci* Operational: An API must be able to implement the capability it claims to offer. This is the fundamental requirement. 30e41f4b71Sopenharmony_ci* Expressive: By calling an API, the caller can clearly express what they want to do. 31e41f4b71Sopenharmony_ci* Simple: An API must be easy to learn and use, and not prone to errors. 32e41f4b71Sopenharmony_ci* Predictable: An API must always complete its mission according to the definition. If the definition does not contain errors or exceptions, no error or exception should occur regardless of how many times the API is called. If an error or a failure may occur, describe the error or failure in the definition and return the corresponding error at the correct time. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ciYou must understand the following when designing APIs: 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci* The stability and consistency of APIs take precedence over the API quantity. 37e41f4b71Sopenharmony_ci* A better API name is the one that is easy to understand, not a shorter one. 38e41f4b71Sopenharmony_ci* APIs should be encapsulated to prevent too much information from being exposed. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ciFrom the perspective of developers, a well-designed API has different features in different phrases. 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci* In the learning phase 43e41f4b71Sopenharmony_ci * Easy to understand 44e41f4b71Sopenharmony_ci * Easy to use 45e41f4b71Sopenharmony_ci* In the development phase 46e41f4b71Sopenharmony_ci * Expressive 47e41f4b71Sopenharmony_ci * Simple 48e41f4b71Sopenharmony_ci * Predictable 49e41f4b71Sopenharmony_ci* In the maintenance phase 50e41f4b71Sopenharmony_ci * Stable 51e41f4b71Sopenharmony_ci * Easy to maintain 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci## API Design Overview 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ciTo make the rules as universal as possible, the specifications do not involve programming language differences or coding specifications. You need to comply with the corresponding requirements for them. 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ciThis document provides pre-release review specifications and post-release evaluation specifications. 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci* The pre-release review specifications are the basic requirements for APIs. Any API must meet these requirements before passing the release review. 60e41f4b71Sopenharmony_ci* The post-release evaluation specifications require the API providers to keep an eye on the APIs after they are published. Even though an API has met all the pre-release review specifications, it may not be perfect. On-time review after the release helps further improve the API quality. 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ciThe following lists all the rules that you should comply with during API design. The list is provided for quick indexing, and the detailed description is provided in the following sections. 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ciEvery OpenHarmony API provider should be familiar with these rules. 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci### Pre-release Review Specifications 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci* Usability 69e41f4b71Sopenharmony_ci * Rule 1: Coding rules compliant 70e41f4b71Sopenharmony_ci * Rule 2: Grammatically correct 71e41f4b71Sopenharmony_ci * Rule 3: Proper use of abbreviations 72e41f4b71Sopenharmony_ci * Rule 4: Accurate use of words in pairs 73e41f4b71Sopenharmony_ci * Rule 5: Accurate use of design or architecture patterns 74e41f4b71Sopenharmony_ci * Rule 6: No controversial names 75e41f4b71Sopenharmony_ci * Rule 7: Appropriate parameter types 76e41f4b71Sopenharmony_ci * Rule 8: Appropriate parameter quantity 77e41f4b71Sopenharmony_ci * Rule 9: Appropriate parameter sequence 78e41f4b71Sopenharmony_ci * Rule 10: Complete return value definition 79e41f4b71Sopenharmony_ci * Rule 11: Appropriate exception definition 80e41f4b71Sopenharmony_ci * Rule 12: Positive expressions 81e41f4b71Sopenharmony_ci * Rule 13: Reducing the possibility of errors 82e41f4b71Sopenharmony_ci * Rule 14: No sequential coupling 83e41f4b71Sopenharmony_ci * Rule 15: Accurate logic expression 84e41f4b71Sopenharmony_ci * Rule 16: Encapsulation welcomed 85e41f4b71Sopenharmony_ci * Rule 17: Single responsibility 86e41f4b71Sopenharmony_ci* Availability 87e41f4b71Sopenharmony_ci * Rule 18: Reliability 88e41f4b71Sopenharmony_ci * Rule 19: Complete functionalities 89e41f4b71Sopenharmony_ci * Rule 20: Permission and privacy protection 90e41f4b71Sopenharmony_ci * Rule 21: Concurrent environment 91e41f4b71Sopenharmony_ci * Rule 22: Resource management closure 92e41f4b71Sopenharmony_ci * Rule 23: Retry logic 93e41f4b71Sopenharmony_ci * Rule 24: Idempotence requirements 94e41f4b71Sopenharmony_ci * Rule 25: Device universality 95e41f4b71Sopenharmony_ci* Consistency 96e41f4b71Sopenharmony_ci * Rule 26: Consistent terms and concepts 97e41f4b71Sopenharmony_ci * Rule 27: Consistent inter-device behavior 98e41f4b71Sopenharmony_ci * Rule 28: Consistent version evolution 99e41f4b71Sopenharmony_ci * Rule 29: Consistent naming style 100e41f4b71Sopenharmony_ci * Rule 30: Consistent parameter sequence 101e41f4b71Sopenharmony_ci * Rule 31: Consistent synchronous/asynchronous style 102e41f4b71Sopenharmony_ci* Compatibility 103e41f4b71Sopenharmony_ci * Rule 32: Version compatibility 104e41f4b71Sopenharmony_ci * Rule 33: Backward binary-code compatibility 105e41f4b71Sopenharmony_ci* API references 106e41f4b71Sopenharmony_ci * Rule 34: Basic descriptions for modules, namespaces, classes, and functions 107e41f4b71Sopenharmony_ci * Rule 35: Clear description of use cases 108e41f4b71Sopenharmony_ci * Rule 36: Accurate API description 109e41f4b71Sopenharmony_ci * Rule 37: Accurate parameter description 110e41f4b71Sopenharmony_ci * Rule 38: Accurate return value/exception description 111e41f4b71Sopenharmony_ci * Rule 39: Complete meta information 112e41f4b71Sopenharmony_ci * Rule 40: Consistent style 113e41f4b71Sopenharmony_ci* Organization mode 114e41f4b71Sopenharmony_ci * Rule 41: Reasonable layering 115e41f4b71Sopenharmony_ci * Rule 42: Proper module division 116e41f4b71Sopenharmony_ci * Rule 43: API reference document available (as part of the API) 117e41f4b71Sopenharmony_ci* Quality related 118e41f4b71Sopenharmony_ci * Rule 44: Performance requirements met 119e41f4b71Sopenharmony_ci * Rule 45: Proper power consumption 120e41f4b71Sopenharmony_ci * Rule 46: Testable 121e41f4b71Sopenharmony_ci * Rule 47: Environmentally adaptable 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci### Post-release Evaluation Specifications 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci* Stability 126e41f4b71Sopenharmony_ci * Rule 48: Minimized rate for deprecated or changed APIs 127e41f4b71Sopenharmony_ci* Security 128e41f4b71Sopenharmony_ci * Rule 49: No abuse of APIs 129e41f4b71Sopenharmony_ci * Rule 50: No exploitation of APIs 130e41f4b71Sopenharmony_ci* Maintainability 131e41f4b71Sopenharmony_ci * Rule 51: Evolution support 132e41f4b71Sopenharmony_ci * Rule 52: Consistent behavior after feature expansion 133e41f4b71Sopenharmony_ci * Rule 53: Documents updated accordingly 134e41f4b71Sopenharmony_ci* Irreplaceability 135e41f4b71Sopenharmony_ci * Rule 54: Orthogonal design 136e41f4b71Sopenharmony_ci* Feedback from developers 137e41f4b71Sopenharmony_ci * Rule 55: Developer feedback noticed 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci## Description of Pre-release Review Specifications 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci### Usability 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ciUsability should be considered in any design. During the design of OpenHarmony APIs, consider usability from the following aspects: 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci#### Naming 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci* **Rule 1: Coding rules compliant** 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ciThe definition of an API must comply with the coding rules of the project to which the API belongs, for example, the rules for uppercase and lowercase letters, underscores (_), hyphens (-), and prefixes. 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ciYou can refer to the following coding rules for the OpenHarmony project: 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci* [JavaScript Coding Style Guide](../contribute/OpenHarmony-JavaScript-coding-style-guide.md) 154e41f4b71Sopenharmony_ci* [C Coding Style Guide](../contribute/OpenHarmony-c-coding-style-guide.md) 155e41f4b71Sopenharmony_ci* [C++ Coding Style Guide](../contribute/OpenHarmony-cpp-coding-style-guide.md) 156e41f4b71Sopenharmony_ci* [C&C++ Secure Coding Guide](../contribute/OpenHarmony-c-cpp-secure-coding-guide.md) 157e41f4b71Sopenharmony_ci* [HDF Driver Coding Guide](../contribute/OpenHarmony-hdf-coding-guide.md) 158e41f4b71Sopenharmony_ci* **Rule 2: Grammatically correct** 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ciAPI names must be in English. Generally, a class is named using a noun, for example, **exampleManager**, **exampleService**, and **exampleAnimation**. A function is named using a verb or verb-object structure, for example, **start()**, **createUser()**, and **startBoot()**. 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ciIt is incorrect to name a class **Start** or a function **ball()**. 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ciIn terms of verbs, use transitive verbs, intransitive verbs, and verb tenses correctly. For example, you can name events **transferStarted()** and **renderDone()**. 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci* **Rule 3: Proper use of abbreviations** 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ciAvoid abbreviations as much as possible, as abbreviations are difficult to understand or even cause misunderstanding. 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ciUse only abbreviations that are well known. Do not make your own abbreviations. 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci* **Rule 4: Accurate use of words in pairs** 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ciMany words can be used to express the same meaning. The table below lists some examples. 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci| Word | Synonym | 177e41f4b71Sopenharmony_ci| ----- | -------------------------------------------------- | 178e41f4b71Sopenharmony_ci| send | deliver, dispatch, announce, distribute, route | 179e41f4b71Sopenharmony_ci| find | search, extract, locate, recover | 180e41f4b71Sopenharmony_ci| start | launch, create, begin, open | 181e41f4b71Sopenharmony_ci| make | create, set up, build, generate, compose, add, new | 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ciWhen naming APIs, try to use words in pairs, rather than any synonym. 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ciFor example, if **add** is used for an API name, **remove** is a better option than **destroy** for its counterpart; if **increase** is used, **decrease** is a better option that **reduce**. 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ciThe table below lists common words in pairs that can be used in API names. 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci| Word | Counterpart | 190e41f4b71Sopenharmony_ci| -------- | -------- | 191e41f4b71Sopenharmony_ci| add | remove | 192e41f4b71Sopenharmony_ci| increase | decrease | 193e41f4b71Sopenharmony_ci| open | close | 194e41f4b71Sopenharmony_ci| begin | end | 195e41f4b71Sopenharmony_ci| insert | delete | 196e41f4b71Sopenharmony_ci| show | hide | 197e41f4b71Sopenharmony_ci| create | destroy | 198e41f4b71Sopenharmony_ci| lock | unlock | 199e41f4b71Sopenharmony_ci| source | target | 200e41f4b71Sopenharmony_ci| first | last | 201e41f4b71Sopenharmony_ci| min | max | 202e41f4b71Sopenharmony_ci| start | stop | 203e41f4b71Sopenharmony_ci| get | set | 204e41f4b71Sopenharmony_ci| next | previous | 205e41f4b71Sopenharmony_ci| up | down | 206e41f4b71Sopenharmony_ci| new | old | 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ci* **Rule 5: Accurate use of design or architecture patterns** 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ciThe design or architecture pattern is actually the jargon of the software industry. Use them correctly to avoid misunderstanding. 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ciIf your API contains words such as **Strategy**, **Builder**, **Factory**, and **Singleton**, make sure you understand these design patterns accurately and use them correctly. 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ciWhen your API uses a specific pattern, use the terms defined in that pattern. Do not change the world class or sequence. 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci* **Rule 6: No controversial names** 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ciDo not use controversial names, including but not limited to any words that violate the laws and regulations, cause religious disputes, or lead to racial discrimination, as well as dirty words. 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ciConsidering that OpenHarmony is designed to empower a variety of industries, you need to pay more attention when deciding whether a name is controversial. 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci#### Parameters 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci* **Rule 7: Appropriate parameter types** 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ciTypically, it is better to use class type parameters than simple type parameters. 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ciFor example, the following APIs seem to be fine at first glance: 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci* `addPerson(string id, string name, int age)` 231e41f4b71Sopenharmony_ci* `removePerson(string id, string name, int age)` 232e41f4b71Sopenharmony_ci* `modifyPerson(string id, string name, int age)` 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ciHowever, they are not friendly for expansion. If you define the parameters using the class type **Person**, it would be easy to add new parameters. What's more, you do not need to make any changes to the APIs. 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ciClass type parameters reduce the parameter quantity in an API, thereby making the API easy to be remembered. 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci* **Rule 8: Appropriate parameter quantity** 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ciUse 7 or less parameters in an API. In most cases, use 3 to 5 parameters in an API, for easy of use. 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ciThe number of parameters in an API should not exceed 10 in any case. If an API contains more than 10 parameters, the possible cause is that the type is not well encapsulated or the API implementation logic is too complex. If this is the case, consider to use another form. 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci* **Rule 9: Appropriate parameter sequence** 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ciIn some programming languages, parameters in an API are often organized in the order of input parameters followed by output parameters. 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ciIf you further sort the parameters based on their logical relationship and importance, it is easier for developers to memorize and use the parameters. 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ciYou should place optional parameters after mandatory parameters, and place callback functions at the end. For example, in the **fs.readFile** API, the **path** parameter is mandatory, and **encoding** and **flag** have default values. 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci```js 253e41f4b71Sopenharmony_cifs.readFile(path[, options], callback) 254e41f4b71Sopenharmony_ci``` 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci```js 257e41f4b71Sopenharmony_cifs.readFile('/etc/passwd', (err, data) => { 258e41f4b71Sopenharmony_ci if (err) throw err; 259e41f4b71Sopenharmony_ci console.log(data); 260e41f4b71Sopenharmony_ci}); 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_cifs.readFile('/etc/passwd', { 263e41f4b71Sopenharmony_ci encoding: 'utf-8', 264e41f4b71Sopenharmony_ci flag: 'r+' 265e41f4b71Sopenharmony_ci}, (err, data) => { 266e41f4b71Sopenharmony_ci if (err) throw err; 267e41f4b71Sopenharmony_ci console.log(data); 268e41f4b71Sopenharmony_ci}); 269e41f4b71Sopenharmony_ci``` 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci#### Return Values 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci* **Rule 10: Complete return value definition** 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ciThe definition of return values must be complete. This means that you need to consider exceptions in addition to normal situations so developers will be able to handle the exceptions. 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ciExample: 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci* For a return value of the number type, define the value range and the situation in which an extreme value occurs. 280e41f4b71Sopenharmony_ci* For a return value of the Boolean type, define when **true** and **false** will be returned. 281e41f4b71Sopenharmony_ci* For a return value of the array or set type, define when **null** or an empty set will be returned. 282e41f4b71Sopenharmony_ci* For a return value of the enum type, define when each enumerated value will be returned. 283e41f4b71Sopenharmony_ci* **Rule 11: Appropriate exception definition** 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ciAn exception is returned when, for example, an input parameter is invalid or the API implementation logic is incorrect. 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ciFor the same module or service, there should be a unified definition for the situations in which an error value will be returned and an exception will be thrown. 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ciIn addition, the same exception type should be used for the same exception. 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ciConsistency greatly reduces the difficulty in using the API and the probability of errors. 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci#### Others 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ciYou are advised to adhere to the following rules to make your APIs easy to use. 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci* **Rule 12: Positive expressions** 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ciPositive expressions enable developers to think less. 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ciWhen naming APIs, use positive words instead of negative ones. 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ciThe following is a counter example: 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci```js 306e41f4b71Sopenharmony_ciif (!isNotAccessible() || !isNotWritable() || !isNotPrintable()) 307e41f4b71Sopenharmony_ci``` 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ciThe following is recommended: 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci```js 312e41f4b71Sopenharmony_ciif (isAccessible() && isWritable() && isPrintable()) 313e41f4b71Sopenharmony_ci``` 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci* **Rule 13: Reducing the possibility of errors** 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ciMost API use errors are caused by parameter passing errors. 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_ciFor example, for the following function, both the second and third parameters are Boolean values. When calling the function, developers may find it difficult to remember the parameter values correctly. 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci```js 322e41f4b71Sopenharmony_cideclare function findString(text: string, isForward: boolean, isCaseSensitive: boolean): string; 323e41f4b71Sopenharmony_ci``` 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ciYou can define enums to reduce the difficulty. 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci```js 328e41f4b71Sopenharmony_cienum SearchDirection { 329e41f4b71Sopenharmony_ci FORWARD, 330e41f4b71Sopenharmony_ci BACKWARD 331e41f4b71Sopenharmony_ci}; 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_cienum CaseSensitivity { 334e41f4b71Sopenharmony_ci SENSITIVE, 335e41f4b71Sopenharmony_ci INSENSITIVE 336e41f4b71Sopenharmony_ci}; 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_cideclare function findString(text: string, direction: SearchDirection, sensitivity: CaseSensitivity): string; 339e41f4b71Sopenharmony_ci``` 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ciThe use of enums instead of Boolean values and integers helps reduce the possibility of API use errors. 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ciCombining multiple parameters into a type also reduces the possibility of parameter passing errors. 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci* **Rule 14: No sequential coupling** 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ciEnsure high cohesion and low coupling during software design, especially for large projects. 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ciCoupling indicates the dependency between software modules. A too deep coupling often means a poor architecture design. 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ciAccording to the static software structure or hierarchical architecture diagram, if there are few dependencies between modules but obvious API calling directions between upper and lower layers, it is a good design. On the contrary, if there are complex inter-module dependencies or call chains with opposite directions between the upper and lower layers, the structure is poor. 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ciAmong all the types of coupling, sequential coupling must be noted for APIs. Sequential coupling means that the APIs in a class must be called in a specific sequence in order to work properly. 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ciThe following is an example of sequential coupling: 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci```js 358e41f4b71Sopenharmony_cidoSomethingFirst() 359e41f4b71Sopenharmony_cidoSomethingSecond() 360e41f4b71Sopenharmony_cidoSomethingThird() 361e41f4b71Sopenharmony_ci``` 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ciSequential coupling poses high requirements on the callers and is prone to incorrect use. 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ciTo avoid sequential coupling, you can use the **template method** design pattern. 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci* **Rule 15: Accurate logic expression** 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ciSome APIs, for example, **getUserAccount()**, are used to query information, whereas others, for example, **createUserAccount()**, are used to modify data. 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ciNever use an API that seems to be used for query to carry out data modification. It will confuse developers. 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_ciThe name of an API should express everything it does, with no concealing. Only in this way can it be easier for developers to understand what the API does when reading the code. 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ciIf an API implements too many features and cannot be named in a few words, it usually means that you should split the API into multiple APIs, to make it cohesive enough. 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ciDo not assume that developers have the same background information as you do. 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ciFor example, you may name a field **language** and believe all the readers know that you are talking about the programming language. In fact, someone might wonder whether you are talking about the language of speech. Name it **programmingLanguage** will eliminate the misunderstanding. 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ciHowever, do not go to another extreme. If the class name or namespace name contains a prefix, do not repeat it in the function. 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci* **Rule 16: Encapsulation welcomed** 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ciEncapsulation is a key concept in object-oriented programming. 386e41f4b71Sopenharmony_ci 387e41f4b71Sopenharmony_ciEncapsulation means that system capabilities should be encapsulated as simple APIs, with implementation details hidden as much as possible. 388e41f4b71Sopenharmony_ci 389e41f4b71Sopenharmony_ciIt is like an iceberg. No matter how large the portion submerged in water is, the visible portion above water should be small enough and concise enough. This is a user-friendly interface. 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ciEncapsulation makes it easy for developers to use, while making fewer errors. For example, an electronic engineer connects all the electric wires in the room and leaves only one button to users. This is a good encapsulation. It prevents users from the need to understand the complex circuit structure and protect them from electric shocks. 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci* **Rule 17: Single responsibility** 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ciAn API should try to do only one thing (a single core responsibility) as much as possible. Example: 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ci```js 398e41f4b71Sopenharmony_ci// Not recommended 399e41f4b71Sopenharmony_ciview.fetchDataAndRender(url, templete); 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci// Recommended 402e41f4b71Sopenharmony_cilet data = view.dataManager.fetchData(url); 403e41f4b71Sopenharmony_ciview.render(data, templete); 404e41f4b71Sopenharmony_ci``` 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ciSingle responsibility enables developers to use APIs in the minimum unit as required. It also enables developers to encapsulate the service logic based on their service requirements. 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ciIn some cases, the rule "single responsibility" may conflict with "encapsulation welcomed". For example, if there are many APIs with a single capability, developers have to notice the calling details for each API. 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ciIf this is the case, determine the level of the capability provided by your API based on the encapsulation level. To provide the capability for a high-level caller, you can encapsulate the APIs. To provide the capability for a low-level caller, you can provide separate APIs. 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci### Availability 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci* **Rule 18: Reliability** 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ciThe APIs provided by the operating system must be fully reliable. 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ciReliability does not mean that every call has to be successful. If resources are used up, providing a proper return value or exception is also an implementation of reliability. 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ciFor every API, all the possible situations must be accurately defined, and the work should be completed according to the defined behavior in the corresponding situation. 421e41f4b71Sopenharmony_ci 422e41f4b71Sopenharmony_ciUnreliable behavior includes failure to return a value as scheduled or causing application exceptions for no reason. 423e41f4b71Sopenharmony_ci 424e41f4b71Sopenharmony_ci* **Rule 19: Complete functionalities** 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_ciWhen planning a feature or capability, consider the completeness of functionalities. For example, a process should not be interrupted or an option should not be missing within the supported scope. This requires you to perform sufficient verification and deduction when designing the APIs. 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ciEven if the comprehensive features of an operating system are gradually provided through a series of iterations across years, every specific version should provide closed and self-consistent capabilities. To achieve this goal, you must define clear boundaries for each iteration. 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci* **Rule 20: Permission and privacy protection** 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ciPermission control and data protection must be provided for any API that involves user data or user privacy. 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ciComply with the following principles for permission control: 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci1. Completeness: All behaviors across application sandboxes must be limited by permission control. 437e41f4b71Sopenharmony_ci2. Optimal granularity: One permission protects only one type of object. Developers only need to apply for the permission of the minimum granularity to access an API. 438e41f4b71Sopenharmony_ci3. Clearness: The protected object, permission scope, and sensitivity level must be clearly specified in the permission definition. 439e41f4b71Sopenharmony_ci4. Minimum scope: A permission is assigned only to applications that have service requirements. 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ciComply with the following principles for privacy protection: 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci1. The return value should contain only necessary information. 444e41f4b71Sopenharmony_ci2. The API calling should not obtain or collect users' personal data, unless required permissions are assigned by users. 445e41f4b71Sopenharmony_ci3. In case that an API is called by multiple applications, if personal data needs to be disclosed to the callee, the caller must specify the disclosed data type, data receiver, and data use purpose in the privacy statement. 446e41f4b71Sopenharmony_ci4. When an API needs to access sensitive user data (such as calls, contacts, and media content), the system picker mechanism must be used to prevent the API from accessing the data through sensitive permission application. 447e41f4b71Sopenharmony_ci5. Open APIs should not contain unrelated features. 448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_ci* **Rule 21: Concurrent environment** 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ciThe OpenHarmony APIs do not require all threads to be safe, considering the extremely high cost (for example, program complexity and performance impact). You only need to select the APIs as required. 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ciAPIs dedicated for concurrent environments must be properly designed and described. 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ciIn the internal implementation of an API, thread safety issues should be solved as much as possible. 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci* **Rule 22: Resource management closure** 458e41f4b71Sopenharmony_ci 459e41f4b71Sopenharmony_ciIf an API requests dynamic resources, resource release should also be considered. 460e41f4b71Sopenharmony_ci 461e41f4b71Sopenharmony_ciIf the requested resources are returned to the caller, an API must be provided for the caller to release the resources. 462e41f4b71Sopenharmony_ci 463e41f4b71Sopenharmony_ciIf the requested resources are not directly returned to the caller, consider the resource lifecycle and release timing in the API. 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ciIf there is an upper limit for resource usage, describe it clearly and provide an API to check whether the upper limit is reached. 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ciPay special attention to the time for releasing exclusive resources. 468e41f4b71Sopenharmony_ci 469e41f4b71Sopenharmony_ci* **Rule 23: Retry logic** 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ciFor APIs that may encounter calling failures, provide a retry mechanism. For example, for the scenario in which a process cannot obtain an exclusive resource (such as camera) that is already occupied by another, provide an API to query whether the resource is available. 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_ciTo prevent repeated attempts, you can provide a listener. 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ciSpecify the maximum number of retries allowed after the API call failure. 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci* **Rule 24: Idempotence requirements** 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ciIn mathematics, the idempotent function expression is **f(x) = f(f(x))**. For example, the function for calculating the absolute value, that is, **abs(x) = abs(abs(x))**, is idempotent. 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ciIn computer science, idempotence indicates that one or more requests for a resource should have the same effect. 482e41f4b71Sopenharmony_ci 483e41f4b71Sopenharmony_ciFrom the perspective of APIs, the effect of calling the API once to open a file should be the same as the effect of calling the API multiple times to open the file. Similarly, the effect of calling the API once to close a file should be the same as the effect of calling the API multiple times to close the file. 484e41f4b71Sopenharmony_ci 485e41f4b71Sopenharmony_ci* **Rule 25: Device universality** 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ciOpenHarmony is designed for various types of devices. 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ciThe API design should meet the device universality requirements. Example: 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ci* For UI components, consider the size of different screens. 492e41f4b71Sopenharmony_ci* For data storage, consider the size of different storage devices. 493e41f4b71Sopenharmony_ci* For user input events, consider different user interaction modes, such as touches, voice, and keys. 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ciCertain APIs are available only for specific devices. Example: 496e41f4b71Sopenharmony_ci 497e41f4b71Sopenharmony_ci* Health sensor APIs are available only for wearables. 498e41f4b71Sopenharmony_ci* Vehicle control APIs are available only for telematics devices. 499e41f4b71Sopenharmony_ci 500e41f4b71Sopenharmony_ciYou can calibrate the API application scenarios by referring to [SysCap](../application-dev/reference/syscap.md). 501e41f4b71Sopenharmony_ci 502e41f4b71Sopenharmony_ci### Consistency 503e41f4b71Sopenharmony_ci 504e41f4b71Sopenharmony_ci* **Rule 26: Consistent terms and concepts** 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ciFor purposes of easy understanding, use consistent terms and concepts in API names and descriptions. Avoid jargon. Abstract scenario-specific service models to form coherent and consistent OpenHarmony concepts. 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ciIn this regard, observe the following principles: 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ci1. Use a single term for each concept. 511e41f4b71Sopenharmony_ci2. Terms should be appropriate, explainable, and easy to understand. 512e41f4b71Sopenharmony_ci4. The definition of terms should be accurate and unambiguous. 513e41f4b71Sopenharmony_ci5. Common terms in the industry should not be redefined and should be used by following the industry practices. 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ciIn general, use terms already defined in [OpenHarmony Glossary](../glossary.md). If necessary, you can add terms to the glossary. 516e41f4b71Sopenharmony_ci 517e41f4b71Sopenharmony_ci* **Rule 27: Consistent inter-device behavior** 518e41f4b71Sopenharmony_ci 519e41f4b71Sopenharmony_ciBy default, an API should behavior the same between devices of different types. 520e41f4b71Sopenharmony_ci 521e41f4b71Sopenharmony_ciIf the behavior will be different because of the device types, clearly describe the differences and provide a check mechanism. 522e41f4b71Sopenharmony_ci 523e41f4b71Sopenharmony_ci* **Rule 28: Consistent version evolution** 524e41f4b71Sopenharmony_ci 525e41f4b71Sopenharmony_ciBy default, every API should provide consistent behavior in different versions. If an API change will cause behavior incompatibility, the minimum requirement is to distinguish the target version of the applications. 526e41f4b71Sopenharmony_ci 527e41f4b71Sopenharmony_ciSimply speaking, API behavior changes should not affect developed applications. 528e41f4b71Sopenharmony_ci 529e41f4b71Sopenharmony_ci* **Rule 29: Consistent naming style** 530e41f4b71Sopenharmony_ci 531e41f4b71Sopenharmony_ciAccurate API names do not always mean consistency. For example, words with similar meanings, such as **picture** and **image**, and **path** and **URL**, are often seen in the same set of APIs. You should use the same name when referring to the same object. 532e41f4b71Sopenharmony_ci 533e41f4b71Sopenharmony_ciFor example, the following APIs, which are used to obtain media assets, have different naming styles: 534e41f4b71Sopenharmony_ci 535e41f4b71Sopenharmony_ci```js 536e41f4b71Sopenharmony_cideclare function getMediaAsserts(): Array<MediaAssert>; 537e41f4b71Sopenharmony_cideclare function getAudios(): Array<AudioAssert>; 538e41f4b71Sopenharmony_cideclare function getVideos(): Array<VideoAssert>; 539e41f4b71Sopenharmony_cideclare function getImages(): Array<ImageAssert>; 540e41f4b71Sopenharmony_ci``` 541e41f4b71Sopenharmony_ci 542e41f4b71Sopenharmony_ciThey should be named as follows: 543e41f4b71Sopenharmony_ci 544e41f4b71Sopenharmony_ci```js 545e41f4b71Sopenharmony_cifunction getMediaAsserts(): Array<MediaAssert>; 546e41f4b71Sopenharmony_cifunction getAudioAsserts(): Array<AudioAssert>; 547e41f4b71Sopenharmony_cifunction getVideoAsserts(): Array<VideoAssert>; 548e41f4b71Sopenharmony_cifunction getImageAsserts(): Array<ImageAssert>; 549e41f4b71Sopenharmony_ci``` 550e41f4b71Sopenharmony_ci 551e41f4b71Sopenharmony_ci* **Rule 30: Consistent parameter sequence** 552e41f4b71Sopenharmony_ci 553e41f4b71Sopenharmony_ciThe same parameter sequence should be used for APIs in the same namespace or module. 554e41f4b71Sopenharmony_ci 555e41f4b71Sopenharmony_ciFor example, there is no restriction on the sequence of **deviceId** and **missionId** in a single API, but their sequence in the APIs of a module must be the same. 556e41f4b71Sopenharmony_ci 557e41f4b71Sopenharmony_ci```js 558e41f4b71Sopenharmony_cifunction getMissionInfo(deviceId: string, missionId: number): Promise<MissionInfo>; 559e41f4b71Sopenharmony_cifunction getMissionSnapShot(deviceId: string, missionId: number): Promise<MissionSnapshot>; 560e41f4b71Sopenharmony_ci 561e41f4b71Sopenharmony_ci// Correct 562e41f4b71Sopenharmony_cifunction getLowResolutionMissionSnapShot(deviceId: string, missionId: number): Promise<MissionSnapshot>; 563e41f4b71Sopenharmony_ci 564e41f4b71Sopenharmony_ci// Incorrect 565e41f4b71Sopenharmony_cifunction getLowResolutionMissionSnapShot(missionId: number, deviceId: string): Promise<MissionSnapshot>; 566e41f4b71Sopenharmony_ci``` 567e41f4b71Sopenharmony_ci 568e41f4b71Sopenharmony_ci* **Rule 31: Consistent synchronous/asynchronous style** 569e41f4b71Sopenharmony_ci 570e41f4b71Sopenharmony_ciAsynchronous APIs should be able to be determined based on input parameters and return values. They should use the same style. Example: 571e41f4b71Sopenharmony_ci 572e41f4b71Sopenharmony_ci```javascript 573e41f4b71Sopenharmony_ci// Callback mode 574e41f4b71Sopenharmony_cifunction getDefaultDisplay(callback: AsyncCallback<Display>): void; 575e41f4b71Sopenharmony_ci// Promise mode 576e41f4b71Sopenharmony_cifunction getDefaultDisplay(): Promise<Display>; 577e41f4b71Sopenharmony_ci``` 578e41f4b71Sopenharmony_ci 579e41f4b71Sopenharmony_ciIf both synchronous and asynchronous APIs are provided, you can add the suffix **Sync** to the synchronization API name for better distinguishing. Example: 580e41f4b71Sopenharmony_ci 581e41f4b71Sopenharmony_ci```js 582e41f4b71Sopenharmony_cifunction getDefaultDisplaySync(): Display; 583e41f4b71Sopenharmony_ci``` 584e41f4b71Sopenharmony_ci 585e41f4b71Sopenharmony_ciIf only the synchronization API is provided and the return value is not **void**, you do not need to add the suffix. Example: 586e41f4b71Sopenharmony_ci 587e41f4b71Sopenharmony_ci```js 588e41f4b71Sopenharmony_cifunction registerMissionListener(listener: MissionListener): number; 589e41f4b71Sopenharmony_ci``` 590e41f4b71Sopenharmony_ci 591e41f4b71Sopenharmony_ci### Compatibility 592e41f4b71Sopenharmony_ci 593e41f4b71Sopenharmony_ci* **Rule 32: Version compatibility** 594e41f4b71Sopenharmony_ci 595e41f4b71Sopenharmony_ciCarry out full planning and all-round consideration at the very beginning, since API changes pose a high cost. However, you must notice that API changes are inevitable during system development. 596e41f4b71Sopenharmony_ci 597e41f4b71Sopenharmony_ciAPI changes must be backward compatible. After an API is deprecated, mark the API **deprecated** in the source code and reference document, and provide the new counterpart. 598e41f4b71Sopenharmony_ci 599e41f4b71Sopenharmony_ciRetain the deprecated APIs in at least five API versions released since the deprecation. After five versions, you can delete the deprecated APIs after providing obvious notifications to developers and leaving sufficient time for them to modify the applications. 600e41f4b71Sopenharmony_ci 601e41f4b71Sopenharmony_ci* **Rule 33: Backward binary-code compatibility** 602e41f4b71Sopenharmony_ci 603e41f4b71Sopenharmony_ciBinary-code compatibility means that an existing program can be linked to and run properly without recompilation after version evolution. It ensures that the memory layout of an instance does not change in the case of a version update. 604e41f4b71Sopenharmony_ci 605e41f4b71Sopenharmony_ciCommon C++ API changes that cause binary-code incompatibility include: 606e41f4b71Sopenharmony_ci 607e41f4b71Sopenharmony_ci1. Deleting any API elements 608e41f4b71Sopenharmony_ci2. Adding a virtual function 609e41f4b71Sopenharmony_ci3. Changing the inheritance of a class 610e41f4b71Sopenharmony_ci4. Changing the declaration sequence of virtual functions 611e41f4b71Sopenharmony_ci5. Adding a non-static member variable 612e41f4b71Sopenharmony_ci6. Changing the declaration sequence of non-static member variables 613e41f4b71Sopenharmony_ci 614e41f4b71Sopenharmony_ciIn terms of binary-code compatibility, C APIs have inherent advantages over C++ APIs. Therefore, you are advised to define native APIs using C. 615e41f4b71Sopenharmony_ci 616e41f4b71Sopenharmony_ci### API References 617e41f4b71Sopenharmony_ci 618e41f4b71Sopenharmony_ciAPI references are used to carry API information. Their quality is vital for the ecosystem development. 619e41f4b71Sopenharmony_ci 620e41f4b71Sopenharmony_ci* **Rule 34: Basic descriptions for modules, namespaces, classes, and functions** 621e41f4b71Sopenharmony_ci 622e41f4b71Sopenharmony_ciA basic description must be provided for each module, namespace, class, and function. 623e41f4b71Sopenharmony_ci 624e41f4b71Sopenharmony_ciA detailed description must be provided for key modules and complex modules. 625e41f4b71Sopenharmony_ci 626e41f4b71Sopenharmony_ciThe description should be in English. 627e41f4b71Sopenharmony_ci 628e41f4b71Sopenharmony_ci* **Rule 35: Clear description of use cases** 629e41f4b71Sopenharmony_ci 630e41f4b71Sopenharmony_ciSample code must be provided for all APIs, and common application scenarios must be covered. 631e41f4b71Sopenharmony_ci 632e41f4b71Sopenharmony_ciA detailed application scenario description should be provided for complex APIs. You can provide a link to the tutorial that exhibits how to use the API. 633e41f4b71Sopenharmony_ci 634e41f4b71Sopenharmony_ci* **Rule 36: Accurate API description** 635e41f4b71Sopenharmony_ci 636e41f4b71Sopenharmony_ciThere should be no typos in the API description. 637e41f4b71Sopenharmony_ci 638e41f4b71Sopenharmony_ciAll code samples must be able to run properly. If the behavior of an API varies in different versions, describe the differences. 639e41f4b71Sopenharmony_ci 640e41f4b71Sopenharmony_ciMark deprecated APIs and provide the counterparts. 641e41f4b71Sopenharmony_ci 642e41f4b71Sopenharmony_ci* **Rule 37: Accurate parameter description** 643e41f4b71Sopenharmony_ci 644e41f4b71Sopenharmony_ciEach parameter in an API must be clearly described. Example: 645e41f4b71Sopenharmony_ci 646e41f4b71Sopenharmony_ci* For a non-simple type, specify whether the parameter can be null. 647e41f4b71Sopenharmony_ci* For an enum type, describe the use case of each enumerated value. 648e41f4b71Sopenharmony_ci* For an optional parameter, specify when the caller needs to pass the parameter and when to ignore the parameter. 649e41f4b71Sopenharmony_ci 650e41f4b71Sopenharmony_ci**Rule 38: Accurate return value/exception description** 651e41f4b71Sopenharmony_ci 652e41f4b71Sopenharmony_ciIf an API has a return value or an exception, describe them in the API reference. Example: 653e41f4b71Sopenharmony_ci 654e41f4b71Sopenharmony_ci```js 655e41f4b71Sopenharmony_ci/** 656e41f4b71Sopenharmony_ci * Sync function of rename. 657e41f4b71Sopenharmony_ci * @param {string} path - path. 658e41f4b71Sopenharmony_ci * @returns {void} rmdir success. 659e41f4b71Sopenharmony_ci * @throws {BusinessError} 401 - if type of path is not string. 660e41f4b71Sopenharmony_ci * @throws {BusinessError} 201 - if permission denied. 661e41f4b71Sopenharmony_ci * @syscap SystemCapability.FileManagement.File.FileIO 662e41f4b71Sopenharmony_ci * @since 7 663e41f4b71Sopenharmony_ci */ 664e41f4b71Sopenharmony_cideclare function rmdirSync(path: string): void; 665e41f4b71Sopenharmony_ci``` 666e41f4b71Sopenharmony_ci 667e41f4b71Sopenharmony_ci* **Rule 39: Complete meta information** 668e41f4b71Sopenharmony_ci 669e41f4b71Sopenharmony_ciThe API reference must contain basic meta information, such as **@syscap** and **@since**. 670e41f4b71Sopenharmony_ci 671e41f4b71Sopenharmony_ciThe meta information describes the basic information about an API. The tool and SDK will use the information to perform corresponding processing, for example, displaying a message for a deprecated API. 672e41f4b71Sopenharmony_ci 673e41f4b71Sopenharmony_ci* **Rule 40: Consistent style** 674e41f4b71Sopenharmony_ci 675e41f4b71Sopenharmony_ciThe API reference document must use consistent style, for example, bold text and image colors. 676e41f4b71Sopenharmony_ci 677e41f4b71Sopenharmony_ci### Organization Mode 678e41f4b71Sopenharmony_ci 679e41f4b71Sopenharmony_ci* **Rule 41: Reasonable layering** 680e41f4b71Sopenharmony_ci 681e41f4b71Sopenharmony_ciAn operating system is usually structured in a hierarchical model, and each layer solves different levels of problems. 682e41f4b71Sopenharmony_ci 683e41f4b71Sopenharmony_ciYou need to consider layering during API design. 684e41f4b71Sopenharmony_ci 685e41f4b71Sopenharmony_ciFor example, in the construction industry, all buildings have walls and doors, the walls are made of bricks, and the doors are made of wood. There are abstract layers as follows: 686e41f4b71Sopenharmony_ci 687e41f4b71Sopenharmony_ci* The first layer is raw materials, including cement, sand, and wood. 688e41f4b71Sopenharmony_ci* The second layer is building elements constructed from raw materials. Examples of building elements are doors, windows, and walls. 689e41f4b71Sopenharmony_ci* The third floor is the type of room, for example, bedroom, bathroom, and living room. 690e41f4b71Sopenharmony_ci* The top floor is a variety of buildings for different purposes, such as hotels and apartments. 691e41f4b71Sopenharmony_ci 692e41f4b71Sopenharmony_ciEach layer has its own concepts to consider and problems to solve. Think carefully about the layer that your API targets. 693e41f4b71Sopenharmony_ci 694e41f4b71Sopenharmony_ci* **Rule 42: Proper module division** 695e41f4b71Sopenharmony_ci 696e41f4b71Sopenharmony_ciIn addition to horizontal layering, you should pay attention to vertical layering, that is, module division. OpenHarmony APIs are organized in the form of namespaces. A namespace usually corresponds to a module. 697e41f4b71Sopenharmony_ci 698e41f4b71Sopenharmony_ciA smaller subsystem can provide only one namespace and have all the APIs in this namespace. A large subsystem can provide multiple namespaces. 699e41f4b71Sopenharmony_ci 700e41f4b71Sopenharmony_ci* **Rule 43: API reference document available (as part of the API)** 701e41f4b71Sopenharmony_ci 702e41f4b71Sopenharmony_ciWhen talking about the APIs provided for developers, we mean not only programming interfaces, but also anything that can be exposed. 703e41f4b71Sopenharmony_ci 704e41f4b71Sopenharmony_ciThe API reference document is one that exposed. It should be managed according to the same rules as the APIs. 705e41f4b71Sopenharmony_ci 706e41f4b71Sopenharmony_ci### Quality Related 707e41f4b71Sopenharmony_ci 708e41f4b71Sopenharmony_ci* **Rule 44: Performance requirements met** 709e41f4b71Sopenharmony_ci 710e41f4b71Sopenharmony_ciThe APIs provided by an operating system may be used by all upper-layer applications. They must meet performance requirements in practice. 711e41f4b71Sopenharmony_ci 712e41f4b71Sopenharmony_ciThe following lists some requirements: 713e41f4b71Sopenharmony_ci 714e41f4b71Sopenharmony_ci1. Respond in a timely manner to avoid callers waiting. If an API call takes a long time, use an asynchronous API. 715e41f4b71Sopenharmony_ci2. Use the shared memory and message queue when a large amount of data needs to be transmitted. 716e41f4b71Sopenharmony_ci3. Minimize the number of new process entities. 717e41f4b71Sopenharmony_ci4. Release resources in a timely manner. A fault tolerance mechanism must be provided for abnormal scenarios to ensure timely resource release. 718e41f4b71Sopenharmony_ci 719e41f4b71Sopenharmony_ci* **Rule 45: Proper power consumption** 720e41f4b71Sopenharmony_ci 721e41f4b71Sopenharmony_ciOpenHarmony was designed for different types of devices, most of which were passive devices. Therefore, power consumption must be considered during API design. 722e41f4b71Sopenharmony_ci 723e41f4b71Sopenharmony_ciDuring the implementation of each function or mechanism, consider power consumption. Provide a detailed description for high power consumption APIs and offer guidance on how to use them. 724e41f4b71Sopenharmony_ci 725e41f4b71Sopenharmony_ciProvide a mechanism to prevent APIs from consuming power heavily. For example, after the device screen is locked or the application is switched to the background, stop the high power consumption behavior. 726e41f4b71Sopenharmony_ci 727e41f4b71Sopenharmony_ciEnsure that power consumption does not deteriorate during version evolution. 728e41f4b71Sopenharmony_ci 729e41f4b71Sopenharmony_ci* **Rule 46: Testable** 730e41f4b71Sopenharmony_ci 731e41f4b71Sopenharmony_ciProvide complete automatic test cases to: 732e41f4b71Sopenharmony_ci 733e41f4b71Sopenharmony_ci1. Quick detect faults during API development to improve API quality. 734e41f4b71Sopenharmony_ci2. Ensure that code modification does not affect existing functionalities during version iteration. 735e41f4b71Sopenharmony_ci3. Ensure backward compatibility of APIs. 736e41f4b71Sopenharmony_ci 737e41f4b71Sopenharmony_ciAn OpenHarmony API must meet the following requirements: 738e41f4b71Sopenharmony_ci 739e41f4b71Sopenharmony_ci1. Automatic API test cases must be delivered for all new APIs. 740e41f4b71Sopenharmony_ci2. Use a test case for each single scenario. One test case covers a single functionality of an API, simplifying the code logic of each single test case. 741e41f4b71Sopenharmony_ci3. The test case execution must be efficient. The execution time of each test case must be limited to milliseconds. 742e41f4b71Sopenharmony_ci4. Automatic API test cases must be available for all APIs. 743e41f4b71Sopenharmony_ci5. Assertions with functional logic are required in addition to the capability of throwing exceptions. 744e41f4b71Sopenharmony_ci 745e41f4b71Sopenharmony_ci* **Rule 47: Environmentally adaptable** 746e41f4b71Sopenharmony_ci 747e41f4b71Sopenharmony_ciTo support user personality features, an operating system usually provides environment customization capabilities, such as language, light/dark theme, and font size. 748e41f4b71Sopenharmony_ci 749e41f4b71Sopenharmony_ciFor related APIs, if developers do not pass specified parameters, the results in the corresponding environment should be returned based on the environment in use. 750e41f4b71Sopenharmony_ci 751e41f4b71Sopenharmony_ci## Description of Post-Release Evaluation Specifications 752e41f4b71Sopenharmony_ci 753e41f4b71Sopenharmony_ciEven though all the pre-release rules have been satisfied, some issues might be detected during API use. 754e41f4b71Sopenharmony_ci 755e41f4b71Sopenharmony_ciIt does not mean that these issues do not need to be considered during API design. 756e41f4b71Sopenharmony_ci 757e41f4b71Sopenharmony_ciOn the contrary, you should carry out full considerations during the design to prevent these issues from occurring after the API release. 758e41f4b71Sopenharmony_ci 759e41f4b71Sopenharmony_ciIf issues related to the following rules occur after the API release, the API quality is not good. 760e41f4b71Sopenharmony_ci 761e41f4b71Sopenharmony_ci### Stability 762e41f4b71Sopenharmony_ci 763e41f4b71Sopenharmony_ci* **Rule 48: Minimized rate for deprecated or changed APIs** 764e41f4b71Sopenharmony_ci 765e41f4b71Sopenharmony_ciFor an API, stability is the most important attribute. 766e41f4b71Sopenharmony_ci 767e41f4b71Sopenharmony_ciAPI deprecation and behavior changes greatly reduce maintenance efficiency of developers, especially when multiple device types and system versions are involved. 768e41f4b71Sopenharmony_ci 769e41f4b71Sopenharmony_ciDesigning an API with long-term stability and continuous compatibility is the goal of every API designer. 770e41f4b71Sopenharmony_ci 771e41f4b71Sopenharmony_ciThe API deprecation rate and change rate are inversely proportional to the API quality to some extent. 772e41f4b71Sopenharmony_ci 773e41f4b71Sopenharmony_ci### Security 774e41f4b71Sopenharmony_ci 775e41f4b71Sopenharmony_ci* **Rule 49: No abuse of APIs** 776e41f4b71Sopenharmony_ci 777e41f4b71Sopenharmony_ciAll APIs should be designed to prevent themselves from being abused. Abuse means that the use of an API exceeds the expected limit, either in quantity or scope. 778e41f4b71Sopenharmony_ci 779e41f4b71Sopenharmony_ciFor example, the capability for accessing user data (such as **Gallery** and **Contacts**) or running in the background for a long time may be abused. 780e41f4b71Sopenharmony_ci 781e41f4b71Sopenharmony_ciTo prevent abuse in terms of quantity, use one-time authorization. 782e41f4b71Sopenharmony_ci 783e41f4b71Sopenharmony_ciTo prevent abuse in terms of scope, perform restrictions based on the caller identity. 784e41f4b71Sopenharmony_ci 785e41f4b71Sopenharmony_ciNo matter what mechanism is used, describe it in the API reference. In the case of excessive abuse, notify the caller through a specific return value. 786e41f4b71Sopenharmony_ci 787e41f4b71Sopenharmony_ci* **Rule 50: No exploitation of APIs** 788e41f4b71Sopenharmony_ci 789e41f4b71Sopenharmony_ciExploitation means that the use of an API causes negative impact, for example, system attacks. 790e41f4b71Sopenharmony_ci 791e41f4b71Sopenharmony_ciIf a system problem occurs when an API or a combination of APIs is called, regardless of how they are called, there are defects in the API. 792e41f4b71Sopenharmony_ci 793e41f4b71Sopenharmony_ciIt is forbidden to use an API if it (in any case) can be called to crash the system, prevent the system from working, or steal user data. You should consider all possible calling scenarios to avoid extreme situations. 794e41f4b71Sopenharmony_ci 795e41f4b71Sopenharmony_ci### Maintainability 796e41f4b71Sopenharmony_ci 797e41f4b71Sopenharmony_ci* **Rule 51: Evolution support** 798e41f4b71Sopenharmony_ci 799e41f4b71Sopenharmony_ciA large feature of the operating system may take years to complete. This requires you to consider scalability during API design. 800e41f4b71Sopenharmony_ci 801e41f4b71Sopenharmony_ciIf a group of new APIs are added to the new version and the original APIs are deprecated, the API design is poor. This situation should be avoided. 802e41f4b71Sopenharmony_ci 803e41f4b71Sopenharmony_ci* **Rule 52: Consistent behavior after feature expansion** 804e41f4b71Sopenharmony_ci 805e41f4b71Sopenharmony_ciWith the system evolution, it is common that new parameters are added to APIs or new options are added to parameters. 806e41f4b71Sopenharmony_ci 807e41f4b71Sopenharmony_ciIt is important to keep API behavior consistent. The original behavior should not be damaged by the introduction of new scenarios. 808e41f4b71Sopenharmony_ci 809e41f4b71Sopenharmony_ciIf the behavior has to be changed, only the applications using the new version provide the new behavior. The behavior of applications using the old version remains unchanged. 810e41f4b71Sopenharmony_ci 811e41f4b71Sopenharmony_ci* **Rule 53: Documents updated accordingly** 812e41f4b71Sopenharmony_ci 813e41f4b71Sopenharmony_ciWhen updating the API implementation, update the API reference document accordingly. From the perspective of compatibility, the original behavior should not be changed. New functionalities should be implemented by new APIs. 814e41f4b71Sopenharmony_ci 815e41f4b71Sopenharmony_ciHowever, you can change the behavior of an existing API in the following cases: 816e41f4b71Sopenharmony_ci 817e41f4b71Sopenharmony_ci1. The change is made to rectify the defect. 818e41f4b71Sopenharmony_ci2. The change is made to improve performance or reduce power consumption. 819e41f4b71Sopenharmony_ci3. The change is made to provide new features or scenarios, without affecting the original feature or scenario logic. 820e41f4b71Sopenharmony_ci 821e41f4b71Sopenharmony_ciDescribe the first and second cases in the Release Notes, and describe the third case in the API reference document. 822e41f4b71Sopenharmony_ci 823e41f4b71Sopenharmony_ci### Irreplaceability 824e41f4b71Sopenharmony_ci 825e41f4b71Sopenharmony_ci* **Rule 54: Orthogonal design** 826e41f4b71Sopenharmony_ci 827e41f4b71Sopenharmony_ciOrthogonality means that APIs should not overlap. 828e41f4b71Sopenharmony_ci 829e41f4b71Sopenharmony_ciFor example, if an API provides the capability of creating a user account, whereas another API provides the capability of creating a user and logging in to the system, the orthogonality rule is not satisfied. You can change the logic of the second API so it provides the login capability only. 830e41f4b71Sopenharmony_ci 831e41f4b71Sopenharmony_ciIf you draw API capabilities one by one, there should be no overlap between these graphs. For APIs at the same layer, it is not allowed that an API provides capabilities A, B, and C, whereas another API provides capabilities B, C, and D. 832e41f4b71Sopenharmony_ci 833e41f4b71Sopenharmony_ciTo facilitate invocation, some APIs can be combined into a higher-level API. This uses different abstraction levels, not an overlap at the same layer. 834e41f4b71Sopenharmony_ci 835e41f4b71Sopenharmony_ci### Feedback from Developers 836e41f4b71Sopenharmony_ci 837e41f4b71Sopenharmony_ci* **Rule 55: Developer feedback noticed** 838e41f4b71Sopenharmony_ci 839e41f4b71Sopenharmony_ciA released API may have defects detected, even after going through the trial phase. 840e41f4b71Sopenharmony_ci 841e41f4b71Sopenharmony_ciTherefore, you should continue to notice the feedback from developers after the API release. 842e41f4b71Sopenharmony_ci 843e41f4b71Sopenharmony_ciThe following types of feedback may be possible: 844e41f4b71Sopenharmony_ci 845e41f4b71Sopenharmony_ci* Requirements: Developers ask for new APIs to provide capabilities. You can plan these requirements in later versions. 846e41f4b71Sopenharmony_ci* Defects related to API behavior: The behavior of an API is inconsistent with that described in the reference document. This defect should be fixed as soon as possible. If the inconsistency is caused by incorrect description in the reference document, correct the document. 847e41f4b71Sopenharmony_ci* Inappropriate API design: The name or parameter setting of an API has defects. In this case, develop a new API to replace the defective API. 848e41f4b71Sopenharmony_ci 849e41f4b71Sopenharmony_ciIn some cases, you may need to change the behavior of a published API. Note that behavior changes take effect only on new applications. The behavior can be determined based on the target API version of the application. 850e41f4b71Sopenharmony_ci 851e41f4b71Sopenharmony_ciIn the worst case, you may need to deprecate an existing API and use a new one. 852