Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Refactoring Loops into Methods | Functions Returning Arrays & Refactoring Loops
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Methods & Modular Thinking

bookRefactoring Loops into Methods

When writing programs, you often encounter situations where similar or identical blocks of code appear in multiple places. This is especially common with loops that perform the same task, like printing every element in an array. Such repetition can make your code harder to read, maintain, and update. Instead of copying and pasting the same loop in different parts of your program, you can refactor the code by moving the repeated logic into a method. Refactoring means restructuring your code without changing its external behavior, making it cleaner and more organized.

Program.cs

Program.cs

Program.cs (refactored)

Program.cs (refactored)

copy
1234567891011121314151617181920212223
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 2, 4, 6, 8 }; // Print elements (first time) for (int i = 0; i < numbers.Length; i++) { System.Console.WriteLine(numbers[i]); } // Print elements (second time) for (int i = 0; i < numbers.Length; i++) { System.Console.WriteLine(numbers[i]); } } } }

Refactoring repeated code into methods helps you write programs that are easier to read and understand. When you give a descriptive name to a method, anyone reading your code can immediately tell what the method does without digging into the details of the loop. This also makes your code easier to maintain—if you need to change how you print the array, you only need to update the method in one place. Reducing repetition also lowers the chance of errors, since you avoid having to make the same change in multiple locations.

Program.cs

Program.cs

copy
12345678910111213141516171819202122
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] scores = { 10, 20, 30, 40 }; int total = SumArray(scores); System.Console.WriteLine("Sum: " + total); } public static int SumArray(int[] arr) { int sum = 0; for (int i = 0; i < arr.Length; i++) { sum += arr[i]; } return sum; } } }
Note
Definition

Definition: Refactoring is the process of restructuring existing code without changing its external behavior. It is important because it helps keep code clean, organized, and easier to maintain, while making future changes less error-prone.

1. What is the main advantage of refactoring repeated code into a method?

2. Which of the following is NOT a benefit of using methods for repetitive tasks?

question mark

What is the main advantage of refactoring repeated code into a method?

Select the correct answer

question mark

Which of the following is NOT a benefit of using methods for repetitive tasks?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 3

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

bookRefactoring Loops into Methods

Glissez pour afficher le menu

When writing programs, you often encounter situations where similar or identical blocks of code appear in multiple places. This is especially common with loops that perform the same task, like printing every element in an array. Such repetition can make your code harder to read, maintain, and update. Instead of copying and pasting the same loop in different parts of your program, you can refactor the code by moving the repeated logic into a method. Refactoring means restructuring your code without changing its external behavior, making it cleaner and more organized.

Program.cs

Program.cs

Program.cs (refactored)

Program.cs (refactored)

copy
1234567891011121314151617181920212223
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 2, 4, 6, 8 }; // Print elements (first time) for (int i = 0; i < numbers.Length; i++) { System.Console.WriteLine(numbers[i]); } // Print elements (second time) for (int i = 0; i < numbers.Length; i++) { System.Console.WriteLine(numbers[i]); } } } }

Refactoring repeated code into methods helps you write programs that are easier to read and understand. When you give a descriptive name to a method, anyone reading your code can immediately tell what the method does without digging into the details of the loop. This also makes your code easier to maintain—if you need to change how you print the array, you only need to update the method in one place. Reducing repetition also lowers the chance of errors, since you avoid having to make the same change in multiple locations.

Program.cs

Program.cs

copy
12345678910111213141516171819202122
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] scores = { 10, 20, 30, 40 }; int total = SumArray(scores); System.Console.WriteLine("Sum: " + total); } public static int SumArray(int[] arr) { int sum = 0; for (int i = 0; i < arr.Length; i++) { sum += arr[i]; } return sum; } } }
Note
Definition

Definition: Refactoring is the process of restructuring existing code without changing its external behavior. It is important because it helps keep code clean, organized, and easier to maintain, while making future changes less error-prone.

1. What is the main advantage of refactoring repeated code into a method?

2. Which of the following is NOT a benefit of using methods for repetitive tasks?

question mark

What is the main advantage of refactoring repeated code into a method?

Select the correct answer

question mark

Which of the following is NOT a benefit of using methods for repetitive tasks?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 3
some-alt