# Refactoring

Refactoring is the process of restructuring existing code without changing its external behavior. The goal is to improve the code's internal structure -- making it more readable, maintainable, and efficient -- while preserving the same functionality and API contract. Refactoring is typically done in small, incremental steps, each verified by automated tests.

Common refactoring techniques include extracting methods or functions, renaming variables for clarity, removing code duplication, simplifying conditional logic, and decomposing large classes or modules into smaller, focused units. Automated refactoring tools built into IDEs can perform many of these transformations safely.

In API development, refactoring backend service code should not change the API's external behavior -- clients should continue to work without modification. API gateways support safe refactoring by providing a stable external interface while the backend is restructured. For example, a team can refactor a monolithic backend into microservices and use the gateway to route requests to the new services, all without changing the public API that clients depend on.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.serverlessapigateway.com/glossary/r/refactoring.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
