From the course: Refactoring with GitHub Copilot

Unlock this course with a free trial

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

Optimizing conditionals and loops

Optimizing conditionals and loops - Github Copilot Tutorial

From the course: Refactoring with GitHub Copilot

Optimizing conditionals and loops

Two areas ripe for refactoring are conditionals and loops. And, of course, Copilot can help guide the way. Here's an example of a function that calculates a discount. Some initial thoughts on line 4, 16, and 22, it's checking the customer type. Could that logic be related to customer type, specific functions? Actually, slowing down a bit. This is three levels deep. And yikes, the isHoliday bool causes that, but it's also checked quite a few times, six, 12, 18, 20, 23. I have opinions on how to refactor this function, and I'm sure you do as well. I'm going to see what Copilot thinks. Refactor the conditionals. When I was writing this course, I tested and Copilot completely dropped logic around the VIP customer post type. Let's see if it does that again. Yes, interesting. It suggests the regular and premium and creates wrapper functions, which seems logical, but VIP is gone again. Maybe instead of asking for a refactor, I should ask to simplify it. Copilot does like switch statements…

Contents