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
Post a Comment