0

I want to check input values for methods and I dont want to write code like this:

void name(String name) {
    if(name != null) {
        //some action
    } else {
        //some another action
    }
}

So I can use decorator pattern or annotations.

Decorator example:

public interface User {
    void name(String name);
} 

public final class dbUser {
    //ctor...

    void name(String name) {
        //jdbc call for update user name
    }

}

public final class CheckedNullUser {
    private final CheckedNullUser user;

    public CheckedNullUser(User user) {
        this.user = user;
    }

    void name(String name) {
        String inputName = "Some default value";

        if(name != null) {
            inputName = name;
        }

        return user.name(inputName);
    }

}

Annotation example:

public interface User {
    void name(@NotNull String name);
} 

public final class dbUser {
    //ctor...

    void name(@NotNull String name) {
        //jdbc call for update user name
    }

}

Which approach is better? Exist better approaches?

  • What about using Objects.requireNonNull(name) as the first statement in the dbUser.name method? – toongeorges Feb 28 '17 at 12:57
  • @RomcoBomco Your *Decorator* example is not an example of *Decorator* but an example of simple delegation. You show a class called `dbUser` but never use it in your code. Your pseudo code doesn't make sense without the missing components. – Chetan Kinger Feb 28 '17 at 14:45
  • @CKing My code should look like new CheckedNullUser(new dbUser(//ctor args)).name("CKing"); so CheckedNullUser class decorates dbUser class. – RomcoBomco Mar 01 '17 at 10:05

2 Answers2

0

Which one is better? It will depend on your requirements and the behavior of your application.

Using decorator pattern generates several other objects just for validation, thus more work for the garbage collector. (if yous application generates too many of those objects you might start to see executions of the "stop-the-world" garbage collector, which will cause freezes).

Using annotations (Bean Validation - JSR 349) leverages reflection, so if your application has a near-real-time requirement, this might slow things down.

From those 2, I particularly prefer the Annotations approach, since the code gets easier to understand and simpler to find the checks that will be performed on each field.

https://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/index.html

Now about other approaches, you can create a marker Interface that simply contains a single method like:

boolean validate() throws ValidationException

Then your objects should implement this interface and before running your object you make a call to the validate method that performs all the checks you want.

Boschi
  • 622
  • 3
  • 10
0

@NotNull annotations are doing nothing itself. It's not the way to handle the null check. It's rather a way of making documentation. Additionally, most IDE's will inform you about potential null pointer errors. When It comes to the null check handling, you should take a look at this question link. When it comes to business - logic related validation I think you would be better off checking it in the first place. If the logic is too complex you can create some auxiliary service for handling it.

Community
  • 1
  • 1
Bari
  • 328
  • 1
  • 8