These three functions (getUserList, getArticleList, getCommentList)
contain a lot of duplicated code for pagination, sorting, and filtering.
Please help me:
1. Analyze the duplicated parts
2. Propose a refactoring plan
3. Evaluate the risk of the refactor
Step 3: Prepare a safety net (ask AI to help)
Before refactoring, please write test cases for me first
so that the behavior stays the same after the refactor
Step 4: Refactor step by step (AI executes, you verify)
According to the plan we just discussed, first extract the shared function
Now optimize the data structure
Finally update the call sites
Step 5: Verify the result (done by you)
Run all tests and make sure they pass
Manually test key functionality
Check whether code quality has improved
Real conversation example:
You: These three functions have a lot of duplicate code. Can you help me refactor them?
[Paste the code of the three functions]
AI: I analyzed them, and the main differences are:
- getUserList: gets the user list
- getArticleList: gets the article list
- getCommentList: gets the comment list
Their common logic is pagination, sorting, and filtering.
Refactoring plan:
1. Extract a generic getList function
2. Pass the different parts as parameters
This can reduce 80% of the duplicated code.
Should I implement it?
You: Yes, but first help me write test cases
AI: [Generates tests]
I have written tests for all three functions,
covering the main use cases.
Can I start refactoring now?
You: Yes, start refactoring
AI: [Refactors the code]
Refactoring is complete. I created a generic getList function,
and the original three functions now all call that shared function.
Please run the tests to confirm everything works: npm test
3.4 Code Review
Scenario: a teammate submitted code, and you need to review it.
Complete workflow:
Step 1: Quickly understand the change (ask AI to help)
Summarize what this PR changed
Which files were mainly modified
You: Help me review this PR
[Provide the PR link or code]
AI: I reviewed the change and found these issues:
1. Security issues:
- Line 23: user input is not escaped, which may lead to XSS risk
- Line 45: the SQL query is built by string concatenation, which risks injection
2. Performance problems:
- Line 67: the database is queried inside a loop, which creates an N+1 problem
3. Code quality:
- The handleSubmit function is too long (200 lines); consider splitting it
- Error handling is missing
Would you like me to explain any of these in more detail?
You: Explain the SQL injection issue in detail
AI: On line 45:
```javascript
const query = `SELECT * FROM users WHERE name = '${userName}'`;
```
This directly concatenates user input into the SQL statement.
If a user enters `' OR '1'='1`, it becomes:
```sql
SELECT * FROM users WHERE name = '' OR '1'='1'
```
This would return all user data.
The correct approach is to use a parameterized query:
```javascript
const query = 'SELECT * FROM users WHERE name = ?';
db.query(query, [userName]);
```
Would you like me to fix it?