From the course: Agile Software Development: Refactoring

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Refactoring: Extract Variable/Extract Method

Refactoring: Extract Variable/Extract Method

From the course: Agile Software Development: Refactoring

Start my 1-month free trial

Refactoring: Extract Variable/Extract Method

- [Instructor] I've gone ahead and refactored many of the hard-coded numbers, and I've included a copy of my code, so you can begin at the same point as me. The next thing I notice is lots of duplicated code. One of the things that I see duplicated over and over is this phrase, items of i. We see it here at line 19, we see it at line 20, we see it at line 21, we see it over and over in the code. This is a really good candidate for refactoring, this is a good candidate for extracting a variable. So let's highlight that phrase, right-click, select Refactor, extract variable. Make sure we click Replace all 34 occurrences, so it really does the entire refactoring for us. And IntelliJ has suggested a really good default name here: item. So let's press enter and accept that. It looks like it has refactored this everywhere that it's highlighted: lines 20, 21, 22, et cetera, and let's rerun the tests. So everything is passing, our pin down tests are continuing to provide safety as we refactor…

Contents