2011-07-11 16:57:22 +00:00
|
|
|
Ceph Coding style
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
Coding style is most important for new code and (to a lesser extent)
|
|
|
|
revised code. It is not worth the churn to simply reformat old code.
|
|
|
|
|
|
|
|
C code
|
|
|
|
------
|
|
|
|
|
|
|
|
For C code, we conform by the Linux kernel coding standards:
|
|
|
|
|
2015-11-30 13:12:37 +00:00
|
|
|
https://www.kernel.org/doc/Documentation/CodingStyle
|
2011-07-11 16:57:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
C++ code
|
|
|
|
--------
|
|
|
|
|
|
|
|
For C++ code, things are a bit more complex. As a baseline, we use Google's
|
|
|
|
coding guide:
|
|
|
|
|
2015-11-30 13:12:37 +00:00
|
|
|
https://google.github.io/styleguide/cppguide.html
|
2011-07-11 16:57:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
As an addendum to the above, we add the following guidelines, organized
|
|
|
|
by section.
|
|
|
|
|
|
|
|
* Naming > Type Names:
|
|
|
|
|
|
|
|
Google uses CamelCaps for all type names. We use two naming schemes:
|
|
|
|
|
2018-05-23 02:40:11 +00:00
|
|
|
- for naked structs (simple data containers), lower case with _t.
|
|
|
|
Yes, _t also means typedef. It's perhaps not ideal.
|
2011-07-14 17:49:33 +00:00
|
|
|
|
2018-05-23 02:40:11 +00:00
|
|
|
struct my_type_t {
|
2018-05-24 19:03:49 +00:00
|
|
|
int a = 0, b = 0;
|
|
|
|
void encode(...) ...
|
|
|
|
...
|
2011-07-11 16:57:22 +00:00
|
|
|
};
|
2011-07-14 17:49:33 +00:00
|
|
|
|
|
|
|
- for full-blown classes, CamelCaps, private: section, accessors,
|
|
|
|
probably not copyable, etc.
|
2011-07-11 16:57:22 +00:00
|
|
|
|
|
|
|
* Naming > Variable Names:
|
|
|
|
|
2011-07-14 17:49:33 +00:00
|
|
|
Google uses _ suffix for class members. That's ugly. We'll use
|
2018-05-23 02:40:11 +00:00
|
|
|
a m_ prefix, like so, or none at all.
|
2011-07-11 16:57:22 +00:00
|
|
|
|
2011-07-14 17:49:33 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
int get_foo() const { return m_foo; }
|
|
|
|
void set_foo(int foo) { m_foo = foo; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_foo;
|
|
|
|
};
|
|
|
|
|
2011-07-11 16:57:22 +00:00
|
|
|
* Naming > Constant Names:
|
|
|
|
|
2011-07-13 18:25:22 +00:00
|
|
|
Google uses kSomeThing for constants. We prefer SOME_THING.
|
2011-07-11 16:57:22 +00:00
|
|
|
|
|
|
|
* Naming > Function Names:
|
|
|
|
|
|
|
|
Google uses CamelCaps. We use_function_names_with_underscores().
|
|
|
|
|
|
|
|
Accessors are the same, {get,set}_field().
|
|
|
|
|
|
|
|
* Naming > Enumerator Names:
|
|
|
|
|
|
|
|
Name them like constants, as above (SOME_THING).
|
|
|
|
|
|
|
|
* Comments > File Comments:
|
|
|
|
|
2011-07-14 17:50:08 +00:00
|
|
|
Don't sweat it, unless the license varies from that of the project
|
2018-03-13 21:07:50 +00:00
|
|
|
(LGPL2.1) or the code origin isn't reflected by the git history.
|
2011-07-11 16:57:22 +00:00
|
|
|
|
2012-08-31 22:18:53 +00:00
|
|
|
* Formatting > Tabs:
|
|
|
|
Indent width is two spaces. When runs of 8 spaces can be compressed
|
|
|
|
to a single tab character, do so. The standard Emacs/Vim settings
|
|
|
|
header is:
|
|
|
|
|
|
|
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
|
|
|
|
// vim: ts=8 sw=2 smarttab
|
|
|
|
|
2011-07-11 16:57:22 +00:00
|
|
|
* Formatting > Conditionals:
|
|
|
|
|
|
|
|
- No spaces inside conditionals please, e.g.
|
|
|
|
|
|
|
|
if (foo) { // okay
|
|
|
|
|
2011-07-13 18:25:22 +00:00
|
|
|
if ( foo ) { // no
|
2011-07-11 16:57:22 +00:00
|
|
|
|
2018-05-23 02:40:11 +00:00
|
|
|
- Always use newline following if, and use braces:
|
2011-07-14 17:49:33 +00:00
|
|
|
|
|
|
|
if (foo) {
|
2018-05-23 02:40:11 +00:00
|
|
|
bar; // like this, even for a one-liner
|
2011-07-14 17:49:33 +00:00
|
|
|
}
|
2011-07-13 18:25:22 +00:00
|
|
|
|
2018-05-23 02:40:11 +00:00
|
|
|
if (foo)
|
|
|
|
bar; // no, usually harder to parse visually
|
|
|
|
|
|
|
|
if (foo) bar; // no
|
|
|
|
|
|
|
|
if (foo) { bar; } // definitely no
|
2011-07-11 16:57:22 +00:00
|
|
|
|
2018-05-23 11:42:09 +00:00
|
|
|
* Header Files -> The `#define` Guard:
|
2011-07-11 16:57:22 +00:00
|
|
|
|
2018-05-23 11:42:09 +00:00
|
|
|
`#pragma once` is allowed for simplicity at the expense of
|
|
|
|
portability sinces `#pragma once` is wildly supported and is known
|
|
|
|
to work on GCC and Clang.
|
2011-07-11 16:57:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
The following guidelines have not been followed in the legacy code,
|
|
|
|
but are worth mentioning and should be followed strictly for new code:
|
|
|
|
|
|
|
|
* Header Files > Function Parameter Ordering:
|
|
|
|
|
|
|
|
Inputs, then outputs.
|
|
|
|
|
|
|
|
* Classes > Explicit Constructors:
|
|
|
|
|
|
|
|
You should normally mark constructors explicit to avoid getting silent
|
|
|
|
type conversions.
|
|
|
|
|
|
|
|
* Classes > Copy Constructors:
|
|
|
|
|
|
|
|
- Use defaults for basic struct-style data objects.
|
|
|
|
|
|
|
|
- Most other classes should DISALLOW_COPY_AND_ASSIGN.
|
|
|
|
|
|
|
|
- In rare cases we can define a proper copy constructor and operator=.
|
|
|
|
|
|
|
|
* Other C++ Features > Reference Arguments:
|
|
|
|
|
|
|
|
Only use const references. Use pointers for output arguments.
|
|
|
|
|
|
|
|
* Other C++ Features > Avoid Default Arguments:
|
|
|
|
|
|
|
|
They obscure the interface.
|
2018-01-30 15:19:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
Python code
|
|
|
|
-----------
|
|
|
|
|
|
|
|
For new python code, PEP-8 should be observed:
|
|
|
|
|
|
|
|
https://www.python.org/dev/peps/pep-0008/
|
|
|
|
|
|
|
|
Existing code can be refactored to adhere to PEP-8, and cleanups are welcome.
|
|
|
|
|
|
|
|
|
|
|
|
JavaScript / TypeScript
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
For Angular code, we follow the official Angular style guide:
|
|
|
|
|
|
|
|
https://angular.io/guide/styleguide
|
|
|
|
|
|
|
|
To check whether your code is conformant with the style guide, we suggest
|
|
|
|
using TSLint with Codelyzer:
|
|
|
|
|
|
|
|
https://www.npmjs.com/package/codelyzer
|
|
|
|
|