PostGIS
Back to homepage

Coding Style Guidelines

This page covers source formatting, comments, Doxygen comments, and naming conventions. For broader developer workflow notes, including release policy, SQL API upgrade rules, and the Availability:, Changed:, and Replaces comments parsed by the upgrade script generator, see PostGIS Development Guide.

Preamble

PostGIS was created over many years, by many different people, some in a hurry. As a result, the existing coding style is all over the map. We recognize this, and understand it will be the work of many years before PostGIS has a consistently named internal set of functions, but, we can dream.

If new functions follow this guideline, if we do a little renovation work from time to time, we will eventually get there.

Documentation Voice

Write contributor documentation in plain, direct English. Prefer short sentences, concrete commands, and links to the maintained page for deeper context. Explain the purpose of a step before listing exceptions.

Avoid preserving old wiki phrasing when it hides the current source of truth. When converting older notes, keep the useful idea and replace stale paths, release numbers, chat networks, or one-off examples with current repository links.

Formatting

Most C code should use an ANSI standard formatting with tabs for block indenting. When not block indenting, use spaces. To convert a file to the standard format use:

astyle –style=ansi –indent=tab

Some files use space indenting instead (check .editorconfig for info). For them, you can use:

astyle –style=ansi –indent=spaces=2

Do not get too happy with this command. If you want to re-format a file you are working on:

a) run astyle on it b) commit c) do your work d) run astyle again e) commit

The idea is to avoid combining style-only commits and commits that change logic, so the logic commits are easier to read.

Before submitting patches that modify C or C++ files, run clang-format only on the relevant hunks to avoid unrelated churn:

git clang-format

The command formats staged changes by default. Use git clang-format <commit> to reflow a specific range, or pass --extensions c,cpp,h when touching files with non-standard suffixes. Validate that the resulting diff stays focused on the code you touched.

Commits that only change layout are listed in .git-blame-ignore-revs, so git blame attributes lines to the change that last altered their content. Enable it once per clone:

git config blame.ignoreRevsFile .git-blame-ignore-revs

Keep style-only commits separate from logic changes and add their full SHA to .git-blame-ignore-revs on master and every stable branch; the file is kept identical on all of them.

Declare loop counters in the for statement when they are not needed after the loop.

Types and return values in new and changed code:

  • A yes/no answer is bool with true/false and nothing else: not int, char, LW_TRUE/LW_FALSE or GDAL’s TRUE/FALSE, and no third value such as -1 for an error.
  • Errors are raised with lwerror() or rterror(), not returned: inside PostgreSQL neither comes back. A function returns only its answer, in its natural type. After raising, it returns the safest answer for a caller that does continue (unit tests, raster2pgsql): false, zero, empty, NULL, “outside”, never a value that means something else. State in the function’s documentation what it returns after an error.
  • Call sites do not compare a result with -1, LW_TRUE or LW_FALSE. When a fix makes every caller longer, it belongs inside the function.
  • An out parameter is for a real second result, or for a failure that is an ordinary outcome the caller acts on, such as parsing input or opening a file: then return bool, or a pointer that is NULL.
  • true means it worked. A variable that holds such a result is named for success (success, found, valid): never bool err, bool failed or a status where zero is the good value.
  • Raster functions that return rt_errorstate are compared by name (!= ES_NONE) and never tested as a truth value: there zero means success.
  • A size in bytes is size_t. PostgreSQL stores and allocates at most 1 GB - 1 byte: compute sizes with overflow-checked arithmetic and check that limit before allocating or setting a varlena size. Do not narrow a size_t without a range check.
  • A count, an index and a loop counter have the type of what they count or of their bound: uint32_t for points, rings and geometries (print with %u), uint16_t for raster dimensions and bands. A signed value from SQL, GDAL or GEOS is checked for being non-negative once, where it enters.
  • Spell fixed-width types as in <stdint.h> (int64_t, not int64); avoid long and bare unsigned. uint8_t is for bytes and flags.

Prefer !ptr to ptr == NULL and if (ptr) to if (ptr != NULL) in new and reformatted code, including code produced by formatting tools or AI assistants. Both forms remain acceptable in contributions; review does not ask contributors to rewrite one into the other.

Macros should be ALL_UPPERCASE. Enumerations should be ALL_UPPERCASE.

Comments should be written in C style (/* …. */) and not C++ style (//)

When code was rewritten because the obvious way was wrong, say so where the code is: name the alternative that was tried or looks simpler, and why it is not used. “A list of handler names would miss /vsizip/{/vsicurl/...}, so any second /vsi counts” saves the next reader from proposing the list again. The commit message is not enough: it is not in front of whoever edits the line next.

When describing a function, the description should be right above the function and should start with /** This is so the function description can be picked up by the doxygen autodocumentor. For example

/**
 * Does something funny
 */
double funny_function(POINT2D *p1, POINT2D *p2, POINT2D *q){
	funny stuff here;
}

More advanced commenting

/**
 * Does something funny
 *
 * @param p1       : first point
 * @param p2     : second point
 * @param q   : the quiet point
 *
 * @return a funny double number
 */
double funny_function(POINT2D *p1, POINT2D *p2, POINT2D *q){
	funny stuff here;
}

Naming

For ./liblwgeom:

  • Files should be named with an lw prefix.
  • Functions should have an lw prefix (lw_get_point).
  • Function names should use underscores, not camel case.
  • Function names should indicate the geometry type of inputs if relevant (lwline_split)

For ./postgis:

  • C functions called by the back-end directly (function(PG_FUNCTION_ARGS)) should be named to match their most likely SQL alias. So the SQL ST_Distance(geometry) maps to the C function ST_Distance(PG_FUNCTION_ARG)
  • C utility functions should be prefixed with pgis_ (lower case)