# Coding Prompts ## Code Refactoring & Optimization **Difficulty:** Intermediate | **Best Model:** Claude 3.5 Sonnet / GPT-4o | **Use Case:** Improving existing code quality **Tags:** coding, refactoring, optimization, clean-code ### Prompt: ```text Act as a Senior Software Engineer. Refactor the following code for better readability, performance, and adherence to DRY principles. Explain the time and space complexity optimizations. ```python [CODE_SNIPPET] ``` ``` ### Variables/Template Example: ```text CODE_SNIPPET: def calc(n): r=[] for i in range(n): if i%2==0: r.append(i) return r ``` ### Example Output: ```text Here is the refactored code using a list comprehension, which is more Pythonic and slightly faster: ```python def calc(n): return [i for i in range(n) if i % 2 == 0] ``` **Time Complexity**: O(N) - We still iterate through `n` elements once. **Space Complexity**: O(N) - The resulting list contains N/2 elements. ``` --- ## Security Vulnerability Scanner **Difficulty:** Advanced | **Best Model:** GPT-4o | **Use Case:** Security and bug fixing **Tags:** coding, security, qa, vulnerabilities ### Prompt: ```text Act as a Lead Security Engineer. Analyze this code for potential vulnerabilities (SQL Injection, XSS, CSRF, etc.). Provide a detailed report and a secure version of the code. ```javascript [CODE_SNIPPET] ``` ``` ### Variables/Template Example: ```text CODE_SNIPPET: app.get('/user', (req, res) => { db.query(`SELECT * FROM users WHERE id = ${req.query.id}`); }); ``` ### Example Output: ```text **Vulnerability Found: SQL Injection (SQLi)** The code directly concatenates user input (`req.query.id`) into the SQL query string. An attacker could pass `1 OR 1=1` to expose all users. **Secure Code (Using Parameterized Queries):** ```javascript app.get('/user', (req, res) => { db.query('SELECT * FROM users WHERE id = ?', [req.query.id], (error, results) => { // handle results }); }); ``` ``` --- ## Comprehensive Unit Test Generator **Difficulty:** Intermediate | **Best Model:** Claude 3.5 Sonnet | **Use Case:** Test-driven development **Tags:** coding, testing, tdd, unit-tests ### Prompt: ```text Write comprehensive unit tests for the following function using [TESTING_FRAMEWORK]. Include happy paths, edge cases, and expected exceptions. Ensure maximum coverage. ``` [CODE_SNIPPET] ``` ``` ### Variables/Template Example: ```text TESTING_FRAMEWORK: pytest CODE_SNIPPET: def divide(a, b): return a / b ``` ### Example Output: ```text ```python import pytest from my_module import divide def test_divide_happy_path(): assert divide(10, 2) == 5.0 def test_divide_negative_numbers(): assert divide(-10, 2) == -5.0 assert divide(-10, -2) == 5.0 def test_divide_by_zero(): with pytest.raises(ZeroDivisionError): divide(10, 0) ``` ``` --- ## Database Schema Architect **Difficulty:** Advanced | **Best Model:** GPT-4o | **Use Case:** System Design **Tags:** coding, database, sql, architecture ### Prompt: ```text Act as a Database Architect. Design a relational database schema for a [APP_DESCRIPTION]. Provide the SQL `CREATE TABLE` statements, identify Primary and Foreign Keys, and suggest appropriate indexes for performance. ``` ### Variables/Template Example: ```text APP_DESCRIPTION: E-commerce platform handling users, products, orders, and reviews. ``` ### Example Output: ```text Here is the schema for the e-commerce platform: ```sql CREATE TABLE Users ( id UUID PRIMARY KEY, email VARCHAR(255) UNIQUE NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE Products ( id UUID PRIMARY KEY, name VARCHAR(255) NOT NULL, price DECIMAL(10, 2) NOT NULL ); -- Recommended Indexes CREATE INDEX idx_users_email ON Users(email); ``` ``` ---