Refactoring your code
Good software engineers refactor their work continuously. Names of methods and members should always indicate what they are doing. Since business requirements often change during the development process, in particular when agile methodologies come in, so do your apps.
If you choose the right names and stick to the rule that the length of methods must be limited to, well let's say, scrolling one page at most to view the whole method, often you do not need many comments to explain what your code is doing. If it is hard to come up with a good name for a particular method, then it is probably doing too much.
Since changing names could be scary, as it could break your code, developers often choose not to do so. Or, they decide to do it later. You save yourself a few minutes by doing so in advance. Your code could be hard to understand if some one else has a look at your code or if you have a look at your code one year later. Going through the code to find out what a method does can be very time consuming. A descriptive name for your method can solve this.
The good news is that using Android Studio, refactoring is painless and pretty easy. Just highlight the name of a member or method, right-click on it, and pick the Refactor item from the context menu that pops up.
In the Refactor submenu that appears when you choose the Refactor item, you will find many interesting options. The one option that you will use here and which you will be using the most is the Rename… option.
How to do it…
The following steps describe how to rename a method in the Refactor submenu:
- Highlight the name of the method you would like to rename.
- From the context menu, choose Refactor.
- From the submenu, choose Rename (or use the shortcut Shift + F6).
- Now, you can rename your method or member in place and apply the changes by hitting the Enter button. Android Studio provides you with some suggestions that you can accept or you can type the name you want.
Tip
If you repeat step 2 and 3, a dialog box appears in which you can edit the name. (Or use the shortcut Shift + F6 twice).
- Click on the Preview button to see what the effect of the renaming will be.
- At the bottom of your screen, a new view appears, which shows you the impact of the renaming in each file (class, resource, or otherwise).
- Click on the Do refactor button in that view to apply all the changes.
The following screenshot shows an example of an in-place refactoring (renaming).
How it works...
Android Studio will take care of renaming a method or member and any references to it everywhere in the project. This includes Java classes, layouts, drawables, and anything else that you can think of.
There are many other interesting options available from the Refactor menu that you can use. Some of them will be discussed in the next chapters in the recipes where they will come in handy.
Now, let's move on to the next chapter and build a real app, shall we?
See also
- For more information about refactoring code, refer to Chapter 8, Improving quality.