代码风格指南¶
本文档提供了GraphScope代码库的编码风格指南,涵盖C++、Python、Rust、Java和shell脚本。 在整个代码库中遵循一致的编码标准有助于提高可维护性、可读性和代码质量。
C++ 风格¶
我们在C++编码规范上遵循Google C++风格指南。
Python 风格¶
我们遵循black代码风格作为Python编码标准。
Rust 风格¶
我们遵循rust-lang代码风格,并使用GraphScope自定义的配置来规范Rust语言的编码标准。
Java 风格¶
我们在Java编码规范上遵循Google Java Style Guide。
脚本风格¶
我们在shell脚本编码规范上遵循Google Shell风格指南。
样式检查与规范工具¶
GraphScope针对每种语言使用不同的代码检查工具来强制执行代码风格规则:
C++: clang-format-8 和 cpplint
Python: Flake8
Rust语言: rust-fmt
Java: google-java-format
每个代码检查工具都可以集成到构建流程中,以确保代码符合风格指南要求。 以下是检查各语言代码风格的命令:
对于C++代码,通过MakeFile命令进行格式化和代码检查:
# format
$ make graphscope_clformat
# lint
$ make graphscope_cpplint
对于Python:
首先安装依赖项:
$ pip3 install -r coordinator/requirements-dev.txt
检查样式:
$ pushd python
$ python3 -m isort --check --diff .
$ python3 -m black --check --diff .
$ python3 -m flake8 .
$ popd
$ pushd coordinator
$ python3 -m isort --check --diff .
$ python3 -m black --check --diff .
$ python3 -m flake8 .
对于Rust语言,我们提供了一个shell脚本来执行格式检查:
$ cd interactive_engine/executor
$ ./check_format.sh
对于Java:
下载google-java-format工具:
$ wget https://github.com/google/google-java-format/releases/download/v1.13.0/google-java-format-1.13.0-all-deps.jar
检查样式:
$ files_to_format=$(git ls-files *.java)
$ java -jar google-java-format-1.13.0-all-deps.jar --aosp --skip-javadoc-formatting -i $files_to_format