-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdry-principle.java
More file actions
178 lines (148 loc) · 4.9 KB
/
dry-principle.java
File metadata and controls
178 lines (148 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// DRY Principle - Don't Repeat Yourself
// Write code once, use it many times
// ======= EXAMPLE 1: DUPLICATE METHODS =======
// BAD - Same code repeated
class BadCalculator {
public int addTwoNumbers(int a, int b) {
System.out.println("Adding numbers...");
int result = a + b;
System.out.println("Result: " + result);
return result;
}
public int addThreeNumbers(int a, int b, int c) {
System.out.println("Adding numbers...");
int result = a + b + c;
System.out.println("Result: " + result);
return result;
}
}
// GOOD - No repetition
class GoodCalculator {
public int add(int... numbers) {
System.out.println("Adding numbers...");
int result = 0;
for (int num : numbers) {
result += num;
}
System.out.println("Result: " + result);
return result;
}
}
// ======= EXAMPLE 2: DUPLICATE VALIDATION =======
// BAD - Same validation repeated
class BadUserService {
public void createUser(String email) {
if (email == null || email.isEmpty() || !email.contains("@")) {
throw new IllegalArgumentException("Invalid email");
}
System.out.println("User created with email: " + email);
}
public void updateUser(String email) {
if (email == null || email.isEmpty() || !email.contains("@")) {
throw new IllegalArgumentException("Invalid email");
}
System.out.println("User updated with email: " + email);
}
}
// GOOD - Validation extracted to method
class GoodUserService {
private void validateEmail(String email) {
if (email == null || email.isEmpty() || !email.contains("@")) {
throw new IllegalArgumentException("Invalid email");
}
}
public void createUser(String email) {
validateEmail(email);
System.out.println("User created with email: " + email);
}
public void updateUser(String email) {
validateEmail(email);
System.out.println("User updated with email: " + email);
}
}
// ======= EXAMPLE 3: DUPLICATE CONSTANTS =======
// BAD - Same values repeated
class BadConstants {
public void processOrder() {
double tax = 0.15; // Tax rate repeated
System.out.println("Processing with tax: " + tax);
}
public void calculateTotal() {
double tax = 0.15; // Same tax rate repeated
System.out.println("Calculating with tax: " + tax);
}
}
// GOOD - Constant defined once
class GoodConstants {
private static final double TAX_RATE = 0.15;
public void processOrder() {
System.out.println("Processing with tax: " + TAX_RATE);
}
public void calculateTotal() {
System.out.println("Calculating with tax: " + TAX_RATE);
}
}
// ======= EXAMPLE 4: DUPLICATE TEXT/STRINGS =======
// BAD - Same error messages repeated
class BadValidationService {
public void validateAge(int age) {
if (age < 18) {
throw new IllegalArgumentException("Age must be 18 or older to register");
}
}
public void checkUserAge(int age) {
if (age < 18) {
throw new IllegalArgumentException("Age must be 18 or older to register");
}
}
public void verifyAge(int age) {
if (age < 18) {
throw new IllegalArgumentException("Age must be 18 or older to register");
}
}
}
// GOOD - Error messages as constants
class GoodValidationService {
private static final String AGE_ERROR_MESSAGE = "Age must be 18 or older to register";
private static final int MIN_AGE = 18;
public void validateAge(int age) {
if (age < MIN_AGE) {
throw new IllegalArgumentException(AGE_ERROR_MESSAGE);
}
}
public void checkUserAge(int age) {
if (age < MIN_AGE) {
throw new IllegalArgumentException(AGE_ERROR_MESSAGE);
}
}
public void verifyAge(int age) {
if (age < MIN_AGE) {
throw new IllegalArgumentException(AGE_ERROR_MESSAGE);
}
}
}
/*
DRY Principle - Simple Summary:
What is DRY?
- Don't Repeat Yourself
- Write code once, use it everywhere
- If you copy-paste code, you're probably doing it wrong
Types of Duplication:
1. Duplicate Methods - Same logic in different methods
2. Duplicate Validation - Same checks repeated
3. Duplicate Constants - Same values written multiple times
4. Duplicate Text/Strings - Same messages repeated
5. Duplicate Error Messages - Same error text everywhere
How to Fix:
- Extract common code into methods
- Use constants for repeated values
- Create helper methods for common operations
- Use loops instead of repeating similar code
Benefits:
- Easier to maintain (change once, fixes everywhere)
- Less bugs (no inconsistent copies)
- Smaller code size
- Easier to understand
Remember: If you change the same thing in multiple places,
you violated DRY principle!
*/