Style Guide for Writing Sample Programs

We produce sample programs for all Open Market APIs. To improve the look and consistency of sample programs across all our products, we'd like everyone to follow certain coding guidelines.

We produce sample code in five different languages:

 

 

 


 

C Programming Language

We provide sample C programs for the following APIs:

This section describes the style guidelines for coding sample C applications.

 

General Rules
The start of every sample C program
    /************************************************************* * program_name.c * * Description of what the program does. * (Could be many lines.) * * Copyright (c) [year] Open Market, Inc. * All rights reserved. *************************************************************/
Writing block comments
    /********************************** * Like this. * Can easily add more text. **********************************/
Writing inline comments
    /* like this */
    
Naming your programs
  • Should start with lowercase letter
  • Should start with verb
  • Use underscores instead of white space

For example:

    create_digital_offer.c   /* good program name */
    CreateDigitalOffer.c     /* bad program name */
    digital_offer.c          /* bad program name */ 
    
Naming your functions
  • Should start with capital letter
  • Should start with verb
  • Should be mixed case, without underscores
  • Functions that initialize (allocate memory, for example, in BPAPI applications) should typically be named Init.
  • Functions that clean up (deallocate memory that had been allocated by Init should typically be named CleanUp.

For example:

    PrepareDatabase();   /* good function name */
    prepare_database();  /* bad function name */
    database();          /* bad function name */ 
    
Naming your variables
  • Should be in lowercase (to distinguish from function names and constants).
  • Should use underscores to connect variable names that contain multiple words.
  • Should be highly descriptive.

For example:

    transaction_server   /* good variable name */
    transactionserver    /* bad variable name (needs underscore) */
    ts                   /* bad variable name (too ambiguous) */ 
    
What the main() function should do
  • Should be as short as possible
  • Should primarily consist of function calls
How to specify exit codes Sample code should contain #define'd exit codes.
0 is reserved for successful error-free exit.

Example:
#define SUCCESS 0
#define FILE_NOT_FOUND  1
#define COULD_NOT_CONNECT_WITH_TRANSACT 2
Using the preprocessor directives #debug and #verbose Should not be in production sample code but may be in courseware code.
What to name the variable that holds a function's return value. return_code
What to name variables in BPAPI programs
Data Structure Variable Name
OMTX_Handletransact_handle
OMTX_MUBuyerProfilebuyer_profile
OMTX_MUCustomFieldcustom_field (not CustomFields)
OMTX_MUHandlemu_handle
OMTX_MUUserDetailsuser_details

An Example C Function

Here is a sample C function. An explanation follows.

Line(s) What To Do On This Line
1 Put a blank line between functions.
2-5 Describe the function.
6 Specify the function's return type on a separate line.
7 Specify the function's name and its parameters. If the function has only one parameter, specify the function name and parameter on the same line. If the function has multiple parameters, specify one parameter per line.
8 Place the opening brace { flush left just after the last function parameter.
9-11 Specify all local function variables starting at column 1.
17, 18, 22, 27, 30 Outer level code should start at column 3.
19-21, 28-29 Embedded code should start at column 5.
18, 27 The starting brace should start at the end of a line (not on its own line).
22, 30 The closing brace should be aligned with the outer level code (for example, with for and if.

 

Word Usage in Text

When describing C programs in your programmer guides, follow these guidelines:

 

 

 

 

 


 

C++ Coding Rules

(The doc department modified (slightly) engineering's guidelines for C++.)

We provide sample C++ applications for all Transact APIs, including:

If you are writing fresh C++ examples or modifying some existing ones, then we'd like you to follow these coding guidelines.

We code two different categories of C++ programs:

NonCOM Applications

First off, we (the doc department) aren't writing classes and header files. Rather, we are demonstrating how to instantiate classes that are part of the APIs. Therefore, our sample C++ applications look a lot like sample C applications; that is, they contain a main() function and possibly a bunch of secondary functions called from main(). In fact, because context variables don't work too well as function parameters, our main() functions have a tendency to be quite long.

Most C++ applications have roughly the same start, which is:

  1. Collecting credential information (their user name and password) from the user.

  2. Invoking TTransactAPIService2.Login() to test the credentials.

  3. Creating at least one context.
  4. Validating the context(s).

Copy the existing starting code for your API or a related API.

Secondary Functions

Most nonCOM applications require two secondary functions:

Paste the following code into the top of every nonCOM application:

Paste the following code for OutputLatin1 into your application: