From the course: Refactoring with GitHub Copilot

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Removing dead code

Removing dead code

As a codebase grows, functions can be replaced, especially when refactoring. As a result, dead code that is code that can't be reached, can make it into your project. This is a simple exercise. This function accepts a price and if a user is a member. If the price is greater than 100, a 10 percent discount, and if they're a member, another five percent discount, then a check on if the amount is negative. If it is, they get a 50 percent discount. But how could a price be negative? Okay. So it's a dumb example, but it will work here. I'll ask Copilot, is there dead code here? Now this is cool. A static analyzer would not catch something like this, whereas Copilot, an AI with context, sees the price is less than zero and says it's logically incorrect and unnecessary. Now, just for the fun of it, I'm going to change the variable names and function call to something without context. Price becomes number and member becomes a bool. I'm going to call all the comments. And I will call this a…

Contents