File size: 3,818 Bytes
af66d50 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | # 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);
```
```
---
|