Extract Method / Variable
What is Extract Method / Variable?
Extract Method / Variable: Pull selected code out into its own method or variable. A JetBrains staple of clean refactoring. Extract method and extract variable are refactorings your IDE can do for you. Select a piece of code, choose extract, and the editor moves it into a new method or variable with a name you choose, updating the original code to use it.
Frequently asked questions about Extract Method / Variable
How is Extract Method / Variable used in practice?
In JetBrains IDEs the shortcuts are Ctrl+Alt+M for a method and Ctrl+Alt+V for a variable, or Cmd+Option+M and Cmd+Option+V on a Mac. VS Code offers them through the light bulb menu.
What should you keep in mind with Extract Method / Variable?
This is one of the simplest ways to make code readable. A condition like if ($order->paid_at && !$order->shipped_at && $order->total > 0) becomes if ($order->isReadyToShip()). Because the IDE does the edit, it handles variables and return values correctly, which makes small improvements safe to do while you work.
At a glance
| Category | Keyboard shortcuts |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |