I am trying to create a library where I need to validate and parse a file in a CSV-like format and then use this data to generate a Tree data structure.

At the moment I split the process into two steps:

  1. Validate the file and store its lines in a list (in the parsing package)
  2. Read the list plus a string array containing a subset of columns to generate the tree (in the hierarchy package)

I was thinking if the steps can be simplified as a single one, since I would only need to store data just once (and there would be better performance) and I would store less data because of the columns subset. But I am concerned that the single step would result in a merged package that has too many responsibilities: validation and tree preparation.

What would be the best option in terms of best practice of software architecture?

  • From the Code Complete 2's perspective, several passes through the data where each pass is in its own loop is usually preferable. It produces code that's easier to read and maintain. – Nick Alexeev Jun 26 '19 at 21:53
  • Thanks @NickAlexeev for your answer. What if I created a friendly and readable API for my data structure in such a way that the "more complex" insertion logic is handled transparently in the Tree class? I am starting to think that validation should be done during the creation of the tree: javapractices.com/topic/TopicAction.do?Id=209 – Vektor88 Jun 26 '19 at 21:59
  • 1
    I tend to do validation in twice. (1) At the user input or data intake. The data haven't become model, yet. The purpose is to find discrepancies in the data and help users fix them. This is a friendly validation. (2) At the setters of the model. The purpose is to stop bad data from corrupting the model. This is not a friendly validation, and it will throw hard exceptions. The validation rules are such that if step (1) passes validation, then step (2) should pass also. (Similar conversation on GitHub.) – Nick Alexeev Jun 26 '19 at 22:30
  • @NickAlexeev this seems really similar to what I am trying to achieve. (1) I parse the input file and run a validation line by line. I can easily implement a Line class that calls "validate" method inside its constructor. The created Line is then passed to my Tree implementation to add it in the tree and in this level I have a (2) second validation before adding an element to the tree derived by Line class. In my question I was wondering if it's better to validate all Lines and collect them into a list that subsequently I add to the Tree, or directly populate the Tree right after validation. – Vektor88 Jun 27 '19 at 5:39

Do nothing, if you see no performance problems.

Otherwise make parser return Iterator instead of List, that way, you will only need enough memory to store a single line at cost of complicating parser's lifetime management.


I need to validate and parse a file in a CSV-like format and then use this data to generate a Tree data structure

Your process is inherently two-stage. Trying to merge these two separate things into one will certainly result in less legible and harder-to-maintain code. It is almost certainly not necessary to write everything as one giant package/class/function to get good time and space efficiency.

Your first stage is to import a .csv file and parse it into some table-esque data structure. It is possible to validate that your .csv is valid (check it's a text file, check it's comma delimited) and that it contains tabular data (do all rows have the same number of colums, etc.) without knowing or caring about what the data will later be used for.

Your second stage is to take tabular data (e.g. an array of arrays) and turn it into a tree. At this point, your hierarchy package will be doing validation but it will only be validating the tree structure (e.g. every node except the root has one parent, etc.). By this stage, you definitely don't care about invalid file formats or file-not-found errors.

If you want to save space (and I would question whether you really need to), you can write your hierarchy so that it frees each row of the tabular data as it builds the tree or it could modify the tabular data to represent a tree (see the heap data structure for an example of a tree represented as an array).

  • I feel like writing the data twice is suboptimal for larger datasets, but feel free to prove me wrong – Vektor88 Jun 28 '19 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.