Q:

How to refactor this code?

Please suggest in refactoring this code.
Avoid code duplication, mutiple If's
public FormDataDTO getDataForFieldFormCHrzntalField(Field field) {
        FormDataDTO formDataDTO = new FormDataDTO();
        CHrzntalField cHrzntalField = (CHrzntalField) field;
        for (int j = 0; j < cHrzntalField.getFieldCount(); j++) {
            Field sField = cHrzntalField.getField(j);
            if (sField instanceof LabelField) {
                LabelField labelField = sField;
                String fieldName = labelField.getText();
                System.out.println("The Label field name is " + fieldName);
                formDataDTO.setFieldName(fieldName);
            } else if (sField instanceof CTextFieldBorder) {
                CTextFieldBorder cTextFieldBorder = (CTextFieldBorder) sField;
                Field ssField = cTextFieldBorder.getField(0);
                if (ssField instanceof TextField) {
                    TextField textField = ssField;
                    System.out.println("Inside TextField---- "
                            + textField.getText());
                    formDataDTO.setFieldType("TextField");
                    formDataDTO.setSelectedValue(textField.getText());
                } else if (ssField instanceof DateField) {
                    DateField dateField = ssField;
                    String dateString = dateField.toString();
                    System.out.println("dateString " + dateString);
                    formDataDTO.setFieldType("Date");
                    formDataDTO.setSelectedValue(dateString);
                }
            } else if (sField instanceof CChoiceField) {
                CChoiceField cChoiceField = (CChoiceField) sField;
                int i = cChoiceField.getSelectedIndex();
                String selectedValue = cChoiceField.getChoice(i);
                System.out.println("Choice " + selectedValue);
                formDataDTO.setFieldType("Combo");
                formDataDTO.setSelectedValue(selectedValue);
            } else if (sField instanceof CheckboxField) {
                CheckboxField checkboxField = (CheckboxField) sField;
                boolean checkStatus = checkboxField.getChecked();
                System.out.println("Check box field " + checkStatus);
                formDataDTO.setFieldType("Checkbox");
                String status = new Boolean(checkStatus).toString();
                formDataDTO.setSelectedValue(status);
            }
        }
        return formDataDTO;
    }

A:

First step is to create a unit test verifying the behavior of this method. Secondly, "Tell, don't ask" is a principle of good OO design, so it would be good if you could refactor the Field type and its subclasses, to implement a method that allows them to set the necessary information on the FormDataDTO.

A:

You could start by pulling each case block (the code inside the if / else if blocks) into their own methods.  There isn't a lot of repetition that I can see, it's just trying to do too much in one method.

