Skip to content

.pr_agent_auto_best_practices

root edited this page Jul 29, 2025 · 15 revisions

Pattern 1: Fix typos and grammatical errors in documentation, comments, and user-facing text to maintain professional quality and clarity. This includes correcting spelling mistakes, grammar issues, and ensuring consistent terminology usage throughout the codebase.

Example code before:

# Fix the typo in configuration filename
To configure automatic ticket creation, add the following to `.pr_agnet.toml`:

Example code after:

# Fix the typo in configuration filename  
To configure automatic ticket creation, add the following to `.pr_agent.toml`:
Relevant past accepted suggestions:

Pattern 2: Add proper null safety checks and defensive programming practices when accessing nested dictionary keys, object attributes, or API responses to prevent AttributeError and KeyError exceptions at runtime.

Example code before:

# Unsafe nested access
sender = pr_data.get("author", {}).get("user", {}).get("name", "")
files = [f for f in files if not r.match(f['path']['toString'])]

Example code after:

# Safe nested access with null checks
author = pr_data.get("author", {})
user = author.get("user", {}) if author else {}
sender = user.get("name", "") if user else ""
files = [f for f in files if f.get('path', {}).get('toString') and not r.match(f['path']['toString'])]
Relevant past accepted suggestions:

Pattern 3: Replace debug print statements with proper logging using the existing logger infrastructure to maintain consistent logging patterns and avoid cluttering production output with debug information.

Example code before:

print("DEBUG: Returning True from should_process_pr_logic")
print(f"DEBUG: Provider={provider_name}, result={result}")

Example code after:

get_logger().debug("Returning True from should_process_pr_logic")
get_logger().debug(f"Provider={provider_name}, result={result}")
Relevant past accepted suggestions:

Pattern 4: Extract duplicated code logic into helper methods or functions to follow the DRY (Don't Repeat Yourself) principle and improve code maintainability, especially when the same conditional checks or processing patterns appear multiple times.

Example code before:

if get_settings().pr_reviewer.get('publish_output_no_suggestions', True) or "No major issues detected" not in pr_review:
    # publish logic
else:
    # same condition check repeated elsewhere
    if get_settings().pr_reviewer.get('publish_output_no_suggestions', True) or "No major issues detected" not in pr_review:

Example code after:

def _should_publish_review(self, pr_review):
    return get_settings().pr_reviewer.get('publish_output_no_suggestions', True) or "No major issues detected" not in pr_review

if self._should_publish_review(pr_review):
    # publish logic
Relevant past accepted suggestions:

Pattern 5: Add proper error handling with try-catch blocks for operations that can fail, such as API calls, file operations, or data parsing, to prevent crashes and provide meaningful error messages to users.

Example code before:

# API call without error handling
self.client.update_secret(
    SecretId=secret_name,
    SecretString=secret_value
)

Example code after:

# API call with proper error handling
try:
    self.client.put_secret_value(
        SecretId=secret_name,
        SecretString=secret_value
    )
except Exception as e:
    get_logger().error(f"Failed to store secret {secret_name}: {e}")
    raise e
Relevant past accepted suggestions:

[Auto-generated best practices - 2025-07-29]

Clone this wiki locally