如果您有一套自定义的最佳实践或惯例,希望
Gemini Code Assist on GitHub
在执行代码审核时检查或遵循这些实践或惯例,您可以将
styleguide.md Markdown 文件添加到代码库的 .gemini/ 根文件夹中。
企业版 Gemini Code Assist on GitHub 的用户可以使用控制台添加样式指南信息,以便在多个代码库中使用。 Google Cloud 在这两种情况下,样式指南都被视为常规 Markdown 文件,并扩展了
Gemini Code Assist on GitHub 使用的标准提示。
标准代码审核模式
如果未指定自定义样式指南,Gemini Code Assist 会将代码审核重点放在以下主要类别上:
正确性:确保代码按预期运行并处理极端 情况,检查逻辑错误、竞态条件或不正确的 API 用法。
效率:识别潜在的性能瓶颈或需要 优化的方面,例如过多的循环、内存泄漏、低效的数据结构、 冗余计算、过多的日志记录和低效的字符串操作。
可维护性:评估代码的可读性、模块化程度以及对 语言惯用法和最佳实践的遵循情况。针对变量、函数和类的命名不当、缺少注释或文档、代码复杂、代码重复、格式不一致和幻数。
安全性:识别数据处理或输入 验证中的潜在漏洞,例如敏感数据的不安全存储、注入攻击、 访问控制不足、跨站请求伪造 (CSRF) 和不安全 直接对象引用 (IDOR)。
其他:审核拉取请求时会考虑其他主题,例如测试、性能、可伸缩性、模块化和可重用性,以及错误日志记录和监控。
样式指南示例
styleguide.md 文件没有定义的架构。相反,它是对您希望 Gemini Code Assist 如何构建代码审核的自然语言描述。以下代码段是
styleguide.md 文件的一个示例:
# Company X Python Style Guide
## Introduction
This style guide outlines the coding conventions for Python code developed at
Company X. It's based on PEP 8, but with some modifications to address
specific needs and preferences within our organization.
## Key Principles
* **Readability:** Code should be easy to understand for all team members.
* **Maintainability:** Code should be easy to modify and extend.
* **Consistency:** Adhering to a consistent style across all projects
improves collaboration and reduces errors.
* **Performance:** While readability is paramount, code should be efficient.
## Deviations from PEP 8
### Line Length
* **Maximum line length:** 100 characters (instead of PEP 8's 79).
* Modern screens allow for wider lines, improving code readability in
many cases.
* Many common patterns in our codebase, like long strings or URLs, often
exceed 79 characters.
### Indentation
* **Use 4 spaces per indentation level.** (PEP 8 recommendation)
### Imports
* **Group imports:**
* Standard library imports
* Related third party imports
* Local application/library specific imports
* **Absolute imports:** Always use absolute imports for clarity.
* **Import order within groups:** Sort alphabetically.
### Naming Conventions
* **Variables:** Use lowercase with underscores (snake_case): `user_name`, `total_count`
* **Constants:** Use uppercase with underscores: `MAX_VALUE`, `DATABASE_NAME`
* **Functions:** Use lowercase with underscores (snake_case): `calculate_total()`, `process_data()`
* **Classes:** Use CapWords (CamelCase): `UserManager`, `PaymentProcessor`
* **Modules:** Use lowercase with underscores (snake_case): `user_utils`, `payment_gateway`
### Docstrings
* **Use triple double quotes (`"""Docstring goes here."""`) for all docstrings.**
* **First line:** Concise summary of the object's purpose.
* **For complex functions/classes:** Include detailed descriptions of
parameters, return values, attributes, and exceptions.
* **Use Google style docstrings:** This helps with automated documentation generation.
```python
def my_function(param1, param2):
"""Single-line summary.
More detailed description, if necessary.
Args:
param1 (int): The first parameter.
param2 (str): The second parameter.
Returns:
bool: The return value. True for success, False otherwise.
Raises:
ValueError: If `param2` is invalid.
"""
# function body here
```
### Type Hints
* **Use type hints:** Type hints improve code readability and help catch
errors early.
* **Follow PEP 484:** Use the standard type hinting syntax.
### Comments
* **Write clear and concise comments:** Explain the "why" behind the code,
not just the "what".
* **Comment sparingly:** Well-written code should be self-documenting where
possible.
* **Use complete sentences:** Start comments with a capital letter and use
proper punctuation.
### Logging
* **Use a standard logging framework:** Company X uses the built-in
`logging` module.
* **Log at appropriate levels:** DEBUG, INFO, WARNING, ERROR, CRITICAL
* **Provide context:** Include relevant information in log messages to aid
debugging.
### Error Handling
* **Use specific exceptions:** Avoid using broad exceptions like `Exception`.
* **Handle exceptions gracefully:** Provide informative error messages and
avoid crashing the program.
* **Use `try...except` blocks:** Isolate code that might raise exceptions.
## Tooling
* **Code formatter:** [Specify formatter, e.g., Black] - Enforces
consistent formatting automatically.
* **Linter:** [Specify linter, e.g., Flake8, Pylint] - Identifies potential
issues and style violations.
跨多个代码库管理样式指南
如果您拥有 企业版 Gemini Code Assist on GitHub,则可以将一个样式 指南应用于多个代码库。代码库按 Developer Connect 连接分组,并且通过控制台管理其集体样式指南 。 Google Cloud
如果代码库作为群组的一部分进行管理,但也有自己的 styleguide.md 文件,则代码库的 styleguide.md 会与群组样式指南合并。
以下步骤展示了企业版用户如何跨多个代码库控制一个样式指南。这些步骤假定您之前已 设置 Gemini Code Assist on GitHub。
在 Google Cloud 控制台中,前往 Gemini Code Assist 代理和工具 页面。
在代理 部分中,找到 Code Assist 源代码管理 卡片,然后点击高级 。
系统会打开修改 Code Assist 源代码管理 窗格。
在连接 表格中,点击要应用样式指南的连接的名称。
系统会打开连接的详情页面。
在样式指南 标签页中,添加您希望与此连接关联的代码库使用的样式指南。
点击保存 。
后续步骤
- 修改配置 Gemini Code Assist on GitHub。