andré michael bonkowski

Refactoring in AppCode

| Comments

AppCode offers a rich set of refactoring’s, which is fast, safe, and easy to use.

Rename

Naming is surprisingly difficult. It’s easy to get “paralyzed” if the perfect name of a class or method does not pops into place immediately. But hey, you can always rename it later!

The “Rename” refactoring in AppCode is triggered by the shortcut ⇧F6. By using this, you can rename a file, class, method, property, ivar, local variable, and so on. In the file or project view, the shortcut triggers renaming a file or a class.

Rename class from file or project view

If you just are renaming a file (such as a plist or an image), AppCode will rename the file, and also change where it is referenced. If you are renaming a class, both the header, and the implementation of the class is renamed, and the references of the files in the project file. If you select Rename when the cursor is on a class name used by the class, the selected class is renamed.

If you triggers the rename from inside the source file, the name of the class, variable, and so on, is entering a “edit mode,” almost like “Edit all in Scope,” in Xcode. The difference is that while in Xcode you renamed only the names inside the current class, the renaming in AppCode is global.

Inline rename class from the file or project view In the example above, changing the name KMRObjectViewLayout to something else, will rename the KMRObjectViewLayout class, it’s containing file, and all the references to the class.

Rename method inline

Here is an example of how it looks like when you are renaming a method name. All instances in scope are selected and edited.

Rename class from file or project view In the next example, shown above, we want to rename an instance variable. AppCode gives us a few name proposals, based on the name of the variable, and its usage. (As you can see, contentWidth is suggested based on the method name)

Change signature

I often begin with a method signature that after a while needs to be altered. This can be to include an additional argument, or maybe the argument is not needed anymore, or even the order of the arguments is not right. If the method is called from several places, this can be a time-consuming and error prone task. By using the ⌘F6, Change Signature, this is done in a simple and safe way.

The video below shows how you can use the Change Signature refactoring.

As you can see, this is a really powerful feature, which I use a lot. You can also change the return type, and even convert a method to a function or a block, or vice versa.

Convert

Converting a method to a function or a block can also be done by using one of the Convert to menu items. In addition to these, you also have Convert to Property and Convert to Instance Variable.

Move

The Move refactoring allows you to move a property, ivar’s and methods from one class to another. If the target class does not exist, you get a warning that tells you that. While this is correct, I would expect AppCode to ask whether I want to create the non-existing class. There is a feature request for this already registered in the AppCode issue tracker, so I expect it to be in a future build of the EAP.

Update: The “missing” feature is now fixed.

This is one of the other great things about JetBrains. They respond to bug reports and feature requests almost immediately, and the Issue Tracker is publicly visible.

Copy

When in the project browser, selecting a header, or an implementation file, or when inside one of these files, F5 will prompt you to enter the name of the new copy of the files, and where to copy them. While in the “To directory” input field ⌃␣ will provide path completion. When accepting, the class name inside the source files is changed as well.

Copy a class

Safe Delete

Before you delete a class or a file, you want to ensure that it’s not used somewhere. Safe Delete does that for you, and warn you if this is the case. Besides search for usages, by default it also searches in comments and strings. The keyboard shortcut for this are ⌦⌘

Extract…

There are several Extract factoring’s, and here is a short description of each:

Extract Variable

Let say that you have a hard coded value. You select the value, and type ⌥⌘V. AppCode will declare a variable above where it’s used, with the correct type, and the name of the new variable selected. To change the proposed name, just begin to type the name of the variable. If no value or expression is explicitly selected, AppCode gives you a suggestion of what to base the value of the introduced value from as shown in the example below:

Select variable expression

Extract Constant

This refactoring, ⌥⌘C, is similar to Extract Variable, but this introduces a constant instead. If no actual value is selected, AppCode gives you an opportunity to select what the constant is based on from values and expressions in scope. When the constant is selected, AppCode asks for you if you would like to replace only this one, or all similar values found in the current file.

Extract Parameter

If you are creating a variable inside a method, but you would like it to be passed as a parameter, Extract parameter, ⌥⌘ P is the refactoring you would like to use.

Extract Property

By using ⌥⌘E on a variable, AppCode shows a dialog where the declaration of the property is shown, togheter with a checkbox. If you check the checkbox, the property is generated in a private category. If not, the property is declared in the header.

Introduce property

Extract Instance Variable

By using ⌥⌘I on a local variable, AppCode changes it into an ivar, but with the option to generate a property for the variable if the checkbox is selected. You can also select to declare the ivar in the interface (which you don’t want to do).

Extract Define

⌥⌘F extracts a macro from the selected value or expression

Extract Typedef

As the name suggests, ⌥⌘F creates a typdef from the selected type.

Extract Method

This is one of the most my frequently used refactoring. Let say a method has too much responsibility, and you want to move some of the logic into a new method, Select the code you want to move, type ⌥⌘F, and AppCode generates a new method for you, including necessary parameters.

Extract method

As you can see from the screenshot above, it’s quite similar to Change Signature. The main difference is the Targets drop down. Depending on what you select in this drop down, the signature is declared in the header file, in a private category, or just in the implementation file.

Extract Block Parameter

By using Extract Block Parameter you select a chunk of code. A block declaration based on the selected code is created, and the signature of the method is changed to take a block of this type. Finally, the existing usages of this method are changed so that the extracted block is passed as a parameter.

Extract Superclass

To create a superclass from an existing class, the easiest way to accomplish this is to select the Extract Superclass. This refactoring does not have a default keyboard shortcut (You can create one yourself). You will be presented with a dialog where methods, properties, and ivars are shown in a list with checkboxes, and an input field where you type the name of the new superclass. You check the name of what you want to move into the new superclass. If one of the selected methods has dependencies to other methods, properties, or ivars, you get a warning, telling you about the problem. Extract Superclass

Extract Subclass

Here you create a subclass from a superclass. You select which methods, ivars and properties you want to take with you into the new subclass.

Extract Protocol and Category

Similar to the two above, except you are extracting protocols and categories.

Inline

If you want to inline a method, AppCode will move the implementation of the method into where it’s called from. If the method is called from more than one place, AppCode prompts you and asks for “Do you want to inline ‘n’ usages of method ‘mehodName’? You options are: cancel the refactoring, execute the operation or view usages of the method you have selected to inline. If you select the latter, a list of usages is shown, and while navigating in this list, you can exclude the calls you don’t want to inline by . If you change your mind, and want to include them again, you can do that by typing⇧⌫

Below is a sample of an excluded usage of a method. This feature is also used in all refactoring where the change will affect more than in one place. Excluded file

You can think of the Inline refactoring as an inverse of Extract Variable, Extract Constant and so on.

Pull Members Up and Pull Members Down

These refactoring’s are the same as Extract Superclass and Extract Subclass. (At least as far as I know).

Refactor This…

Refactor this If your having trouble remembering all the different Refactor keyboard shortcuts, you should at least try to learn this one: ⌃T. This shortcut will present a popup menu containing all the available refactoring’s, that you can select from, by using the number in front of the name, or navigate by using the arrow keys, and select the one you want by

If you are new to AppCode, or haven’t used the Refactorings that AppCode offers, I would suggest that you take a closer look at what it have to give.

Comments