DRY Principle
Don't Repeat Yourself Principle
Example of "exactly the same"
class UserService {
public void createUser(String email, String password) {
if (isEmailValid(email)) {
// create user
}
}
private boolean isEmailValid(String email) {
String pattern = "email pattern";
Pattern pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
}
class SubscriptionService {
public void subscribe(String topic, String email) {
if (isEmailValid(email)) {
// subscribe
}
}
// By copying and pasting an existing class,
// we have introduced some duplicated code in the codebase.
private boolean isEmailValid(String email) {
String pattern = "email pattern";
Pattern pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
}Example of "similar to"
Example of "duplication of implementation"
Last updated