Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära 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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

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

bookExtract Function Technique

Svep för att visa menyn

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2
some-alt