> For the complete documentation index, see [llms.txt](https://docs.serverlessapigateway.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.serverlessapigateway.com/glossary/r/refactoring.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
