إذا كان لديك مجموعة مخصّصة من أفضل الممارسات أو الاصطلاحات التي تريد أن يتحقّق منها Gemini Code Assist على GitHub أو يتبعها عند إجراء مراجعة للرمز البرمجي، يمكنك إضافة ملف styleguide.md Markdown إلى المجلد الجذر .gemini/ في المستودع.
يمكن لمستخدمي
إصدار المؤسسة من
Gemini Code Assist على GitHub
استخدام وحدة تحكّم Google Cloud لإضافة معلومات دليل الأسلوب
لاستخدامها في مستودعات متعددة.
في كلتا الحالتين، يتم التعامل مع دليل الأسلوب على أنّه ملف Markdown عادي، ويوسّع الطلب العادي الذي يستخدمه Gemini Code Assist على GitHub.
الأنماط العادية لمراجعة الرموز البرمجية
في حال عدم تحديد أدلة أنماط مخصّصة، تكون هذه هي الفئات الرئيسية للمجالات التي يركّز فيها Gemini Code Assist على مراجعة الرمز:
الصحة: التأكّد من أنّ الرمز البرمجي يعمل على النحو المنشود ويتعامل مع الحالات الحدّية، والتحقّق من أخطاء المنطق أو حالات التزامن أو الاستخدام غير الصحيح لواجهة برمجة التطبيقات
الكفاءة: تحدّد هذه الفئة المشاكل المحتملة في الأداء أو المجالات التي يمكن تحسينها، مثل الحلقات المفرطة، وتسرب الذاكرة، وبُنى البيانات غير الفعّالة، والحسابات المكرّرة، والتسجيل المفرط، ومعالجة السلاسل غير الفعّالة.
سهولة الصيانة: تقيّم هذه السمة سهولة قراءة الرمز البرمجي، وتصميم الوحدات، والالتزام بالأساليب الشائعة وأفضل الممارسات في اللغة. يستهدف هذا النوع من المراجعات التسمية السيئة للمتغيرات والدوال والفئات، وعدم توفّر التعليقات أو المستندات، والرموز البرمجية المعقّدة، وتكرار الرموز البرمجية، والتنسيق غير المتسق، والأرقام السحرية.
الأمان: تحدّد هذه الفئة الثغرات الأمنية المحتملة في معالجة البيانات أو التحقّق من صحة الإدخال، مثل التخزين غير الآمن للبيانات الحسّاسة، وهجمات الحقن، وعناصر التحكّم غير الكافية في الوصول، وتزوير الطلبات من موقع إلكتروني مختلف (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 على GitHub، يمكنك تطبيق دليل أسلوب واحد على مستودعات متعددة. يتم تجميع المستودعات حسب اتصال Developer Connect، ويمكن إدارة دليل الأسلوب الجماعي من خلال وحدة تحكّم Google Cloud.
إذا تمت إدارة مستودع كجزء من مجموعة وكان لديه أيضًا ملف styleguide.md خاص به، سيتم دمج ملف styleguide.md الخاص بالمستودع مع دليل الأسلوب الخاص بالمجموعة.
توضّح الخطوات التالية كيف يمكن لمستخدمي إصدار المؤسسة التحكّم في دليل أسلوب واحد على مستوى مستودعات متعددة. تفترض هذه الخطوات أنّك أعددت Gemini Code Assist على GitHub سابقًا.
في Google Cloud Console، انتقِل إلى صفحة الوكلاء والأدوات في Gemini Code Assist.
في قسم الوكلاء، ابحث عن بطاقة إدارة الرمز المصدر في Code Assist وانقر على خيارات متقدمة.
يتم فتح لوحة إدارة رمز المصدر في "مساعد التعديل".
في جدول عمليات الربط، انقر على اسم عملية الربط التي تريد تطبيق دليل أسلوب عليها.
سيتم فتح صفحة التفاصيل الخاصة بالاتصال.
في علامة التبويب دليل الأسلوب، أضِف دليل الأسلوب الذي تريد أن تستخدمه المستودعات المرتبطة بهذا الربط.
انقر على حفظ.
الخطوات التالية
- تعديل الإعدادات في Gemini Code Assist على GitHub