Junior — Middle
Have you applied the principle of avoiding duplication in your code?
sobes.tech AI
Answer from AI
The principle of avoiding duplication (DRY — Don't Repeat Yourself) is about not repeating the same code or logic in different parts of the program. This helps reduce errors, makes maintenance easier, and improves code readability.
For example, if the same operation needs to be performed in multiple places, it's better to extract it into a separate method or class and call it from there, rather than copying the code.
Example:
// Bad — code duplication
int sum1 = a + b;
int sum2 = c + d;
// Better — method for addition
public int sum(int x, int y) {
return x + y;
}
int sum1 = sum(a, b);
int sum2 = sum(c, d);
Thus, when you need to change the addition logic, you only need to modify one method, not all places where the operation is performed.