2
\$\begingroup\$
  • 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.

asked Aug 3, 2023 at 21:10
\$\endgroup\$
0

2 Answers 2

4
\$\begingroup\$

I'm not sure what you're getting at about it seeming odd. I suppose the various if/throws are repetitive and could be reduced by writing a function for them.

private void requireNotNull(boolean requirementMet, String errorMessage) {
 if (requirementMet == null) throw CustomHandledException(errorMessage);
}
// or, depending on whether the messages require any calculations:
private void requireNotNull(boolean requirementMet, Supplier<String> lazyErrorMessage) {
 if (requirementMet == null) throw CustomHandledException(lazyErrorMessage.get());
}

This is getting really nitpicky, but I think the logic flow would read slightly easier if you handle the non-null case of getUserResponse first so you don't have a big chunk of code in between retrieving something and working with that retrieved thing.

public void exampleMethod(final UserDto user, String sthElse) {
 requireNotNull(user.getId(), "example");
 requireNotNull(sthElse, "example");
 GetUserResponseDto getUserResponse = client.getUserById(user.getId(), country);
 if (getUserResponse != null) { 
 Locale currentLocale = getUserResponse.getUser().getProfile().getLocale();
 if (!user.getLocale().equals(currentLocale)) {
 client.updateUser(user);
 }
 } else {
 requireNotNull(user.getGivenName(), "example");
 requireNotNull(user.getEmail(), "example");
 CreateUserRequestDto createUserBody = UserMapper.toCreateBody(user);
 client.createUser(createUserBody);
 }
}

Aside from that, I wouldn't start variable names with verbs.

answered Aug 3, 2023 at 21:29
\$\endgroup\$
2
  • \$\begingroup\$ No need to write requireNontNull yourself. Java comes with that: docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/… \$\endgroup\$ Commented Aug 9, 2023 at 14:41
  • 1
    \$\begingroup\$ @RoToRa, depends on whether they specifically need that "CustomHandled" exception. \$\endgroup\$ Commented Aug 9, 2023 at 15:20
2
\$\begingroup\$

Separate into any function

private void validateInputs(final UserDto user, String sthElse) {
 if (user.getId() == null || sthElse == null) {
 throw new CustomHandledException("example");
 }
 
 if (user.getGivenName() == null || user.getEmail() == null) {
 throw new CustomHandledException("example");
 }
}
private void handleUserCreation(final UserDto user) {
 CreateUserRequestDto createUserBody = UserMapper.toCreateBody(user);
 client.createUser(createUserBody);
}
private void handleUserUpdate(final UserDto user, GetUserResponseDto getUserResponse) {
 if (!user.getLocale().equals(getUserResponse.getUser().getProfile().getLocale())) {
 client.updateUser(user);
 }
}

And then combine it in:

public void exampleMethod(final UserDto user, String sthElse) {
 validateInputs(user, sthElse);
 GetUserResponseDto getUserResponse = client.getUserById(user.getId(), country);
 if (getUserResponse == null) {
 handleUserCreation(user);
 } else {
 handleUserUpdate(user, getUserResponse);
 }
}
answered Aug 4, 2023 at 10:02
\$\endgroup\$
2
  • \$\begingroup\$ thanks for the support, definitely gave me some insights \$\endgroup\$ Commented Aug 4, 2023 at 18:58
  • 1
    \$\begingroup\$ I suggest avoiding weak words like "handle" in method names. It doesn't add any meaning to the name, so it becomes a hindrance to readability. These could be "createUser" and "updateUser". \$\endgroup\$ Commented Aug 9, 2023 at 15:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.