راهنمای سبک بررسی کد

اگر مجموعه‌ای از بهترین شیوه‌ها یا قراردادهای سفارشی دارید که می‌خواهید Gemini Code Assist در GitHub هنگام انجام بررسی کد، آنها را بررسی یا دنبال کند، می‌توانید یک فایل markdown با styleguide.md را به پوشه .gemini/ root مخزن خود اضافه کنید. کاربران نسخه سازمانی Gemini Code Assist در GitHub می‌توانند از کنسول Google Cloud برای اضافه کردن اطلاعات راهنمای سبک برای استفاده در چندین مخزن استفاده کنند . در هر دو مورد، راهنمای سبک به عنوان یک فایل Markdown معمولی در نظر گرفته می‌شود و prompt استانداردی را که Gemini Code Assist در GitHub استفاده می‌کند، گسترش می‌دهد.

الگوهای استاندارد بررسی کد

وقتی راهنماهای سبک سفارشی مشخص نشده باشند، اینها دسته‌های اصلی حوزه‌هایی هستند که Gemini Code Assist بررسی کد خود را بر روی آنها متمرکز می‌کند:

  • صحت : اطمینان حاصل می‌کند که کد مطابق انتظار عمل می‌کند و موارد حاشیه‌ای را مدیریت می‌کند، خطاهای منطقی، شرایط رقابتی یا استفاده نادرست از API را بررسی می‌کند.

  • کارایی : تنگناهای عملکرد بالقوه یا حوزه‌های نیازمند بهینه‌سازی، مانند حلقه‌های بیش از حد، نشت حافظه، ساختارهای داده ناکارآمد، محاسبات تکراری، ثبت وقایع بیش از حد و دستکاری ناکارآمد رشته‌ها را شناسایی می‌کند.

  • قابلیت نگهداری : خوانایی کد، ماژولار بودن و پایبندی به اصطلاحات و بهترین شیوه‌های زبان را ارزیابی می‌کند. نامگذاری ضعیف برای متغیرها، توابع و کلاس‌ها، فقدان نظرات یا مستندات، کد پیچیده، تکرار کد، قالب‌بندی متناقض و اعداد جادویی را هدف قرار می‌دهد.

  • امنیت : آسیب‌پذیری‌های بالقوه در مدیریت داده‌ها یا اعتبارسنجی ورودی، مانند ذخیره‌سازی ناامن داده‌های حساس، حملات تزریق، کنترل‌های دسترسی ناکافی، جعل درخواست بین‌سایتی (CSRF) و ارجاعات مستقیم ناامن به اشیاء (IDOR) را شناسایی می‌کند.

  • متفرقه : هنگام بررسی درخواست pull، مباحث دیگری مانند تست، عملکرد، مقیاس‌پذیری، ماژولاریتی و قابلیت استفاده مجدد، و ثبت و نظارت بر خطاها در نظر گرفته می‌شوند.

راهنمای سبک نمونه

فایل 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 راه‌اندازی کرده‌اید .

  1. در کنسول گوگل کلود، به صفحه Gemini Code Assist Agents & Tools بروید.

    به بخش عوامل و ابزارها بروید

  2. در بخش Agents ، کارت Code Assist Source Code Management را پیدا کنید و روی Advanced کلیک کنید.

    پنجره‌ی «ویرایش کد، دستیار مدیریت کد منبع» باز می‌شود.

  3. در جدول Connections ، روی نام اتصالی که می‌خواهید راهنمای سبک را روی آن اعمال کنید، کلیک کنید.

    صفحه جزئیات اتصال باز می‌شود.

  4. در تب راهنمای سبک ، راهنمای سبکی را که می‌خواهید مخازن مرتبط با این اتصال از آن استفاده کنند، اضافه کنید.

  5. روی ذخیره کلیک کنید.

قدم بعدی چیست؟