--- title: "Claude for Code: Debugging, Refactoring, Explanations and Generation" description: "Claude is one of the best developer tools available today. Learn to use it for fast debugging, code review, refactoring, generating boilerplate and learning new languages." slug: "3-3-claude-per-codice" enslug: "3-3-claude-for-coding" accesslevel: premium status: published visible: true featured: true priority: 33 date: 2026-04-22 updated: 2026-04-22 author: "Dario Santocanale" reading_time: "10 min" prerequisites: - Basic programming knowledge - Having completed Clusters 1 and 2 tags: [claude, code, development, debug, refactoring, programming, premium] ---
If you write code and aren't using Claude, you're leaving hours of productivity on the table every day. This guide shows you how to integrate it into your development workflow.
---
Golden rule: show the code, don't describe it
90% of Claude usage errors for code come from prompts like: "I have a PHP function that does X and it doesn't work"
Claude works 10x better when it sees the real code:
[Paste the code]
The problem: [precise description of the bug / wrong behavior]
What I expected: [correct behavior]
What happens instead: [current behavior]
Context: PHP 8.1, Laravel 10, MySQL 8
---
1. Debugging
Debugging an error with stack trace
I have this error:
[Paste the complete stack trace]
The code causing the error:
[Paste the relevant function/class]
Related files (if necessary):
[Paste other relevant files]
Tell me: 1) Exact cause of the error, 2) Fix, 3) How to avoid it in the future.
Debugging silent behavior (no error, wrong output)
This code produces no errors but generates wrong output.
Code:
[code]
Test input: [specific input]
Expected output: [what it should produce]
Actual output: [what it actually produces]
Find the bug. Then explain why it behaves this way.
---
2. Explaining unfamiliar code
When you inherit legacy code or work on a new codebase:
Explain this code as if explaining to a senior developer
who doesn't know this codebase:
[code]
Include:
1. What it does in plain language
2. The execution flow step by step
3. Dependencies and side effects
4. Any design patterns used
5. Potential risks or obvious code smells
Variant for junior developers:
Explain this code to someone who knows how to program
but doesn't know [Python / React / this specific pattern].
Use analogies where useful.
---
3. Code review
Do a professional code review of this [function/class/module]:
[code]
Evaluate:
1. **Correctness**: bugs or unhandled edge cases
2. **Security**: vulnerabilities (SQL injection, XSS, CSRF, auth issues...)
3. **Performance**: expensive operations, N+1 queries, memory leaks
4. **Readability**: naming, cyclomatic complexity, comments
5. **Best practices**: for [language/framework]
6. **Testability**: is it easy to test? How to improve it?
Prioritize issues by impact. Always provide the corrected code,
not just a description of the problem.
---
4. Refactoring
Refactor this code:
[original code]
Refactoring goals:
- [e.g.: reduce duplication]
- [e.g.: separate concerns (SRP)]
- [e.g.: improve readability]
- [e.g.: make testable]
Constraints:
- Keep the same external interface (same inputs/outputs)
- Don't change business logic
- [other specific constraints]
Show me: 1) The refactored code, 2) The main changes made,
3) Why each change improves the code.
---
5. Generating boilerplate and structures
To avoid starting from scratch every time:
Generate the boilerplate for a [type of component/class/module]:
Requirements:
- Language/framework: [e.g.: TypeScript + React]
- Pattern: [e.g.: custom hook with state management and loading/error]
- Functionality: [what it should do]
- Conventions to follow: [naming, structure, comments]
Include TODO comments for parts I need to complete myself.
Practical example:
Generate a TypeScript React custom hook for API calls with:
- Loading state
- Error state
- Data state
- Refetch function
- Automatic cancellation on unmount (AbortController)
- TypeScript generics for the response type
Use current React 18+ best practices.
---
6. Writing tests
Write tests for this function:
[function to test]
Framework: [Jest / PHPUnit / pytest / ...]
Required coverage:
- Happy path (valid inputs)
- Edge cases: [list of edge cases]
- Error cases: [when it should fail]
- [any necessary mocks]
Each test must have a descriptive name that explains
exactly what it tests.
---
7. Automatic documentation
Write documentation for this code:
[code]
Format: [JSDoc / PHPDoc / Python docstring / README markdown]
Include for each public function/method:
- Brief description (1 line)
- Parameters with type and description
- Return value with type and description
- Usage example
- Any exceptions thrown
---
8. Learning a new language/framework
I'm an experienced developer in [language A].
I need to learn [language B / framework C] for [reason].
Show me the same thing in both languages:
[Paste a piece of code in the language you know]
Then tell me:
1. The 5 most important conceptual differences
2. Common "traps" for people coming from [language A]
3. [Language B] conventions I should adopt immediately
---
9. Query optimization
Optimize this SQL query:
sql [query]
Table schema:
sql [CREATE TABLE statements or description]
Current problem: [slow / doesn't scale / wrong results]
Approximate data: [e.g.: table_x has 2M rows, table_y has 500K]
Database: [MySQL 8 / PostgreSQL 15 / SQLite]
Tell me:
1. The specific problem with the current query
2. The optimized query with explanation
3. Which indexes to add
4. The execution plan I should expect after optimization
---
10. Security: code audit
Do a security audit of this code:
[code — controller, form handler, API endpoint, ...]
Context: [framework, application type, who has access]
Sensitive data handled: [passwords, payments, personal data, ...]
Look specifically for:
- OWASP Top 10 applicable to this context
- Input validation and sanitization
- Authentication and authorization
- Error handling (information disclosure)
- Dependencies with known vulnerabilities
For each issue: severity (Critical/High/Medium/Low),
explanation, concrete fix.
---
Setting up a Project for development
For developers using Claude regularly, create a dedicated Project:
## Tech stack
[List frameworks, languages, main libraries]
## Project conventions
- Naming: [camelCase / snake_case / ...]
- Patterns: [repository pattern / MVC / ...]
- Testing: [framework and approach]
- Comments: [language, level of detail]
## Existing codebase
[Description of the general architecture]
## NEVER do
- [Specific architecture violations to avoid]
- [Patterns not to use]
## Response format
When generating code: code first, then explanation.
Always indicate dependencies to install.
---
Up next →Il sistema di file che trasforma Claude nel tuo co-pilota personale 10 min