Book Image

Android Studio Cookbook

By : Mike van Drongelen
Book Image

Android Studio Cookbook

By: Mike van Drongelen

Overview of this book

This book starts with an introduction of Android Studio and why you should use this IDE rather than Eclipse. Moving ahead, it teaches you to build a simple app that requires no backend setup but uses Google Cloud or Parse instead. After that, you will learn how to create an Android app that can send and receive text and images using Google Cloud or Parse as a backend. It explains the concepts of Material design and how to apply them to an Android app. Also, it shows you how to build an app that runs on an Android wear device. Later, it explains how to build an app that takes advantage of the latest Android SDK while still supporting older Android versions. It also demonstrates how the performance of an app can be improved and how memory management tools that come with the Android Studio IDE can help you achieve this. By the end of the book, you will be able to develop high quality apps with a minimum amount of effort using the Android Studio IDE.
Table of Contents (17 chapters)
Android Studio Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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.