Reckless refactoring of complicated method may make it even worse:

void add(Object element) {
	if (!readOnly) {
		int newSize = size + 1;
		if (newSize > elements.length) {
			Object[] newElements = new Object[elements.length + 10];
			for (int i = 0; i < size; i++) {
				newElements[i] = elements[i];
			}
			elements = newElements;
		}
		elements[size++] = element;
	}
}

void add(Object element) {
	if (!readOnly) {
		growIfNecessaryAndAdd(element)
	}
}

private void growIfNecessaryAndAdd(element) {
	int newSize = size + 1;
	if (newSize > elements.length) {
		grow();
	}
	elements[size++] = element;
}

private void grow() {
	Object[] newElements = new Object[elements.length + 10];
	for (int i = 0; i < size; i++) {
		newElements[i] = elements[i];
	}
	elements = newElements;
}

Recursive application of extract method refactoring resulted in a suboptimal code. It has now fewer levels of nesting, but is almost as incomprehensible as before, because you still need to parse whole code in your head, to understand the algorithm. See how to do it properly.

W. Gdela

            Votes: 0

See more like this: