Reputation: 33
To refactor multiple || and && conditions inside If statement in java
To refactor multiple || and && conditions inside If statement in java
To refactor multiple || and && conditions inside If statement in java
for (Student students : studentList) {
if ((Constants.CODE1.equals(students.getactivities().getCode())
&& ValidationRepository.validateStudentId1(department.getId()))
|| ((Constants.CODE2).equals(students.getactivities().getCode())
&& ValidationRepository.validateStudentId2(department.getId())
&& ValidationRepository.validateStudentId3(department.getId()))
|| ((Constants.CODE3
.equals(students.getactivities().getCode())
|| Constants.CODE4
.equals(students.getactivities().getCode()))
&& (ValidationRepository.validateStudentId4(
department.getId(),
students.getactivities().getCode())))
|| ((Constants.CODE5.equals(students.getactivities().getCode())
|| Constants.CODE6
.equals(students.getactivities().getCode())
|| Constants.CODE7
.equals(students.getactivities().getCode())
|| Constants.CODE15
.equals(students.getactivities().getCode())
|| Constants.CODE8
.equals(students.getactivities().getCode())
|| Constants.CODE9
.equals(students.getactivities().getCode())
|| Constants.CODE10
.equals(students.getactivities().getCode())
|| Constants.CODE11
.equals(students.getactivities().getCode()))
&& (ValidationRepository.validateStudentId4(
department.getId(),
students.getactivities().getCode())))
)
{
some statements
}
Upvotes: 1
Views: 1442
Reputation: 126
Create a method to validate student Inside the method create a map of Constant code and validation repo function
public boolean validateStudent(String constantCode, departmentId){
Map<String, Boolean> map = new HashMap<>();
map.put(Constants.CODE1,
ValidationRepository.validateStudentId1(departmentId));
map.put(Constants.CODE2,
ValidationRepository.validateStudentId2(departmentId));
if(map.containsKey(constantCode)){
return map.get(operation);
}
return false;
}
Now you can call validate method like below
validateStudent(constantCode, departmentId)
Internally the map value for the constant code key will be your function. Make sure you use the same parameter name of method and key value in map.
Upvotes: 1
Reputation: 1
Let me give you a piece of advice:
listOf(Constants. CODE7, Constants.CODE8, Constants.CODE9 ...).contains(students.getactivities().getCode());
private validateStudentWithCode1(Students students)
{
return Constants.CODE1.equals(students.getactivities().getCode())
&& ValidationRepository.validateStudentId1(department.getId());
}
Upvotes: 0
Reputation: 209
This question has no clear answer, but I have the following suggestions:
students.getactivities().getCode()
every single time. Not only does it make the code less readable, it is also ineffective. (Unless this method does not return the same value every time!)
Rather use a variable before the if-statement:int code = students.getactivities().getCode();
Then, you can just refer to that variable at any time. The same can be done for department.getId()
.
private boolean matchesAny(int code, int... possibleMatches) {
for (int match: possibleMatches) {
if (code == match) return true;
}
return false;
}
That way, your code will look like this:
int departmentId = department.getId();
for (Student students : studentList) {
int code = students.getactivities().getCode();
// Case 1
if ((Constants.CODE1.equals(code)
&& ValidationRepository.validateStudentId1(departmentId))
// Case 2
|| (Constants.CODE2.equals(code)
&& ValidationRepository.validateStudentId2(departmentId)
&& ValidationRepository.validateStudentId3(departmentId))
// Case 3
|| (matchesAny(code, Constants.CODE3, Constants.CODE4)
&& ValidationRepository.validateStudentId4(departmentId, code))
// Case 4
|| (matchesAny(code,
Constants.CODE5,
Constants.CODE6,
Constants.CODE7,
Constants.CODE8,
Constants.CODE9,
Constants.CODE10,
Constants.CODE11,
Constants.CODE15)
&& ValidationRepository.validateStudentId4(departmentId, code))) {
// some statements
}
}
// rest of method
private boolean matchesAny(int code, int... possibleMatches) {
for (int match : possibleMatches) {
if (code == match) return true;
}
return false;
}
Still not beautiful, but already a lot better. To make it even better, you might try and turn the 4 cases into separate statements and have them all call the same code afterwards.
Upvotes: 0