Method that concatenates strings, some of which may be null, with several different separators

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












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: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR Nº 16/2018

  • modeloVotacao = null: PROJETO DE LEI COMPLEMENTAR Nº 16/2018

  • tipoProposicao = null: 1ª DISCUSSÃO - Nº 16/2018

  • ordemNumero = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR 2018

  • ordemAno = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR Nº 16

  • all null: empty string

  • modeloVotacao and tipoProposicao = null: Nº 16/2018

  • ordemNumero and ordemAno = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR






share|improve this question

















  • 1




    Would help a lot if the variable names were in English.
    – slowy
    Jan 26 at 17:17
















up vote
2
down vote

favorite












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: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR Nº 16/2018

  • modeloVotacao = null: PROJETO DE LEI COMPLEMENTAR Nº 16/2018

  • tipoProposicao = null: 1ª DISCUSSÃO - Nº 16/2018

  • ordemNumero = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR 2018

  • ordemAno = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR Nº 16

  • all null: empty string

  • modeloVotacao and tipoProposicao = null: Nº 16/2018

  • ordemNumero and ordemAno = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR






share|improve this question

















  • 1




    Would help a lot if the variable names were in English.
    – slowy
    Jan 26 at 17:17












up vote
2
down vote

favorite









up vote
2
down vote

favorite











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: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR Nº 16/2018

  • modeloVotacao = null: PROJETO DE LEI COMPLEMENTAR Nº 16/2018

  • tipoProposicao = null: 1ª DISCUSSÃO - Nº 16/2018

  • ordemNumero = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR 2018

  • ordemAno = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR Nº 16

  • all null: empty string

  • modeloVotacao and tipoProposicao = null: Nº 16/2018

  • ordemNumero and ordemAno = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR






share|improve this question













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: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR Nº 16/2018

  • modeloVotacao = null: PROJETO DE LEI COMPLEMENTAR Nº 16/2018

  • tipoProposicao = null: 1ª DISCUSSÃO - Nº 16/2018

  • ordemNumero = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR 2018

  • ordemAno = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR Nº 16

  • all null: empty string

  • modeloVotacao and tipoProposicao = null: Nº 16/2018

  • ordemNumero and ordemAno = null: 1ª DISCUSSÃO - PROJETO DE LEI COMPLEMENTAR








share|improve this question












share|improve this question




share|improve this question








edited Jan 27 at 14:38









200_success

123k14143401




123k14143401









asked Jan 26 at 16:57









aseolin

1111




1111







  • 1




    Would help a lot if the variable names were in English.
    – slowy
    Jan 26 at 17:17












  • 1




    Would help a lot if the variable names were in English.
    – slowy
    Jan 26 at 17:17







1




1




Would help a lot if the variable names were in English.
– slowy
Jan 26 at 17:17




Would help a lot if the variable names were in English.
– slowy
Jan 26 at 17:17










1 Answer
1






active

oldest

votes

















up vote
1
down vote













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).






share|improve this answer























    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186064%2fmethod-that-concatenates-strings-some-of-which-may-be-null-with-several-differ%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    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).






    share|improve this answer



























      up vote
      1
      down vote













      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).






      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        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).






        share|improve this answer















        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).







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jan 27 at 14:40


























        answered Jan 26 at 19:37









        200_success

        123k14143401




        123k14143401






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186064%2fmethod-that-concatenates-strings-some-of-which-may-be-null-with-several-differ%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?