3.7 KiB
Coding Style
The Lean project is moving away from using any C++ as more and more of the compiler is being bootstrapped in Lean itself. But the remaining C++ codebase is using modified version of Google's C++ Style Guide.
C++11 features
Lean makes extensive use of new features in the C++ 11 standard. Developers must be familiar with the standard to be able to understand the code. Here are some of the features that are extensively used.
- Type inference (aka
autokeyword). - Initializer lists.
- Lambda functions and expressions.
nullptrconstant.- Strongly typed enumerations.
- Right angle brackets with no space is now allowed in C++ 11.
- Thread local storage.
- Threading facilities.
- Tuple types.
- Smart pointers.
- When using
std::listmake sure to include thestd::qualifier so you do not accidentally use thelean::listtype. - When using
std::copymake sure to include thestd::qualifier so you do not accidentally use thelean::copytype. - Small and focused functions are preferred: foo(). Try not to exceed 500 lines in a function, except in tests.
- Do not use the
#ifndef-#define-#endifidiom for header files. Instead use#pragma once. - Write
type const & vinstead ofconst type & v. - Use
constextensively. - Use the macro
lean_assertfor assertions. The macrolean_assertis extensively used when writing unit tests.
Naming
-
Class, method, and function names are lower case Use
_for composite names. Example:type_checker. -
Class/struct fields should start with the prefix
m_.Example:
class point { int m_x; int m_y; public: ... };
Namespaces
All code is in the lean namespace. Each frontend is stored in a
separate nested namespace. For example, the SMT 2.0 frontend is stored
in the lean::smt namespace.
Exception: some debugging functions are stored outside of the lean
namespace. These functions are called print and are meant to be used
when debugging Lean using gdb.
Do not use using namespace in a header file.
Templates
Organize template source code using the approach described at http://www.codeproject.com/Articles/3515/How-To-Organize-Template-Source-Code
Idioms
Use some popular C++ idioms:
Formatting
-
Use 4 spaces for indentation.
-
if-then-elsecurly brackets not always required. The following forms are acceptable:if (cond) { ... } else { ... }and
if (cond) statement1; else statement2;In exceptional cases, we also use
if (cond) statement;and
if (cond) statement1; else stament2;if-then-else-if-else
The following forms are acceptable:
if (cond) { ... } else if (cond) { ... } else { ... } ```c++ and ```c++ if (cond) statement1; else if (cond) statement2; else statement3; -
Format code using extra spaces to make code more readable. For example:
environment const & m_env; cache m_cache; normalizer m_normalizer; volatile bool m_interrupted;instead of:
environment const & m_env; cache m_cache; normalizer m_normalizer; volatile bool m_interrupted; -
Spaces in expressions. Write
a == binstead ofa==b. Similarly, we writex < y + 1instead ofx<y+1.