The method
Use in code editors or IDEs with LLM integration. Paste code snippets and ask for refactoring suggestions. Useful for improving code readability, performance, and maintainability. Focus on specific issues or general improvements.
The prompts
Prompt 1
Analyze the following Python code and suggest refactoring improvements focusing on readability, efficiency, and adherence to PEP 8 guidelines. Explain the rationale behind each suggested change and provide the refactored code snippet. Original Code: ```def calculate_average(numbers):
total = 0
for number in numbers:
total += number
average = total / len(numbers)
return average
```
total = 0
for number in numbers:
total += number
average = total / len(numbers)
return average
```
Prompt 2
Review the following JavaScript code for potential performance bottlenecks. Suggest optimizations related to algorithm complexity, memory usage, and rendering performance. Provide the optimized code along with explanations. Original Code: ```function processLargeArray(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
// Some complex operation
console.log(arr[i] * arr[j]);
}
}
}
```
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
// Some complex operation
console.log(arr[i] * arr[j]);
}
}
}
```
Prompt 3
Evaluate the following Java code snippet for potential security vulnerabilities, such as SQL injection, cross-site scripting (XSS), or buffer overflows. Provide recommendations for mitigating these vulnerabilities. Original Code: ```String query = "SELECT * FROM users WHERE username = '" + request.getParameter("username") + "'";``` Consider using parameterized queries or prepared statements instead.
Prompt 4
Refactor this C++ code to improve its object-oriented design. Focus on principles like single responsibility, open/closed, and dependency inversion. Explain how your changes improve the code's structure and maintainability. Original Code: ```class DatabaseConnection {
public:
void connect(std::string connectionString) {/*...*/}
void executeQuery(std::string query) {/*...*/}
void disconnect() {/*...*/}
};
```
public:
void connect(std::string connectionString) {/*...*/}
void executeQuery(std::string query) {/*...*/}
void disconnect() {/*...*/}
};
```
Prompt 5
Analyze the following Go code for concurrency issues, such as race conditions or deadlocks. Suggest improvements using Go's concurrency primitives (goroutines, channels, mutexes) to ensure safe and efficient concurrent execution. Original Code: ```var counter int
func incrementCounter() {
counter++
}
func main() {
go incrementCounter()
go incrementCounter()
fmt.Println(counter) // May print incorrect value
}
```
func incrementCounter() {
counter++
}
func main() {
go incrementCounter()
go incrementCounter()
fmt.Println(counter) // May print incorrect value
}
```