There is a more elegant solution to this, which would be solved in your modeling through inheritance/polymorphism, you could model a super class, for example the class Person, taking as subclasses (inheritance) PessoaFisica
and PessoaJuridica
, in the superclass you could have the method getNomeFormatado
, or getDescricao
for example, returning the correct text, properly formatted to clade object by type... this is the concept of polymorphism.
Pessoa p1 = new PessoaFisica(); // retorna em getNomeFormatado() o valor de getNome().
Pessoa p2 = new PessoaJuridica(); // retorna o valor de getNomeFormatado() o valor de getRazaoSocial().
No ifs, no gambiarras, just good modeling, good application design. Another less elegant option, however, that would work perfectly would be to have a method getNomeFormatado()
no inheritance or polymorphism within the method:
contato.tipoCadastro.codigo == '1' ? contato.getNome() : contato.getRazaoSocial();
I hope I’ve helped.