Quantcast
Channel: Why should Java 8's Optional not be used in arguments - Stack Overflow
Viewing all articles
Browse latest Browse all 29

Answer by Eliezer Garza for Why should Java 8's Optional not be used in arguments

$
0
0

after a lot of debate at my new posting we came with this conventionfor public methods:

  • a primitive means not null (obvious)
  • an Object Backed by a primitive means it's nullable
  • even if it's not a Validated class annotate the params with @NotNull and @NotBlank when required.
  • if a spring component use the @Validated annotation as well
  • you are free to wrap the parameter with Optional<> inside the method call.
   // response will never be null   public @NotNull String methodCall(      int startValue, // required      Integer endValue, // not required      BigDecimal minimum, //not required      @NotNull BigDecimal maximum, //required      @NotBlank String username, //required, not blank      String comment //not required   ){    return String.format("startValue:%d, endValue:%d, minimum:%s, maximum:%s, username:%s, comment:%s", startValue, endValue, minimum, maximum, username, comment);      }

Viewing all articles
Browse latest Browse all 29

Trending Articles