Created
January 17, 2024 10:54
-
-
Save omernaci/2d5b11087839781fe84909a1c717eb9c to your computer and use it in GitHub Desktop.
Cyclomatic Complexity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reduce complexity of the code | |
public boolean isOrderValid(Order order) { | |
if (order != null) { | |
if (order.getItems() != null && !order.getItems().isEmpty()) { | |
for (OrderItem item : order.getItems()) { | |
if (item.getQuantity() > 0) { | |
// Validate item | |
} else { | |
return false; | |
} | |
} | |
return true; | |
} else { | |
return false; | |
} | |
} else { | |
return false; | |
} | |
} | |
// Refactored version | |
public boolean isOrderValid(Order order) { | |
if (order == null || order.getItems() == null || order.getItems().isEmpty()) { | |
return false; | |
} | |
for (OrderItem item : order.getItems()) { | |
if (item.getQuantity() <= 0) { | |
return false; | |
} | |
// Validate item | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment