Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

"Extract Function" from block of code #1106

Closed
scriptnull opened this issue Jul 23, 2017 · 2 comments
Closed

"Extract Function" from block of code #1106

scriptnull opened this issue Jul 23, 2017 · 2 comments

Comments

@scriptnull
Copy link

scriptnull commented Jul 23, 2017

I am not sure if this feature already exists, but as far as I have researched, I am not able to find.

I remember seeing this feature used significantly in Visual Studio while writing C# by almost every .NET programmer, I have come across. I am surprised, that I am not able to find it in VS code.

Let's say I have a something like

package arrays

import "testing"

func TestNewBitArray(t *testing.T) {
	var size int64 = 1
	a := NewBitArray(size)
	bArray := []int8(a)
	if len(bArray) != 1 {
		t.Error("Expected length to be 1, but got", len(bArray))
	}

	size = 10
	a = NewBitArray(size)
	bArray = []int8(a)
	if len(bArray) != 2 {
		t.Error("Expected length to be 2, but got", len(bArray))
	}

	size = 32
	a = NewBitArray(size)
	bArray = []int8(a)
	if len(bArray) != 4 {
		t.Error("Expected length to be 4, but got", len(bArray))
	}
}

There are repeated blocks of instructions the code, which someone might prefer writing during the initial phase of development. After things start to work, I would typically want to make a function out of the repeated block and use it.

Now the workflow would be, I would select the repeated code block. In my case,

	a := NewBitArray(size)
	bArray := []int8(a)
	if len(bArray) != 1 {
		t.Error("Expected length to be 1, but got", len(bArray))
	}

and execute the command called "Extract Function" from context menu or from cmd+p or from "Go" Menu.

This would result in generating functions with appropriate method signature.

	var checkSizeClosure = func(size int) {
		a := NewBitArray(size)
		bArray := []int8(a)
		if len(bArray) != 1 {
			t.Error("Expected length to be 1, but got", len(bArray))
		}
	}

or

func checkSizeLocal(t *testing.T, size int) {
	a := NewBitArray(size)
	bArray := []int8(a)
	if len(bArray) != 1 {
		t.Error("Expected length to be 1, but got", len(bArray))
	}
}

With the newly generated function, I can tweak it a little bit to add an expected variable and my tests could be refactored into an idiomatic table based golang tests like this

package arrays

import "testing"

func TestNewBitArray(t *testing.T) {
	cases := []struct {
		input    int
		expected int
	}{
		{1, 1}, {10, 2}, {32, 4},
	}

	checkSize := func(size, expected int) {
		a := NewBitArray(size)
		bArray := []int8(a)
		if len(bArray) != expected {
			t.Errorf("Expected length to be %d, but got %d", expected, len(bArray))
		}
	}

	for _, testCase := range cases {
		checkSize(testCase.input, testCase.expected)
	}
}

Having "Extract Function" feature would help much in cleaning the code base that is full of repeated code.

@ramya-rao-a
Copy link
Contributor

Yes, this would be a cool feature indeed.
#588 was logged to track this.
Closing this issue in favor of #588

PRs are welcome

@vscodebot vscodebot bot locked and limited conversation to collaborators Jan 23, 2018
@ramya-rao-a
Copy link
Contributor

The latest beta version (0.9.3-beta.2) of this extension has this feature. Please see Refactoring in VS Code on how you can use the refactor feature.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants