0
I have the following function that searches in BD a user that satisfies the login and password passed by parameter:
public Usuario getUsuarioLogin(String pLogin, String pSenha) {
String hql = " select u from Usuario u "
+ " where u.login =:pLogin "
+ " and u.senha =:pSenha ";
Query query = this.getManager().createQuery(hql);
query.setParameter("pLogin", pLogin);
query.setParameter("pSenha", pSenha);
return (Usuario) query.getSingleResult();
}
When I pass login and password that exist in BD is fine, but what will return if the search comes up empty, I tried to compare the returned value with null, but he did not recognize the condition
It’s usually bad to return
null
(Uncle Bob, in his book Clean Code, is one who comments on it), you force an unnecessary check on another(s) point(s) of the program and create a potential bug point. Much better would be, in thecatch
, simply return aUsuario
emptiness.– StatelessDev
Yes, forces an external check, in this specific case I think the best would be to return a Boolean reporting success or failure, would not have the risk of an NPE or checks at all points that the code is used
– Denis Rudnei de Souza