Common Rules
The following style guidelines will be followed for both Slice and C++ code:
C1. Braces for compound statements will be on their own lines, at the same indentation level as the statement they are part of. Code and declarations in the brace-enclosed block will be indented one level from the braces. The only exception to this rule is for C++ namespace and Slice module blocks; because the nesting of these scopes could change in the future, if indentation was used it would have to be changed across many source files if the nesting changed. In addition, most code is contained in at least three namespaces/modules, if not more, and indenting the code to represent this would waste a great deal of space and make the code harder to read.
C2. Indentation: 4 spaces per level, no tabs.
C3. Interface, class and structure names will be in UpperCamelCase style.
C4. Function, operation and member variable names will follow lowerCamelCase style.
C5. Comments will follow Doxygen (Javadoc form) guidelines. For Slice this will apply to interface, class and struct definitions, and all operations. For C++, this will apply to classes and operations. All other comments will follow C++ standard convention, which is for "//" comments to be used except for lengthy multi-line comments.
C6. Comment the end of namespace scopes (or any code blocks long enough that would likely not be entirely visible on a typical screen) to improve maintainability / readability.
C7. All comments shall be in the English language.
Slice Style Guide
The following additional style guidelines will be followed in Asterisk SCF Slice definitions:
- Empty definitions will be shown by opening and closing braces at the end of the line.
- Slice file names will end with "If.ice" (where If is for interface) so that developers in IDEs will be able to tell when they were produced generated code. (The Slice translators use the .ice filename as the basis for the generated C++ and C# filenames.)
- #pragma once will be used for multiple-include protection. The Slice translators use the MCPP library for C-style preprocessing, which supports #pragma once. It provides a performance benefit over traditional #ifdef-based include guards in addition to being easier to read and maintain.
C++ Style Guide
To ensure compatibility with GCC, all C++ source and header files must end with a newline.
- In pointer and reference declarations, the & and * modifiers should be next to the type, not the variable (Foo& foo)
- When declaring variables, modifiers (such as const and auto) should be to written on the left side of the type (const Foo& foo).
- Single argument class and struct constructors should always be marked explicit, to keep the compiler from using one in an unexpected way that keeps an error from being detected at compile time.
C++ Header Files
The following additional style guidelines will be followed in C++ header files:
- #pragma once will be used for multiple-include protection. #pragma once has been supported in GCC since version 3.4.0, and in Visual Studio since (at least) version 6. It provides a performance benefit over traditional #ifdef-based include guards in addition to being easier to read and maintain.
- Forward declarations should be used when the code does not need the entire type declaration; the most common cases of this are pointer and reference variables, which only need the type name declared, but not its complete declaration.
- Header files should #include all other headers required to compile them; source files that #include a header file should not be required to know which other headers are required for it. Organize #include
- Avoid using namespace;
- Use lower-case "m" prefix (member) for member variables, to avoid hiding member variables with local variables in methods. This is not done for Slice class member variables, as Slice class members are always public, since Slice classes are more analogous to C++ struct than C++ class.
C++ Source Files
The following additional style guidelines will be followed in C++ source (.cpp) files:
- Organize #include
- Flatten namespaces (if desired for convenience) here in the source file, not in header files.
- Variables should be initialized using constructor syntax, not assignment syntax, even for Plain-Old-Data types. This is for readability and consistency, not for performance, since modern compilers will compile them to the same object code in nearly all cases. Instead of:
int x = 1;
string str = "This is a string.";
ProxyPtr proxy = 0;
use:
int x(1);
string str("This is a string.");
ProxyPtr proxy(0);
- Write function calls with no whitespace between the function name and the opening parenthesis, or around the parentheses. The only whitespace should be following any commas that separate arguments, like this:
doOperation("xyz", 2, fooPtr); - Don't format statements (for, if, switch, while, etc.) as if they were function calls; a single space should appear after the keyword, like this:
if ((x + 1) > 100)
{
return y;
}
for (x = 0; x++; x < z)
{
y *= 3;
}
- Compound statements must always be enclosed in braces, even if the block contains only one statement. This rule helps to avoid unexpected execution paths when code is modified for debugging purposes (or any other reason). Instead of this:
if (x > 5)
return y;
while (z > 4)
z /= 2;
use this:
if (x > 5)
{
return y;
}
while (z > 4)
{
z /= 2;
}
Editor setup
Emacs
Save a copy of the attached file asterisk-scf-style.el into your site-lisp directory. Make sure you have the following in your .emacs
(require 'asterisk-scf-style)
;; Treat .h files as C++ files
(add-to-list 'auto-mode-alist '("//.h//'" . c++-mode))
;; You may want to merge this with existing configuration in your custom-set-variables block
(setq indent-tabs-mode nil
require-final-newline t
show-trailing-whitespace t
c-default-style '((c-mode . "asterisk-scf") (c++-mode . "asterisk-scf"))
)