DaleSchool

Reinventing Code Writing

Beginner20min

Learning Objectives

  • Generate new code using natural language
  • Request refactoring of existing code
  • Understand the difference between good and bad prompts

Working Code

Start Claude Code in an empty directory and make a request:

> Create a simple REST API server with Express.js.
> A GET /api/health endpoint that returns { status: "ok" }.

Claude generates multiple files like package.json, server.js, etc. Now let's refactor:

> Separate the routes in server.js into a routes/ directory

Claude reorganizes the file structure and updates the import paths automatically.

Try It Yourself

Compare the quality of refactoring requests:

Bad prompt:

> Clean up the code

Good prompt:

> Refactor the error handling middleware in server.js to follow Express best practices.
> Specifically: 1) Extract a custom error class, 2) Separate the error handler middleware, 3) Wrap async/await errors

Compare the two results. The more specific your request, the better the output.

"Why?" — Principles of Prompt Writing

When requesting code from Claude Code, treat it like asking a fellow developer.

The Good Prompt Formula

  1. What — what task to perform
  2. Where — which file or location
  3. How — specific requirements or constraints
> Add a date formatting utility function to src/utils/date.ts.
> Takes an ISO 8601 string and converts it to "March 15, 2025" format.
> Use Intl.DateTimeFormat without libraries like dayjs.

Code Generation vs Refactoring

| Task | Prompt Example | | ----------------- | ------------------------------------------- | | New code | "Create ~" | | Refactoring | "Change ~ to use ~ approach" | | Adding types | "Add TypeScript types to this function" | | Applying patterns | "Convert this code to the Strategy pattern" |

Deep Dive

Should you always accept code Claude generates?

No. You should always review code generated by Claude. Especially:

  • Security-related code (authentication, input validation)
  • Database queries (SQL injection risks)
  • External API calls (key exposure, error handling)

Reading diffs carefully is essential. This practice itself builds your code review skills.

  1. In an empty directory, ask "Create a backend API for a TODO app."
  2. Review the generated code, find areas for improvement, and request refactoring.
  3. Make the same request at two different levels of specificity. Compare the results.

Q1. Which is the best prompt?

  • A) "Write some code"
  • B) "Write good code"
  • C) "Add a user list query API to src/api/users.ts. Query the DB with Prisma and support pagination."
  • D) "Write the best code possible, really good code"