0

I saw an example where the following syntax was used in a customer validator inside a LoginCommand object.

password blank:false, validator: { val, cmd ->
     if(cmd.user && cmd.user.password != val) 
         return "user.password.invalid" 
}

My understanding is that here in the if clause, we are checking two things. First, that a user exists, and second, that the password of the user matches the password held by the LoginCommand. Doesn't it seem redundant to check if the user exists? I mean if the user didn't exist wouldn't cmd.user.password be null and hence the test would fail? Why is the user check necessary?

And if it is necessary, then can't we encapsulate the first user existence check in the second check using this syntax cmd.user?.password != val?

1 Answer 1

2

I mean if the user didn't exist wouldn't cmd.user.password be null and hence the test would fail?

No. If the user didn't exist cmd.user.password would throw a NullPointerException.

And if it is necessary, then can't we encapsulate the first user existence check in the second check using this syntax cmd.user?.password != val?

You could. It is just a matter of style. Do whichever you think is more readable.

Sign up to request clarification or add additional context in comments.

1 Comment

I see, yes from a readability perspective, it would make more sense to use the notation used as I saw it, especially for a newcomer to the framework. Thanks.

Your Answer

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.