In Java, I've to set a POJO class with values. However to decide which setter function to be used, I've to depend on if
condition. My current code looks as follow:
// Code written in a function which is called within a loop, while parsing xml file.
if (name.equals("dim1")) {
line.setDim1Code(Integer.parseInt(value));
} else if (name.equals("dim2")) {
line.setDim2Code(Integer.parseInt(value));
} else if (name.equals("debitcredit")) {
line.setDebitOrCredit(value);
} else if (name.equals("basevalue")) {
line.setBasevalue(Integer.parseInt(value));
} else if (name.equals("rate")) {
line.setRate(Integer.parseInt(value));
} else if (name.equals("value")) {
line.setValue(Integer.parseInt(value));
} else if (name.equals("description")) {
line.setDescription(value);
} else if (name.equals("vatbasetotal")) {
line.setVatBaseTotal(value);
} else if (name.equals("vattotal")) {
line.setVatTotal(value);
}
This is just an example but I've 70+ such properties to be set. My code is working but I wonder if it is right way of doing things?
AFAIK, such code is against coding best practices. How can we optimise this code in Java? What is Java best practice to deal with such code?