2
\$\begingroup\$

I'd like to refactor a Java method I've created, making it cleaner and more beautiful. The method concatenates some strings, which may be empty or not. I'm new on java so i'm not sure how to do that, especially because between the strings I may have separators.

Here is my method:

private String createString(String modeloVotacao, String tipoProposicao, String ordemNumero, String ordemAno) { 
 String modeloDescricao = "";
 if(modeloVotacao != null) {
 modeloDescricao = modeloVotacao + " - ";
 }
 if(tipoProposicao != null) {
 modeloDescricao += tipoProposicao + " ";
 }
 modeloDescricao += ordemNumero != null?ordemNumero:"";
 if(ordemNumero != null && ordemAno != null) {
 modeloDescricao += "/"+ordemAno;
 } else {
 if(ordemAno != null) {
 modeloDescricao += ordemAno;
 }
 }
 return modeloDescricao;
}

Here is some results that this method returns:

  • All filled: 1a DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR No 16/2018
  • modeloVotacao = null: PROJETO DE LEI COMPLEMENTAR No 16/2018
  • tipoProposicao = null: 1a DISCUSSÃO - No 16/2018
  • ordemNumero = null: 1a DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR 2018
  • ordemAno = null: 1a DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR No 16
  • all null: empty string
  • modeloVotacao and tipoProposicao = null: No 16/2018
  • ordemNumero and ordemAno = null: 1a DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR
200_success
146k22 gold badges190 silver badges479 bronze badges
asked Jan 26, 2018 at 16:57
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Would help a lot if the variable names were in English. \$\endgroup\$ Commented Jan 26, 2018 at 17:17

1 Answer 1

1
\$\begingroup\$

The repeated task here is that you want to conditionally insert a separator between two strings if both are non-null. So, write a helper function that does that:

private static String join(String a, String sep, String b) {
 return (a == null) ? b :
 (b == null) ? a : a + sep + b;
}

Then, the code becomes simple:

private String createString(String modeloVotacao, String tipoProposicao, String ordemNumero, String ordemAno) { 
 String description = join(modeloVotacao, " - ",
 join(tipoProposicao, " ",
 join(ordemNumero, "/", ordemAno)));
 return (description == null) ? "" : description;
}

Note that in your final example, where ordemNumero and ordemAno are both null, the output ends with a space. That does not happen in my solution; I consider that to be an improvement.

Please avoid mixing Portuguese with English identifiers (such as createString()). Stick with one or the other (preferably English).

answered Jan 26, 2018 at 19:37
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.