There is a more elegant solution to this, which would be solved in its modeling through inheritance / polymorphism, you could model a super class, for example the Person class, having 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 clada 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().
Without ifs, without gambiarras, only good modeling, a good design of the application. Another less elegant option, however that would work perfectly would be to have a getNomeFormatado()
method with no inheritance or polymorphism to check inside the method:
contato.tipoCadastro.codigo == '1' ? contato.getNome() : contato.getRazaoSocial();
I hope I have helped.