Mr Carl
Mr Carl

Reputation: 187

List objects - best practice

So there is a new guy that has started where I work. I'm quite a junior programmer myself, but I've been developing for a bit longer, so I'm using my "gut feeling" to guide us in developing a project. The blind leading the blind.

A relatively small thing just came up, and I would like to know what is the best way to create this for future reference and why.

Essentially there is a basic XML file with details for files (structure isn't really relevant). He went about querying this XML file and then storing all retrieved files by creating several lists, something like so:

List<Integer> fileId = new List<Integer>;
List<String> title = new List<String>;

And then you would create a method which would query against these Lists looking for the ID.

I pictured a method would be created to query for a file out of the XML file without storing/setting anything, like so:

public Form getFile(Integer id) {
  Form ret = new Form();
  //Query XML, set to ret...
  return ret;
}

I wanted to use value objects, since that's how I'm used to working. So suggested and settled for this in the end:

List<Form> forms = new List<Form>;

So now, we have 2 methods, 1 to populate the 'forms' variable, and then 1 to query it and return the Form... still seems very strange to me.

Also, instead of:

Form tempForm = new Form();
tempForm.id = 1;
tempForm.title = "Foo";

He prefers to do:

Form tempForm = new Form(id, title);

Purely because it's in 1 line and looks tidier. Later down the line though, I don't think using a value object like this is the best way to go.

Maybe I am worrying and thinking about stuff to much as opposed to getting on with development, but any advice on this would be great.

Upvotes: 2

Views: 870

Answers (3)

vidstige
vidstige

Reputation: 13079

Interresting question! There are however several questions in one. Let me answer each one of them separately.

Let me first lay down the definition of a value type as found on domaindrivendesign.org

Definition: A Value Object is an object that describes some characteristic or attribute but carries no concept of identity.

For example a file path is a string, but it also has some restrictions on the format of the string and some operations. Here it would be a good idea to create a value object. Note also that a path carries no notation of identity. That is, two path objects representing the same path would be considered equal.

Now to the actual question, I strongly recommend your way of coding - Creating a class for data that belong together. In your first example id and title are only related by an index into two separate lists.

It's better to use this form

Form tempForm = new Form(id, title);

That way the Form class can be immutable which will give you great readability benefits and also performance gains. Also the fields of the class are encapsulated.

Now to the last thing you thought was strange - Having two methods, one for creating the list and one for querying against it.

Here I would actually create a new class, containing only those two methods instead of having them say in a static class. I would call it a FormCollection. You guys can probably come up with some smarter name since you have more context. Spend at most five minutes figuring out a meaningful name.

You could also refactor your code further to for example take the xml file path or stream as a constructor argument and then have a single method for querying aginst it on id. Like so:

class FormCollection
{
    public FormCollection(String xmlFilePath) { ... }
    public Form getById(int id) { ... }
}

This is probably a good interface to the rest of your application, since it easy and to the point. Also it's easy to test.

Upvotes: 1

Eric B.
Eric B.

Reputation: 24411

I'm not sure I understand your question properly, but at the basis, it sounds like a performance question. ie: is it worth reading in an entire XML file, and restructuring it such that it is faster and easier to query, or is it better to scan the xml file every time and query against it. That's a question that only you can answer. As usual, it's the space-speed tradeoff that you have to evaluate.

If your XML file is huge and would require significant amount of memory to cache and you only query against in sporadically, then perhaps your solution is better. If it is small and speed is critical, then caching it is a good idea.

All that being said, there are several different libraries that you can use to speed up the processing in different ways. You can look at using XQuery and/or XPath (see How to read XML using XPath in Java), JAXB, SAX, etc. Each technology has its own advantages and disadvantages.

Hopefully that will give you a little more background that you can discuss with each other.

Upvotes: 1

Jamie McCrindle
Jamie McCrindle

Reputation: 9214

On your second style question:

One of the reasons to use a constructor is that you can then make your Form object immutable as in:

public class Form {
  private final String id;
  private final String title;
  public Form(String id, String title) {
    this.id = id; this.title = title;
  }
  public String getTitle() { return title; }
  public String getId() { return id; }
}

This helps avoid concurrency issues.

Upvotes: 2

Related Questions