| Prev | Next | Start of Chapter | Next Chapter | Contents | Glossary | Index | Comments | (7 out of 7)

Constructing an API

A common reason for using procedural processing in a G2 application is to create an application programmer's interface (API). An API provide programmatic access to all the public capabilities of a module or set of modules in an application. Typically, you create an API for reusable modules in a large-scale application and domain-specific toolkits.

For information on how to construct modules that support APIs, see Designing Reusable Modules.a

Determining the Scope of the API

An API consists of a set of public, extensible methods on public classes and a set of public, non-extensible procedures and functions. The API should provide programmatic access to all aspects of the functionality of the module, for example:


Note: You should consider native G2 expressions part of the API. For example, you do not need to create a separate API procedure to get the value of a public attribute because the value is available through native G2 syntax.

The API should provide programmatic access to every feature available through the user interface. In particular, you should follow this rule of thumb:

Adhering to this guideline is critical for testability of the module. If you build the module according to the four-layer architecture, as described in Designing Reusable Modules, this will automatically be the case.

The API should include complete documentation for the signature of each API method, procedure, and function. The signatures should not be subject to change.

The modules GFR, GUIDE/UIL, GMS, and GXL provide examples of well constructed APIs.

For more information on the APIs for GFR, GUIDE/UIL, GMS, and GXL, see Chapter 9, Building a User Interface.

Validating Arguments and Signalling Errors

Every API procedure must validate its arguments. Validation consists of two parts:

If any argument is not successfully validated, the procedure should signal an error. You should consider the errors signalled by an API procedure to be an essential part of the procedure signature, which you must document and which is not subject to change.

When you signal an error, you should provide a language-appropriate error message, using the GFR localization facility. You access the language via the client object by using the function gfr-language.

See the G2 Foundation Resources User's Guide for details on localization and error signalling.

Guidelines for Creating an API

Follow these guidelines when constructing your API.

Include the Client as an Argument

G2 is a client/server deployment environment, which supports multiple clients. To allow communications to reach the desired client, all API procedures should require as an argument the appropriate client object, such as an instance of the G2-WINDOW class. If the client is a G2 window, you can use the client argument to access the user mode, language, and other properties of the window. You pass the window or client along the thread of processing so it is available to other API procedures.

If you do not include a client as an explicit argument, the API must provide a way to determine the client object. For example, an object might pass another object that is related through a public relation to the client.

By convention, you should pass the client object as the last argument to the API procedure. For example, this G2 system procedure passes the window as an argument to determine on which window the workspace should be scaled:

When you begin a thread of processing from a user gesture, such as clicking a button or choosing a user menu choice, you should always pass the window associated with the gesture to the target procedure. For example, this action for a user menu choice performs an action on an item in current window:

When no natural client exists for an action, such as when a rule starts a thread of processing, use the G2 window object GFR-DEFAULT-WINDOW as the client. For example, you can use GFR-DEFAULT-WINDOW in communications handlers to indicate that communications should be routed through default output channels. The GFR-DEFAULT-WINDOW is always associated with the default language.

For more information on GFR-DEFAULT-WINDOW, see the G2 Foundation Resources User's Guide.

Maintain Atomicity in API Procedures

Once an API procedure validates its arguments, as described in Validating Arguments and Signalling Errors, no further error checking is required as long as the single processing thread is continued without interruption. When procedures in the core maintain atomicity, they can run at optimal efficiency without the need for existence and argument checks.

However, when procedures do not maintain atomicity, it is critical that the API procedure perform validation of its arguments immediately after the single processing thread has been interrupted.

Here are the general guidelines for maintaining atomicity by properly using procedure statements that interrupt the single thread of execution:

For more information, see the references in this table:

For information on... See...
Shadowing API procedures
Shadow Public Procedures with Private Procedures
Procedural statements that interrupt the thread of processing and disrupt atomicity
Procedural Statements that Implement Multi-Threaded Processing and/or Introduce Wait States

Shadow Public Procedures with Private Procedures

In general, it is a good idea to shadow each API procedure with a similar private procedure whose name begins with an underscore. The API procedure simply validates its arguments and calls the shadow procedure. The shadow procedure performs the actual functionality of the procedure. That way, core procedures do not have to call API functions to access functionality supported by the API.

For example, gxl-get-cell-contents is the API function to get the value stored in a cell of a GXL spreadsheet. This procedure signals an error if row or column are out of bounds for the given spreadsheet. Otherwise, it calls the private procedure _gxl-get-cell-contents. The private procedure performs no further validation on the spreadsheet. Other core procedures call _gxl-get-cell-contents because validation is not necessary.

For more information on naming conventions for public and private knowledge, see Using Public and Private Naming Conventions.

Limit the Use of On Error Statements

Reserve on error statements for the following circumstances only:

Recovery from an error can involve:

| Prev | Next | Start of Chapter | Next Chapter | Contents | Glossary | Index | Comments | (7 out of 7)

Copyright © 1997 Gensym Corporation, Inc.