Features

Refactorings and Quick-fixes changes in 2019.2

Refactorings and quick-fixes are one of the areas that set IDEs apart from text editors. With them, you can quickly move parts of code around in a safe manner, rename identifiers or change function/method signature across large codebases, extract interfaces from existing types, and even extract/inline values.

As such, it should come as no surprise that we gave them a bit of a tweak in 2019.2, and made them even better.

Refactorings in 2019.2

Let’s start with the improved Extract Method refactoring.

Previously, it was not possible to use it with code that contained a return statement. Thanks to feedback from our users, we made some changes to this refactoring and now it’s possible to use it in more conditions.

You might be asking yourself, ok, but what happens if my code selection contains such paths? How will the IDE transform the code? In this case, the IDE will automatically generate the exit code path for you by returning an additional boolean value and placing a check in place of the extracted code.

Let’s see it in action:

2019.2 - Extract Method with returns

Pretty amazing, right?

Now, let’s look at one of our more versatile refactorings: Change Signature. Introduced about a year ago, it gives us new quick-fixes to help fix broken, incomplete, or changing code.

This sets us up nicely for the second part of this article…

Quick-fixes in 2019.2

If you don’t want to read a lot of text, you can get a pretty good feel for what quick-fixes are now possible from the image below:

2019.2 - Change Signature Quick-fixes

But, if you are after something a little more substantial:

GoLand can now suggest and then change the signature of myHandler to match the expected type by the HandleFunc call, HandlerFunc:

package main

type HandlerFunc func(in int, out []byte)

func myHandler() {}

func HandleFunc(path string, handler HandlerFunc) {
	_, _ = path, handler
}

func main() {
	HandleFunc("/", myHandler)
}

It also can add missing parameters to functions with the correct type:

package main

func main() {
	demo(2)
}

func demo() {}

Are you adding errors to your functions or any other new return types? The Change Signature refactoring provided us with smarter ways to fix the following codebase:

package main

import "fmt"

func foo() {
	if false {
		return fmt.Errorf("some specific error happened") // call "Change signature" fix here
	}
}

All these changes mean that we can prototype the code quicker and you have support to safely enhance the existing codebases with new functionality.

That’s it for today. We’ve learned how the changes in the existing refactoring and quick-fixes systems allow us to become more productive and perform safer changes to codebases of any size.

Let us know your thoughts about these changes, or any others from 2019.2, in the comments section below, on our issue tracker, or on Twitter. Also, let us know what you’d like us to cover next. Is there anything you would like to learn more about in our IDE or what can we help you with to be an even more productive programmer?

image description

Discover more