GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing azure.github.io — C++ Guidelines: Introduction | Azure SDKs C++ Guidelines: IntroductionIntroductionThe C++ guidelines are for the benefit of client library designers targeting service applications written in C++. You do not have to write a client library for C++ if your service is not normally accessed from C++. Design PrinciplesThe Azure SDK should be designed to enhance the productivity of developers connecting to Azure services. Other qualities (such as completeness, extensibility, and performance) are important but secondary. Productivity is achieved by adhering to the principles described below: Idiomatic
Consistent
Approachable
Diagnosable
Dependable
General Guidelines✅ DO follow the General Azure SDK Guidelines. Support for non-HTTP ProtocolsThis document contains guidelines developed primarily for typical Azure REST services, i.e. stateless services with request-response based interaction model. Many of the guidelines in this document are more broadly applicable, but some might be specific to such REST services. Azure SDK API DesignThe API surface of your client library must have the most thought as it is the primary interaction that the consumer has with your service. ✅ DO use clear, concise, and meaningful names. ⚠️ YOU SHOULD NOT use abbreviations unless necessary or when they are commonly used and understood. For example, ✅ DO consult the Architecture Board if you wish to use a dependency that is not on the list of approved dependencies. Service ClientService clients are the main starting points for developers calling Azure services with the Azure SDK. Each client library should have at least one client in its main namespace, so it’s easy to discover. The guidelines in this section describe patterns for the design of a service client. There exists a distinction that must be made clear with service clients: not all classes that perform HTTP (or otherwise) requests to a service are automatically designated as a service client. A service client designation is only applied to classes that are able to be directly constructed because they are uniquely represented on the service. Additionally, a service client designation is only applied if there is a specific scenario that applies where the direct creation of the client is appropriate. If a resource can not be uniquely identified or there is no need for direct creation of the type, then the service client designation should not apply. ✅ DO name service client types with the Client suffix (for example, ✅ DO place service client types that the consumer is most likely to interact with in the root namespace of the client library (for example, ✅ DO make service clients ✅ DO ensure that all service client classes thread safe (usually by making them immutable and stateless). ✅ DO expose a Service Client Constructors✅ DO provide a minimal constructor that takes only the parameters required to connect to the service. ⛔️ DO NOT use default parameters in the simplest constructor. ✅ DO provide constructor overloads that allow specifying additional options via an ✔️ YOU MAY provide a constructor that takes a connection if the service supports it. ☑️ YOU SHOULD recommend customers use passwordless authentication methods to connect to the service. Client Configuration✅ DO use relevant global configuration settings either by default or when explicitly requested to by the user, for example by passing in a configuration object to a client constructor. ✅ DO allow different clients of the same type to use different configurations. ✅ DO allow consumers of your service clients to opt out of all global configuration settings at once. ✅ DO allow all global configuration settings to be overridden by client-provided options. The names of these options should align with any user-facing global configuration keys. ⛔️ DO NOT Change the default values of client configuration options based on system or program state. ⛔️ DO NOT Change default values of client configuration options based on how the client library was built. ⛔️ DO NOT change behavior based on configuration changes that occur after the client is constructed. Hierarchies of clients inherit parent client configuration unless explicitly changed or overridden. Exceptions to this requirement are as follows:
⛔️ DO NOT use client library specific runtime configuration such as environment variables or a config file. Keep in mind that many IoT devices won’t have a filesystem or an “environment block” to read from. Using ClientOptionsService Versions✅ DO call the highest supported service API version by default. ✅ DO allow the consumer to explicitly select a supported service API version when instantiating the service client. Use a constructor parameter called
MockingService MethodsService methods are the methods on the client that invoke operations on the service. Sync and AsyncThe C++ SDK is designed for synchronous api calls. ✅ DO provide a synchronous programming model. ⛔️ DO NOT provide an async programming model. Naming
✅ DO name functions with PascalCase. For example: ☑️ YOU SHOULD declare all functions that can never throw exceptions ✅ DO declare all client functions as ✅ DO name namespace scope variables intended for user consumption with PascalCase. ✅ DO name namespace scope ✅ DO name namespace scope non-constant variables intended only for internal consumption with a ⛔️ DO NOT create types in the global namespace. All Azure SDK types MUST be in a namespace. ✅ DO name local variables and parameters with camelCase. ✅ DO use types to enforce units where possible. For example, the C++ standard library provides ✅ DO include units in names when a type based solution to enforce units is not present. If a variable represents weight, or some other unit, then include the unit in the name so developers can more easily spot problems. For example: ✅ DO Declare or define each variable on its own line, except when declaring bitfields. An exception can be made when declaring bitfields (to clarify that the variable is a part of one bitfield). The use of bitfields in general is discouraged. Cancellation✅ DO ensure all service methods, both asynchronous and synchronous, have a final parameter of type The context should be further passed by reference (preferably as a Note that cancellation is a application developer construct, Azure SDK clients MUST NOT cancel the MockingReturn Types✅ DO return
The ✅ DO use one of the following return types to represent an unstructured payload:
✅ DO return a model type if the content has a schema and can be deserialized. For more information, see Model Types Requests to the service fall into two basic groups - methods that make a single logical request, or a deterministic sequence of requests. An example of a single logical request is a request that may be retried inside the operation. An example of a deterministic sequence of requests is a paged operation. The logical entity is a protocol neutral representation of a response. For HTTP, the logical entity may combine data from headers, body and the status line. A common example is exposing an ETag header as a property on the logical entity in addition to any deserialized content from the body. ✅ DO optimize for returning the logical entity for a given request. The logical entity MUST represent the information needed in the 99%+ case. ✅ DO make it possible for a developer to get access to the complete response, including the status line, headers and body. The client library MUST follow the language specific guidance for accomplishing this. For example, you may choose to do something similar to the following: ✅ DO document and provide examples on how to access the raw and streamed response for a given request, where exposed by the client library. We do not expect all methods to expose a streamed response. For methods that combine multiple requests into a single call: ⛔️ DO NOT return headers and other per-request metadata unless it is obvious as to which specific HTTP request the methods return value corresponds to. ✅ DO provide enough information in failure cases for an application to take appropriate corrective action. Thread Safety✅ DO be thread-safe. All public members of the client type must be safe to call from multiple threads concurrently. Service Method ParametersParameter ValidationThe service client will have several methods that perform requests on the service. Service parameters are directly passed across the wire to an Azure service. Client parameters are not passed directly to the service, but used within the client library to fulfill the request. Examples of client parameters include values that are used to construct a URI, or a file that needs to be uploaded to storage. ✅ DO validate client parameters. ⛔️ DO NOT validate service parameters. This includes null checks, empty strings, and other common validating conditions. Let the service validate any request parameters. ✅ DO validate the developer experience when the service parameters are invalid to ensure appropriate error messages are generated by the service. If the developer experience is compromised due to service-side error messages, work with the service team to correct prior to release. Methods Returning Collections (Paging)Although object-orientated languages can eschew low-level pagination APIs in favor of high-level abstractions, C acts as a lower level language and thus embraces pagination APIs provided by the service. You should work within the confines of the paging system provided by the service. ✅ DO export the same paging API as the service provides. ✅ DO indicate in the return type if the consumer has reached the end of the result set. ✅ DO indicate in the return type how many items were returned by the service, and have a list of those items for the consumer to iterate over. Methods Invoking Long Running OperationsSome service operations, known as Long Running Operations or LROs take a long time (up to hours or days). Such operations do not return their result immediately, but rather are started, their progress is polled, and finally the result of the operation is retrieved. Azure::Core library exposes an abstract type called ✅ DO name all methods that start an LRO with the ✅ DO return a subclass of ✔️ YOU MAY add additional APIs to subclasses of Conditional Request Methods✅ DO define ETag-related options e.g., Hierarchical Clients✔️ YOU MAY return clients from other clients e.g., a Supporting TypesIn addition to service client types, Azure SDK APIs provide and use other supporting types as well. Model TypesThis section describes guidelines for the design model types and all their transitive closure of public dependencies (i.e. the model graph). A model type is a representation of a REST service’s resource. ✅ DO ensure model public properties are const if they aren’t intended to be changed by the user. Most output-only models can be fully read-only. Models that are used as both outputs and inputs (i.e. received from and sent to the service) typically have a mixture of read-only and read-write properties. Model Type Naming✅ DO define models using the names from TypeSpec unless those names conflict with keywords or common types from If name collisions are likely and the TypeSpec cannot be changed, you can either use the ✅ DO define model fields using “PascalCase”. ☑️ YOU SHOULD consider the context associated with structure fields when naming structure fields. Customers have expressed confusion when they encounter constructs like Enumerations✅ DO use an ✔️ YOU MAY use a See enumeration-like structure documentation for implementation details. Using Azure Core TypesThe Using Primitive Types✅ DO use The ✅ DO use The ExceptionsError handling is an important aspect of implementing a client library. It is the primary method by which problems are communicated to the consumer. Because we intend for the C client libraries to be used on a wide range of devices with a wide range of reliability requirements, it’s important to provide robust error handling. We distinguish between several different types of errors:
Exhaustion / Act of God ⛔️ DO NOT return an error to the caller. ✅ DO crash, if possible. This means calling some form of fast failing function, like Note: if your client library needs to be resilient to these kinds of errors you must either provide a fallback system, or construct your code in a way to facilitate proving that such errors can not occur. Pre-conditions ✔️ YOU MAY check preconditions on function entry. ✔️ YOU MAY provide a means to disable precondition checks in release / optimized builds. ✅ DO crash, if possible. This means calling some form of fast failing function, like ⛔️ DO NOT throw a C++ exception. Post Conditions ⚠️ YOU SHOULD NOT check post-conditions in a way that changes the computational complexity of the function. ✅ DO provide a way to disable postcondition checks, and omit checking code from built binaries. ✅ DO crash, if possible. This means calling some form of fast failing function, like ⛔️ DO NOT throw a C++ exception. Heap Exhaustion (Out of Memory) ✔️ YOU MAY crash. For example, this may mean dereferencing a nullptr returned by malloc, or explicitly checking and calling abort. Note that on some comonly deployed platforms like Linux, handling heap exhaustion from user mode is not possible in a default configuration. ✔️ YOU MAY propagate a C++ exception of type ⛔️ DO NOT throw bad_alloc from the SDK code itself. Recoverable errors ✅ DO report errors by throwing C++ exceptions defined in the Azure C++ Core Library. For example: ✅ DO produce a recoverable error when any HTTP request fails with an HTTP status code that is not defined by the service/Swagger as a successful status code. ✅ DO document all exceptions each function and its transitive dependencies may throw, except for C++ Exceptions⛔️ DO NOT ✅ DO propagate exceptions thrown by user code, callbacks, or dependencies. Assume any user-provided callback will propagate C++ exceptions unless the SDK documents that the callback must be completely non-throwing. AuthenticationAzure services use a variety of different authentication schemes to allow clients to access the service. Conceptually, there are two entities responsible in this process: a credential and an authentication policy. Credentials provide confidential authentication data. Authentication policies use the data provided by a credential to authenticate requests to the service. ✅ DO support all authentication techniques that the service supports and are available to a client application (as opposed to service side). C is used only for client applications when talking to Azure, so some authentication techniques may not be valid. ✅ DO use credential and authentication policy implementations from the Azure Core library where available. ✅ DO provide credential types that can be used to fetch all data needed to authenticate a request to the service in a non-blocking atomic manner for each authentication scheme that does not have an implementation in Azure Core. ✅ DO provide service client constructors or factories that accept any supported authentication credentials. Service Clients Created after 1/1/2024⛔️ DO NOT provide any authentication mechanism other than managed identity. ⛔️ DO NOT provide any non-standard authentication mechanism. Service Clients Created before 1/1/2024⚠️ YOU SHOULD NOT support providing credential data via a connection string. Connection string interfaces should be provided ONLY IF the service provides a connection string to users via the portal or other tooling. ⛔️ DO NOT support constructing a service client with a connection string unless such connection string. Provide a When implementing authentication, don’t open up the consumer to security holes like PII (personally identifiable information) leakage or credential leakage. Credentials are generally issued with a time limit, and must be refreshed periodically to ensure that the service connection continues to function as expected. Ensure your client library follows all current security recommendations and consider an independent security review of the client library to ensure you’re not introducing potential security problems for the consumer. ⛔️ DO NOT persist, cache, or reuse security credentials. Security credentials should be considered short lived to cover both security concerns and credential refresh situations. If your service implements a non-standard credential system (one that is not supported by Azure Core), then you need to produce an authentication policy for the HTTP pipeline that can authenticate requests given the alternative credential types provided by the client library. ✅ DO Use a “secure” function to zero authentication or authorization credentials as soon as possible once they are no longer needed. Examples of such functions
include: ✅ DO provide a suitable authentication policy that authenticates the HTTP request in the HTTP pipeline when using non-standard credentials. This includes custom connection strings, if supported. NamespacesGrouping services within a cloud infrastructure is common since it aids discoverability and provides structure to the reference documentation. ✅ DO name namespaces using PascalCase. ✅ DO use a root namespace of the form For example, ✅ DO use one of the following pre-approved namespace groups:
If you think a new group should be added to the list, contact [adparch]. ✅ DO pick a shortened service name that allows the consumer to tie the package to the service being used. As a default, use the compressed service name. The namespace does NOT change when the branding of the product changes, so avoid the use of marketing names that may change. A compressed service name is the service name without spaces. It may further be shortened if the shortened version is well known in the community. For example, “Azure Media Analytics” would have a compressed service name of ✅ DO use the following list as the group of services:
If the client library does not seem to fit into the group list, contact the Architecture Board to discuss the namespace requirements. ✅ DO place the management (Azure Resource Manager) API in the
Many ⛔️ DO NOT choose similar names for clients that do different things. ✅ DO register the chosen namespace with the Architecture Board. Open an issue to request the namespace. See the registered namespace list for a list of the currently registered namespaces. ✅ DO place types and functions intended for use in other Azure SDK Clients in a ✅ DO place private implementation details in a Namespace stability contractPublic typesTypes in the Public API surface (types not in an ✅ DO follow semantic versioning rules. Internal typesTypes in the Internal API surface (types in an ⛔️ DO NOT define internal types in public Azure SDK headers. ✅ DO define internal types in headers located in an ✔️ YOU MAY introduce breaking changes to internal types as long as those breaking changes will not break existing Azure SDK clients. ✔️ YOU MAY reference internal types in public headers. ✔️ YOU MAY include template specializations for internal templates in public headers to simplify the implementation. NOTE: Since these types are typically located in the In addition, it is critical to realize that the only dependencies between Azure SDK packages is a >= dependency. That means that a particular version of an existing Azure SDK package is expected to work with any future versions of other Azure SDK packages, even of those packages on which it depends. That means that any breaking changes to internal types MUST be upward compatible. Private typesPrivate types are types located in a ⛔️ DO NOT define private types in public Azure SDK headers. ⛔️ DO NOT consume internal types outside the package in which they are defined. ☑️ YOU SHOULD declare and/or define private types under a ✔️ YOU MAY modify private types without fear of introducing breaking changes. ✔️ YOU MAY reference private types in public headers. ✔️ YOU MAY include template specializations for private templates in public headers to simplify the implementation. Example Namespaces Here are some examples of namespaces that meet these guidelines:
Here are some namespaces that do not meet the guidelines:
Support for Mocking
Azure SDK Library DesignPackaging☑️ YOU SHOULD provide both dynamic and static linking options for your library. Each has its own merits and use cases. ✅ DO publish your package in source code format. Due to differences in platforms, this is the most common publishing mechanism for C libraries. ✅ DO publish your package to vcpkg, a C++ library manager supporting Windows, Linux, and MacOS. Common LibrariesVersioningClient Versions✅ DO be 100% backwards compatible with older versions of the same package. ✅ DO introduce a new package (with new assembly names, new namespace names, and new type names) if you must do an API breaking change. Breaking changes should happen rarely, if ever. Register your intent to do a breaking change with [adparch]. You’ll need to have a discussion with the language architect before approval. Package Version NumbersConsistent version number scheme allows consumers to determine what to expect from a new version of the library. ✅ DO use MAJOR.MINOR.PATCH format for the version of the library dll and the NuGet package. Use -beta._N suffix for beta package versions. For example, 1.0.0-beta.2. ✅ DO change the version number of the client library when ANYTHING changes in the client library. ✅ DO increment the patch version when fixing a bug. ⛔️ DO NOT include new APIs in a patch release. ✅ DO increment the major or minor version when adding support for a service API version. ✅ DO increment the major or minor version when adding a new method to the public API. ☑️ YOU SHOULD increment the major version when making large feature changes. ✅ DO select a version number greater than the highest version number of any other released Track 1 package for the service in any other scope or language. DependenciesDependencies bring in many considerations that are often easily avoided by avoiding the dependency.
✅ DO use the C++ standard library. ✅ DO depend on the Azure Core library for functionality that is common across all client libraries. This library includes APIs for HTTP connectivity, global configuration, and credential handling. ⛔️ DO NOT be dependent on any other packages within the client library distribution package. Dependencies are by-exception and need a thorough vetting through architecture review. This does not apply to build dependencies, which are acceptable and commonly used. ☑️ YOU SHOULD consider copying or linking required code into the client library in order to avoid taking a dependency on another package that could conflict with the ecosystem. Make sure that you are not violating any licensing agreements and consider the maintenance that will be required of the duplicated code. “A little copying is better than a little dependency” (YouTube). ⛔️ DO NOT depend on concrete logging, dependency injection, or configuration technologies (except as implemented in the Azure Core library). The client library will be used in applications that might be using the logging, DI, and configuration technologies of their choice. Documentation Comments✅ DO document every exposed (public or protected) type and member within your library’s code. ✅ DO use doxygen comments for reference documentation. See the documentation guidelines for language-independent guidelines for how to provide good documentation. Repository Guidelines✅ DO locate all source code and README in the [azure/azure-sdk-for-cpp] GitHub repository. ✅ DO follow Azure SDK engineering systems guidelines for working in the [azure/azure-sdk-for-cpp] GitHub repository. ✅ DO format your source code with ✅ DO name all files as lowercase, in a directory of the service short name. Separate words with underscores, and end with the appropriate extension ( ✅ DO place an include file that is not part of the public API in an ✅ DO use characters in the range Filenames should be concise, but convey what role the file plays within the library. ✅ DO use ✔️ YOU MAY have a header file that includes an entire client library. For example, ☑️ YOU SHOULD have headers for smaller components that make sense to be used together. For example, ⛔️ DO NOT substantially change the names exposed by the header in response to macros or other controls. For example, ⛔️ DO NOT declare types which are not a part of the Azure SDK. This requirement precludes Azure headers from including 3rd party headers like Source code layout✅ DO lay out package source code as follows:
☑️ YOU SHOULD align the ✅ DO lay out directories under the
✅ DO base the file name for the service client on the name of the service client. For example, the class declaration for the ☑️ YOU SHOULD have at most one service client declaration per include file. ✅ DO lay out directories under the
✅ DO lay out directories under the
Documentation StyleThere are several documentation deliverables that must be included in or as a companion to your client library. Beyond complete and helpful API documentation within the code itself (docstrings), you need a great README and other supporting documentation.
✅ DO include your service’s content developer in the Architecture Board review for your library. To find the content developer you should work with, check with your team’s Program Manager. ✅ DO follow the Azure SDK Contributors Guide. (MICROSOFT INTERNAL) ✅ DO adhere to the specifications set forth in the Microsoft style guides when you write public-facing documentation. This applies to both long-form documentation like a README and the docstrings in your code. (MICROSOFT INTERNAL) ☑️ YOU SHOULD attempt to document your library into silence. Preempt developers’ usage questions and minimize GitHub issues by clearly explaining your API in the docstrings. Include information on service limits and errors they might hit, and how to avoid and recover from those errors. As you write your code, doc it so you never hear about it again. The less questions you have to answer about your client library, the more time you have to build new features for your service. Docstrings ✅ DO include docstrings compatible with the doxygen tool for generating reference documentation. For example, a (very) simple docstring might look like: ✅ DO use doxygen’s ✅ DO format parameter documentation like the following: For example: ✅ DO document what happens to parameters that are set to null. For example: ✅ DO document which exceptions your function can propagate. Code snippets ✅ DO include example code snippets alongside your library’s code within the repository. The snippets should clearly and succinctly demonstrate the operations most developers need to perform with your library. Include snippets for every common operation, and especially for those that are complex or might otherwise be difficult for new users of your library. At a bare minimum, include snippets for the champion scenarios you’ve identified for the library. ✅ DO build and test your example code snippets using the repository’s continuous integration (CI) to ensure they remain functional. ✅ DO include the example code snippets in your library’s docstrings so they appear in its API reference. If the language and its tools support it, ingest these snippets directly into the API reference from within the docstrings. For example, consider a function called It can be used as follows: When doxygen processes these files, it will see the Use It can be used as follows: Note that automatic links from documentation to examples will only be generated in struct documentation,
not in function documentation. To generate a link from a function’s documentation to an example use ⛔️ DO NOT combine more than one operation in a code snippet unless it’s required for demonstrating the type or member, or it’s in addition to existing snippets that demonstrate atomic operations. For example, a Cosmos DB code snippet should not include both account and container creation operations–create two different snippets, one for account creation, and one for container creation. Combined operations cause unnecessary friction for a library consumer by requiring knowledge of additional operations which might be outside their current focus. It requires them to first understand the tangential code surrounding the operation they’re working on, then carefully extract just the code they need for their task. The developer can no longer simply copy and paste the code snippet into their project. Buildsystem integration ✅ DO Provide a buildsystem target called “docs” to build the documentation ✅ DO Provide an option To provide this you can use the CMake Formatting ✅ DO use cpp-format for formatting your code. Use the .clang-format options. In general, cpp-format will format your code correctly and ensure consistency. However, these are few additional rules to keep in mind. ✅ DO place all conditional or loop statements on one line, or add braces to identify the conditional/looping block. ✅ DO add comments to closing braces. Adding a comment to closing braces can help when you are reading code because you don’t have to find the begin brace to know what is going on. ✅ DO add comments to closing preprocessor directives to make them easier to understand. For example: ⚠️ YOU SHOULD NOT use parens in return statements when it isn’t necessary. ✅ DO place constants on the right of comparisons. For example ✅ DO include a comment for falling through a non-empty ⚠️ YOU SHOULD NOT use README✅ DO have a README.md file in the component root folder. An example of a good ✅ DO optimize the The contributor guide ( Samples✅ DO include runnable examples using ✅ DO use unique example file names within the ✅ DO locate the sample code within the Commonly Overlooked C++ API Design Guidelines⚠️ YOU SHOULD NOT use | ||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |