Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Extract Function Technique | Refactoring Techniques
Code Quality and Refactoring in Python

bookExtract 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)
copy

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.

question mark

Which of the following is a key benefit of extracting functions from a large function?

Select the correct answer

question-icon

Fill in the blank to complete the refactored helper function for finding the highest and lowest grades.

(grades): return max(grades), min(grades)

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 5.26

bookExtract Function Technique

Sveip for å vise menyen

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)
copy

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.

question mark

Which of the following is a key benefit of extracting functions from a large function?

Select the correct answer

question-icon

Fill in the blank to complete the refactored helper function for finding the highest and lowest grades.

(grades): return max(grades), min(grades)

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2
some-alt