- In a project, we use service methods, there, we have to validate certain required inputs for our business logic to work:
public void exampleMethod(final UserDto user, String sthElse) {
if (user.getId() == null) {
throw new CustomHandledException("example");
}
if (sthElse == null) {
throw new CustomHandledException("example");
}
GetUserResponseDto getUserResponse = client.getUserById(user.getId(), country);
if (getUserResponse == null) {
if (user.getGivenName() == null) {
throw new CustomHandledException("example");
}
if (user.getEmail() == null) {
throw new CustomHandledException("example");
}
CreateUserRequestDto createUserBody = UserMapper.toCreateBody(user);
client.createUser(createUserBody);
return;
}
if (user.getLocale().equals(getUserResponse.getUser().getProfile().getLocale())){
return;
}
client.updateUser(user);
}
Even though the code isn't necessarily wrong and is quite understandable (for me) I feel like there is something odd there, do you have any suggestions for a cleaner code?
I tried to write a clean code, however, doesn't feel like it is clean.