Copyright (C) 1996 Aladdin Enterprises. All rights reserved. This file is part of Aladdin Ghostscript. Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or distributor accepts any responsibility for the consequences of using it, or for whether it serves any particular purpose or works at all, unless he or she says so in writing. Refer to the Aladdin Ghostscript Free Public License (the "License") for full details. Every copy of Aladdin Ghostscript must include a copy of the License, normally in a plain ASCII text file named PUBLIC. The License grants you the right to copy, modify and redistribute Aladdin Ghostscript, but only under certain conditions described in the License. Among other things, the License requires that the copyright notice and this notice be preserved on all copies. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - This file, c-style.txt, describes Aladdin's C coding guidelines. For an overview of Ghostscript and a list of the documentation files, see README. Generalities ============ All the rules below are meant to produce code that is easy to read. If you find a rule getting in your way or producing ugly-looking results once in a while, it's OK to break it. Indentation ----------- Tab stops are set every 8 columns. However, tabs are not equivalent to logical nesting levels for C code: see below for details. File layout ----------- Every code file should start with comments containing a copyright notice, the name of the file, and a half-to-one-line summary of what the file contains. C code ====== Indentation ----------- Put the first indentation point at the first tab stop; thereafter, each level of logical nesting indents by an additional 4 columns. Proceed as follows: { ... in-line compound statement ... ... (indented +2 columns) } ... construct requiring subordinate code ... ... subordinate simple statement ... (indented +2 columns) ... construct requiring subordinate code ... { ... subordinate code ... ... (indented +4 columns) } Or you can do this if you prefer: { ... in-line compound statement ... } ... construct requiring subordinate code ... ... subordinate simple statement ... ... construct requiring subordinate code ... { ... subordinate code ... } But not this: if ... { ... subordinate code ... } else { ... subordinate code ... } Spaces ------ Do put a space: - after every comma and semicolon, unless it ends a line; - around every binary operator, although you can omit the spaces around the innermost operator in a nested expression if you like; - on both sides of the the parentheses of an if, for, or while. Don't put a space: - at the end of a line; - before a comma or semicolon; - after unary prefix operators; - before the parenthesis of a macro or procedure call. Parentheses ----------- There are just a few places where parentheses are important: - In expressions that mix && and ||, around the inner subexpressions, even if not required by precedence, e.g., (xx && yy) || zz - In expressions that mix &, |, and/or shifts, especially if mixing these with other operators, around the inner subexpressions similarly, e.g., (x << 3) | (y >> 5) - In macro definitions, around every use of an argument that logically could be an expression, e.g., ((x) * (x) + (y) * (y)) Anywhere else, given the choice, use fewer parentheses. As a matter of personal preference, most of the existing Ghostscript code puts parentheses around conditional expressions, even if they aren't syntactically required. Types ----- Use 'private' instead of 'static' for constructs (procedures and variables) declared at the outermost scope. This allows making such constructs either visible or invisible to profilers with a single changed #define. Use const wherever possible and appropriate. If you find yourself wanting to use void *, try to find an alternative using unions or (in the case of super- and subclasses) casts, unless you're writing something like a memory manager that really treats memory as opaque. Use anonymous structures as little as possible. Declare structure types like this (the _t on the type name is preferable but not required): typedef struct xxx_yyy_s { ... members ... } xxx_yyy_t; Names ----- Use fully spelled-out English words in names rather than contractions. This is most important for procedure and macro names, global variables and constants, #defined and enum values, structure and other typedef names, and structure member names, and for argument and variable names which have uninformative types like int. It's not very important for arguments or local variables of distinctive types, or for local index or count variables. Procedures, variables, and structures visible outside a single .c file should generally have a prefix that indicates what subsystem they belong to (in the case of Ghostscript, gs_ or gx_). This rule isn't followed very consistently. Commenting ---------- The most important descriptive comments are ones in header files that describe structures, including invariants. But every procedure or structure declaration, or group of other declarations, should have a comment. Other ----- Format procedures as follows: scope return_type proc_name(type1 arg1, type2 arg2, type3 arg3, type4 verylongargument4, type5 argument5) { ... body ... } Leave a blank line after the declarations of local variables in a procedure or compound statement, unless there's only 1 variable and the scope is less than 10 lines or so. Global variables ---------------- Avoid global variables (non-const data) like the plague. Avoid global const data, but don't knock yourself out over it. Local variables --------------- Avoid assigning new values to procedure parameters. It makes debugging very confusing when the parameter values printed for a procedure are not the ones actually supplied by the caller. If a local variable only gets assigned a value once, assign it that value at its declaration, if convenient. E.g., int x = ; rather than int x; ... x = ; Error handling -------------- By convention, nearly all procedures return an int that indicates the outcome of the call: 0 indicates a normal return, >0 indicates a non-error return other than the normal case, and <0 indicates an error. All callers should check for error returns and, in general, propagate them to *their* caller. PostScript code =============== Put indentation points every 3 spaces. Format procedure definitions like this: /procname % procname { ...code... } bind def