Extract Function Technique
The extract function technique is a fundamental refactoring method that helps you improve code quality by breaking down large, complex functions into smaller, more focused helper functions. You should consider using this technique when a function grows too long, handles multiple responsibilities, or becomes difficult to understand and maintain. By isolating distinct pieces of logic into their own functions, you make your code easier to read, test, and reuse. This approach also helps you adhere to the Single Responsibility Principle, which states that each function should do one thing and do it well.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253# Before refactoring: a long function that analyzes student grades def analyze_grades(grades): # Calculate average total = 0 for grade in grades: total += grade average = total / len(grades) print(f"Average: {average:.2f}") # Find highest and lowest highest = grades[0] lowest = grades[0] for grade in grades: if grade > highest: highest = grade if grade < lowest: lowest = grade print(f"Highest: {highest}, Lowest: {lowest}") # Count passing students (>= 60) passing = 0 for grade in grades: if grade >= 60: passing += 1 print(f"Passing: {passing}, Failing: {len(grades) - passing}") grades = [82, 56, 91, 67, 45, 73, 88] analyze_grades(grades) # After refactoring: using extract function technique def calculate_average(grades): return sum(grades) / len(grades) def find_highest_and_lowest(grades): return max(grades), min(grades) def count_passing(grades, passing_mark=60): passing = sum(1 for grade in grades if grade >= passing_mark) failing = len(grades) - passing return passing, failing def analyze_grades_refactored(grades): average = calculate_average(grades) print(f"Average: {average:.2f}") highest, lowest = find_highest_and_lowest(grades) print(f"Highest: {highest}, Lowest: {lowest}") passing, failing = count_passing(grades) print(f"Passing: {passing}, Failing: {failing}") analyze_grades_refactored(grades)
1. Which of the following is a key benefit of extracting functions from a large function?
2. Fill in the blank to complete the refactored helper function for finding the highest and lowest grades.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain the benefits of the extract function technique in more detail?
What are some other refactoring techniques similar to extract function?
Can you show an example of when not to use the extract function technique?
Awesome!
Completion rate improved to 5.26
Extract Function Technique
Swipe to show menu
The extract function technique is a fundamental refactoring method that helps you improve code quality by breaking down large, complex functions into smaller, more focused helper functions. You should consider using this technique when a function grows too long, handles multiple responsibilities, or becomes difficult to understand and maintain. By isolating distinct pieces of logic into their own functions, you make your code easier to read, test, and reuse. This approach also helps you adhere to the Single Responsibility Principle, which states that each function should do one thing and do it well.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253# Before refactoring: a long function that analyzes student grades def analyze_grades(grades): # Calculate average total = 0 for grade in grades: total += grade average = total / len(grades) print(f"Average: {average:.2f}") # Find highest and lowest highest = grades[0] lowest = grades[0] for grade in grades: if grade > highest: highest = grade if grade < lowest: lowest = grade print(f"Highest: {highest}, Lowest: {lowest}") # Count passing students (>= 60) passing = 0 for grade in grades: if grade >= 60: passing += 1 print(f"Passing: {passing}, Failing: {len(grades) - passing}") grades = [82, 56, 91, 67, 45, 73, 88] analyze_grades(grades) # After refactoring: using extract function technique def calculate_average(grades): return sum(grades) / len(grades) def find_highest_and_lowest(grades): return max(grades), min(grades) def count_passing(grades, passing_mark=60): passing = sum(1 for grade in grades if grade >= passing_mark) failing = len(grades) - passing return passing, failing def analyze_grades_refactored(grades): average = calculate_average(grades) print(f"Average: {average:.2f}") highest, lowest = find_highest_and_lowest(grades) print(f"Highest: {highest}, Lowest: {lowest}") passing, failing = count_passing(grades) print(f"Passing: {passing}, Failing: {failing}") analyze_grades_refactored(grades)
1. Which of the following is a key benefit of extracting functions from a large function?
2. Fill in the blank to complete the refactored helper function for finding the highest and lowest grades.
Thanks for your feedback!