HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

CSV to Object converter with JavaDoc comments

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javadocwithcsvobjectcommentsconverter

Problem

This is a simple class but I'm looking to check I have the basics down to scratch.

Is my documentation thorough enough? (I'm very new to writing JavaDoc) Have I created too many variables, and does this sacrifice performance for readability in a bad way?

This class is called once to create an array of TestDataObjects to use with our Parsing unit tests.

/**
 * Converts our test data csv in to an ArrayList
 * We divide the csv entries up line by line for each calendar event then create a TestData object from each line.
 * The fields we wish to parse have negated new line characters to ensure they don't interfere with the csv parsing.
 * Once the csv has been parsed we need to unnegate these so the test data is the same as the original data.
 * @param calendar The csv to convert
 * @return An Array List of TestData objects that have all the three fields we wish to parse and the correct results we wish to obtain.
 */
private ArrayList loadTestData(String calendar) {
    ArrayList testDataArray = new ArrayList<>();
    String[] lines = calendar.split("\r\n");

    // always skip lines[0] as it contains the headers
    for (int i = 1; i  fields = CSVReader.parseLine(lines[i]);
        String title = fields.get(0).replace("\\n", "\n");
        String location = fields.get(1).replace("\\n", "\n");
        String description = fields.get(2).replace("\\n", "\n");
        ArrayList correctHostCodes = CSVReader.parseLine(fields.get(3));
        ArrayList correctParticipantCodes = CSVReader.parseLine(fields.get(4));
        ArrayList correctPhoneNumbers = CSVReader.parseLine(fields.get(5));

        TestData testDataObject = new TestData(title + "\n" + location + "\n" + description, correctHostCodes, correctParticipantCodes, correctPhoneNumbers);
        testDataArray.add(testDataObject);
    }

    return testDataArray;
}

Solution

Prefer interfaces to implementations

private ArrayList loadTestData(String calendar) {
    ArrayList testDataArray = new ArrayList<>();


and

ArrayList fields = CSVReader.parseLine(lines[i]);


and

ArrayList correctHostCodes = CSVReader.parseLine(fields.get(3));
        ArrayList correctParticipantCodes = CSVReader.parseLine(fields.get(4));
        ArrayList correctPhoneNumbers = CSVReader.parseLine(fields.get(5));


You would normally write these as

private List loadTestData(String calendar) {
    List testDataArray = new ArrayList<>();


and

List fields = CSVReader.parseLine(lines[i]);


and

List correctHostCodes = CSVReader.parseLine(fields.get(3));
        List correctParticipantCodes = CSVReader.parseLine(fields.get(4));
        List correctPhoneNumbers = CSVReader.parseLine(fields.get(5));


That way, even if parseLine changes its return type, you don't have to change unless it changes to something that doesn't implement the List interface.

Range-based for loops

String[] lines = calendar.split("\r\n");

    // always skip lines[0] as it contains the headers
    for (int i = 1; i  fields = CSVReader.parseLine(lines[i]);


You could rewrite this as

List lines = Arrays.asList(calendar.split("\r\n"));

    // always skip lines[0] as it contains the headers
    for (String line : lines.sublist(1, lines.size())) {
        List fields = CSVReader.parseLine(line);


Or not. If you find the indexed form easier to follow, that works too.

Code Snippets

private ArrayList<TestData> loadTestData(String calendar) {
    ArrayList<TestData> testDataArray = new ArrayList<>();
ArrayList<String> fields = CSVReader.parseLine(lines[i]);
ArrayList<String> correctHostCodes = CSVReader.parseLine(fields.get(3));
        ArrayList<String> correctParticipantCodes = CSVReader.parseLine(fields.get(4));
        ArrayList<String> correctPhoneNumbers = CSVReader.parseLine(fields.get(5));
private List<TestData> loadTestData(String calendar) {
    List<TestData> testDataArray = new ArrayList<>();
List<String> fields = CSVReader.parseLine(lines[i]);

Context

StackExchange Code Review Q#139143, answer score: 3

Revisions (0)

No revisions yet.