Readable way to concat many strings in java -


i have code written in cryptic fashion. sure going maintenance nightmare other me understand.

its mishmash of string concatenation, ternary operator , concatenating using + operator.

so question how make statement readable?

tb.settally_narration(      tb.gettally_mode().equals("ca") ?          "receipt no. "              .concat(tb.gettally_receipt_no())              .concat(", "+tb.gettally_mode()) :          "receipt no. "              .concat(tb.gettally_receipt_no()+", "+tb.gettally_mode())              .concat(", "+tb.gettally_instrument_no()+", "+tb.gettally_instrument_date()+", "+tb.gettally_instrument_bank()) ); 

edit: realize question subjective. , feel belongs codereview stackexchange site. moved there?

since first line of string appears same, write as:

stringbuilder narration = new stringbuilder("receipt no. ");  narration.append(tb.gettally_receipt_no())          .append(", ").append(tb.gettally_mode()); if (!"ca".equals(tb.gettally_mode())) {     narration.append(", ").append(tb.gettally_instrument_no())              .append(", ").append(tb.gettally_instrument_date())              .append(", ").append(tb.gettally_instrument_bank()); }  tb.settally_narration(narration.tostring()); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -