Results 1 to 5 of 5

Thread: Assert.* methods that return values

Hybrid View

  1. #1
    Join Date
    Aug 2011
    Posts
    28

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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 ...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2011
    Posts
    28

    Default

    Quote 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. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    To request changes open a JIRA...

    But for me checking valid values and assignment are different things, but that is probably personal preference...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Aug 2011
    Posts
    28

    Default

    I've raised improvement against spring core: https://jira.springsource.org/browse/SPR-9828

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •