Skip to content

Coding Conventions

Filip Gawin edited this page May 14, 2018 · 16 revisions

This document outlines the coding convention that should be used when writing new code for OpenRW.

Git

Updating branch

Sometimes when your pull is waiting for review, other ones can be merged. You will have to update your branch if merge conflicts occur. You can fix that by rebasing, which doesn't create unnecessary merge commits.

Steps:

  • git remote add upstream https://github.com/rwengine/openrw

  • git fetch upstream

  • git rebase upstream/master

If there's a conflict, you should also do:

  • Resolve conflict
  • git add -A
  • git rebase --continue

C++ Style

The Google C++ style should be used for C++ code, with the following exceptions:

  • Exceptions may be used for fatal situations that cannot be handled in any reasonable way
    • e.g. the ScriptMachine throws when it encounters an instruction that isn't a valid opcode
  • Indentation should be 4 spaces, as a good medium between horizontal space and clarity.

Memory Management

Dynamic allocations for large numbers of objects is discouraged. Large pools of objects should share allocations (however, this is not implemented yet).

Dynamically allocated objects should be owned by a smart pointer, with a preference for std::unique_ptr. If it's not possible to declare a single ownership, then std::shared_ptr should be used.

Asserts & Debugging

These are macros defined in rw/defines.hpp that should be used to make the intended behaviour of code more clear:

  • use RW_MESSAGE for debug messages that won't apppear in release
  • use RW_UNIMPLEMENTED for noting that a particular feature is not yet implemented
  • use RW_CHECK for non-fatal assertions

CMake Style

  • Use targets and properties over variables where possible.
  • use CAPITAL_LETTERS for variables
    • e.g. set(FOO_PATH "/tmp/foo")
  • use lower_case for functions
    • e.g. add_executable(${EXECUTABLE_NAME} ${EXECUTABLE_SOURCES})
  • use capitals for conditions
    • e.g. if(SOME_VAR STREQUAL "FOO")
  • leave closing conditional statements empty

Example

set(SOME_VAR FALSE)
if(NOT SOME_VAR)
  message("SOME_VAR false")
else()
  message("SOME_VAR not false")
endif()
Clone this wiki locally