Announcement Announcement Module
No announcement yet.
Assert.* methods that return values Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Assert.* methods that return values

    I believe it'd be great to change signature of the spring's Assert.* static methods so each of them will return the checked value.
    So the verified code would look as follows:

    this.a = Assert.notNull(a, "a can't be null");
    this.s = Assert.hasLength(s);
    or even shorter when using static imports:

    this.a = notNull(a, "a can't be null");
    this.s = hasLength(s);
    to make code even more nicer the method Assert.nullable(T verifiedValue, T... optionals) can be introduced, that takes verified value and an array of optional values that will be provided in place of verified value if it is null.

    So the code
    this.u = nullable(v, "Hello");
    will set u as v if v is not null and set it to "Hello" if it is.

  • #2
    No please not... That is not what a assertion is for... If you want to return a default or other value when something is empty use commons-lang (for instance StringUtils) which has methods for that.

    Assertions are to check if the input is valid if not abort the processing/throw exception ...


    • #3
      Originally posted by Marten Deinum View Post
      No please not... That is not what a assertion is for...
      I'm agree on nullable method, it looks alien in the Assert class.
      But for returning values in the Assert methods - why not? This wouldn't break the existing code and yet introduce convenient way of use Assert methods in initializers.


      • #4
        To request changes open a JIRA...

        But for me checking valid values and assignment are different things, but that is probably personal preference...


        • #5
          I've raised improvement against spring core:

          As for intentionally splitting checking and assignment - the proposed improvement allows keeping the old style for those who likes it, so I believe it wouldn't be a problem.