views:

164

answers:

4

I have a readData() function that reads files a returns a few different objects of parsed data. Right now, the return type of readData() is Object[]:

Object[] data = readData();
MyGenome genome = data[0];
Species[] breeds = data[1];
//etc

This feels awkward. Is there a better way to return this data? I don't want to have separate functions like readGenome() and readSpecies() because that would require iterating over the files twice. Also, I'd rather wrap up all the gathering of data in one function.

A similar issue: a function that returns a match of at least four characters between two strings:

public int[][] findMatch(String g0, String g1) { /* cool stuff */ }
//...
int[][] bounds = findMatch("ACOIICOCOCICOICA", "AOCCCCCCICCIIIIIOIAOCICOICOICA");

where bounds[0][0] is the left bound on the g0, bounds[0][1] is the right bound on g0, bounds[1][0] is the left bound on g1, etc. This also feels sort of awkward. It is difficult to code with the result without continuously looking up the keys.

+6  A: 

Create a new Class:

class MyAnalysedGenome {
    MyGenome genome;
    Species[] species
    ...
}

and return that. You'll probably find you have other functionality that should go in there too. Perhaps the code that surrounds your getData() call.

Stephen
+4  A: 

How about using a strongly typed class to represent the complex return type of readData()?

public class Taxonomy
{
    public MyGenome genome;
    public Species[] breeds;
    //etc
{

Taxonomy data = readData();

You can do the same thing for your search bounds problem

public class SearchBoundary
{
    public int left;
    public int right;
}

SearchBoundary resultBounds = findMatch(searchBounds);
Ryan Michela
NEVER (and yes I mean ____NEVER____) do public without final. And, even for this simple class you should make the variables private. Also NEVER make a mutable thing (like an array, and possibly MyGenome) public.
TofuBeer
Hehe - let this be a lesson: note how I did not specify public *or* private and do not get flamed...People just assumed it was incomplete (which is was intended to be)
Stephen
NEVER (and yes I mean __NEVER__) strictly follow rules about what you should NEVER do. There are few rules that don't have exceptions. For example, passing pure data back and forth, internally, between two places completely controlled by your code... Avoiding the stupidity of getters and setters in such a situation is perfectly reasonable.
RHSeeger
is this a good place to use getters and setters?
Rosarch
A: 

For the first issue,couldn't you simply use an intermediate data representation ? I mean you could read your file once, which would give you the file content (that you could format the way you want), and then create two methods readGenome() and readSpecies() that would take this file content as a parameter.

LePad
A: 

You can create a class that have genome and species as fields.

...
class DataToBeRead {
    MyGenome  genome;
    Species[] breeds;
}
...


DataToBeRead data   = readData();
MyGenome     genome = data.genome;
Species[]    breeds = data.breeds;

You can make the class private if you do not think anybody else will used it or make it public if someone else will use it.

You can also make it static if you do not want to create a separate file for it.

Hope this helps.

NawaMan