icon: book order: 0
For class names, always use PascalCase
// Correct
class FooBar {};
// Wrong
class fooBar {};
class foo_bar {};
For function and methods, use snake_case
// Correct
int test_func(int x) { return x+1; }
// Wrong
int TestFunc(int x) { return x+1; }
int testFunc(int x) { return x+1; }
For special python objects, use the same name as in python.
auto x = vm->None;
vm->SyntaxError(...);
vm->TypeError(...);
vm->call(obj, __repr__);
For global constants, use k prefix with PascalCase
const int kMaxCount = 10;
const float kMinValue = 1.0;
For macros, use SNAKE_CASE
#define FOO_BAR 1
#define TEST(x) x+1
Please use python style access control.
We do not recommend to use C++ keywords such as private or public to achieve access control. Also do not write any trivial setter/getter.
Use a single _ as prefix to indicate a function or variable is for internal use.
class FooBar {
public:
int _count;
int inc() { _count+=1; }
void _clear() { _count=0; }
}
_ prefix is just a warning to remind you to use such members carefully.
It does not forbid users to access internal members.
Try to make the code compact if it does not affect readability.
// Correct
if(x == 1) break;
// Wrong
if(x == 1){
break;
}
shared_ptr<T>Use a _ suffix to indicate a type is a shared pointer.
typedef shared_ptr<CodeObject> CodeObject_;
CodeObject_ co = make_shared<CodeObject>();