# OGRE Coding Standards

This document describes the coding standards all developers are expected to adhere to when writing code for the OGRE project.

## Top-level organisation issues

- All source files must begin with the standard OGRE copyright statement:    
// This file is part of the OGRE project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at https://www.ogre3d.org/licensing.
// SPDX-License-Identifier: MIT

- All publicly visible classes should be declared in their own header file using the .h extension, placed in the `include` folder of the sub-project in question, and named after the class but prefixed with `Ogre` e.g. `OgreMyClass.h`. Only very tightly related classes should be declared in the same header file. 
    
- Headers of private classes must be placed in the `src` folder of the sub-project in question
    
- Implementations should be placed in a source file called the same name as the class but with an extension of .cpp.
    
- Everything must be declared inside the namespace `Ogre`.

## Portablity

- All code must be cross platform, ANSI C++. No dependencies on platform-specific headers and / or types are allowed (the only exception is when dealing with platform-specific features like windowing, which must be implemented for each platform separately).
    
- If you serialise / deserialise any data, subclass from Serializer and use its methods, it will insulate you from endianness issues. If you need to serialise any types it doesn't already handle, make sure you deal with endianness issues in the same way Serializer does (ie use native endianness by default but detect the inverse on reading and perform flipping if required).

## C++ Standards compliance

- Always prefer the STL over custom containers / algorithms.
    
- Always prefer C++ techniques over C.

- Avoid C-strings (`char*` and functions like sprintf, strcpy, use `Ogre::String`)
            
- Avoid old I/O routines (fopen et al, use `<iostream>`)
            
- Use abstract classes or templates not `void*`
            
- Use overloaded methods not varargs.

- Minimum C++ compiler level is MSVC 14 (VS2015) or gcc 5. Compilers which do not support C++14 properly are not supported.
    
- Use the PImpl idiom (https://en.cppreference.com/w/cpp/language/pimpl) to reduce dependencies between classes.
    
- Always use const-correctness (https://isocpp.org/wiki/faq/const-correctness). Methods taking non-primitive types as parameters should generally take them as const references, methods returning non-primitive types should generally return them as const references. Declare all methods that do not modify internal state `const`. For lazy-update getter methods, declare the internal state which is lazy-updated `mutable`.
    
- Prefer `private` over `protected` to encourage encapsulation. Use public interfaces internally too.
    
- Always declare destructors `virtual` unless the class you are writing should not have any vtable (no other virtual methods).
    
- Avoid non-const by-ref parameters unless you have no other option. We prefer not to have inout parameters since they are less intuitive.

## Naming conventions & Documentation

- Classes, types and structures must be title case (MyNewClass). 
    
- Methods and local variables must be camel case (myNewMethod). 
    
- Member variables should be prefixed with `m` (mInstanceVar), static member variables should be prefixed `ms` (msStaticMemberVar). Do not use any other prefixing such as Hungarian notation.
    
- Preprocessor macros must be all upper case and prefixed with OGRE_
    
- Enums should be named in title case, enum values should be all upper case
    
- All classes and methods must be fully documented in English using Doxygen-compatible comments. Use the `@param` and `@return` directives to define inputs and outputs clearly, and `@note` to indicate points of interest.
    
- Use verbose, descriptive names for classes, methods, variables - everything except trival counters. Code should be self-describing, don't be obtuse.

## Style issues

- Insert a newline before an open brace (contentious I know!)
    
- Use typedefs to declare template-based types that you use to avoid ugliness e.g. typedef std::list<MyType*> MyTypeList;
    
- Always insert spaces in between operators and operands (x + y, not x+y)
    
- Use parenthesis to make the operator precedence unambiguous, even when it is not required ((x * y) + 1, not x * y + 1)

## Error handling

- Fatal errors should always be dealt with though exception handling. No return-value error reporting.
    
- Whenever you make an assumption in your code, verify it with an assert.

* By default use `OgreAssert`. This will generate an exception - even in release builds. 
* For performance critical code resort to `OgreAssertDbg` that only fires in debug builds.
* In public header files use the C `assert` macro to follow user-defined compilation flags.
* Prefer `OGRE_EXCEPT` for runtime-conditions outside of developer control e.g. minimal required OpenGL Version. This allows building Ogre with no assertion for release.

## Design issues

- Use existing design patterns and identify them by their well known names. A good starting reference is the "Gang of Four" book.
    
- Use strong encapsulation. Top-level interfaces should hide implementations and not require the user of the library to understand internals. Avoid public attributes except in structs.
    
- Don't use `friend` if you can avoid it. Where classes need to collaborate on an internal implementation, prefix the methods they use to communicate with `_` (this is our demarcation for "recommended for internal use only"). This can also be used to expose advanced functionality only intended for very skilled users.

## Final words

If in doubt, do as the existing code does!
