Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Android Studio Cookbook

You're reading from  Android Studio Cookbook

Product type Book
Published in Oct 2015
Publisher
ISBN-13 9781785286186
Pages 232 pages
Edition 1st Edition
Languages
Author (1):
Mike van Drongelen Mike van Drongelen
Profile icon Mike van Drongelen
Toc

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:

  1. Highlight the name of the method you would like to rename.

  2. From the context menu, choose Refactor.

  3. From the submenu, choose Rename (or use the shortcut Shift + F6).

  4. 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).

  5. Click on the Preview button to see what the effect of the renaming will be.

  6. 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).

  7. 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 qua lity.

You have been reading a chapter from
Android Studio Cookbook
Published in: Oct 2015 Publisher: ISBN-13: 9781785286186
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}