views:

74

answers:

4

I've got an array with about 250 entries in it, each their own array of values. Each entry is a point on a map, and each array holds info for:

name, another array for points this point can connect to, latitude, longitude, short for of name, a boolean, and another boolean

The array has been written by another developer in my team, and he has written it as such:

names[0]=new Array;
names[0][0]="Campus Ice Centre";
names[0][1]= new Array(0,1,2);
names[0][2]=43.95081811364498;
names[0][3]=-78.89848709106445;
names[0][4]="CIC";
names[0][5]=false;
names[0][6]=false;

names[1]=new Array;
names[1][0]="Shagwell's";
names[1][1]= new Array(0,1);
names[1][2]=43.95090307839151;
names[1][3]=-78.89815986156464;
names[1][4]="shg";
names[1][5]=false;
names[1][6]=false;

Where I would probably have personally written it like this:

    var names = []
    names[0] = new Array("Campus Ice Centre", new Array[0,1,2], 43.95081811364498, -78.89848709106445, "CIC", false, false);
    names[1] = new Array("Shagwell's", new Array[0,1], 43.95090307839151, -78.89815986156464, 'shg", false, false);

They both work perfectly fine of course, but what I'm wondering is:

1) does one take longer than the other to actually process? 2) am I incorrect in assuming there is a benefit to the compactness of my version of the same thing?

I'm just a little worried about his 3000 lines of code versus my 3-400 to get the same result.

Thanks in advance for any guidance.

+6  A: 

What you really want to do here is define a custom data type which represents your data more accurately. I'm not sure what language you are using so here is some psuedocode:

class Location
{
    double latitude;
    double longitude;
    String Name;
    String Abbreviation;
    bool flag1;//you should use a better name
    bool flag2;
}

Then you can just create an array to hold all the Location objects and it would be much more readable and maintainable.

Locations = new Array;
Locations[0] = new Location("Shagwell's",...);
....

===EDIT===

Because you said you are using javascript then the best practise would probably be to store your data in a json text file, this has the benefit of removing the data from the code file and having a very easily editable data source if you want to make changes.

your JSON file would look like this

[{"lat":"23.2323", "long":"-72.3", "name":"Shagwell's" ...},
{"lat":"26.2323", "long":"-77.3", "name":"loc2" ...},
...]

You could then store the json text in an accesible place on your webserver say "data.json", then if you are using jquery you can load it in by doing something like this:

$.getJSON("data.json", function(data) { //do something with the data});
luke
+1 and also, it's standard practice to break your data out of the source code so the program doesn't have to be recompiled when the data changes. An XML file would suit you well here, I think.
Segfault
agree, if the data is fixed he might want to store it in a simple text file or a database
Ion Todirel
We're using Javascript with the Google Maps API.
Douglas
+1  A: 

With structured data, like your example, both you and your co-worker are relatively "wrong". From the looks of things, you should have implemented an array of structures, assuming of course that the data you are presenting is truly unordered, which I would be willing to guess it probably isn't. Arrays are used too often, because they are amongst the first data structures we learn, but very often aren't the best choice.

As to performance, that more often comes down to the data access code than the data type itself. Frankly too, unless you are dealing with gigantic datasets or literally real time applications, performance should be a non issue.

As to the two examples you have posted, after the compiler is done with them, they will be virtually identical.

Serapth
A: 

I personally find the former much more readable. From a performance perspective, the difference is probably minimal.

BradBrening
A: 

Leaving the other answers aside (although I agree with the others that you need structs here) your co-workers way seems better to me. Like Serapth says, the compiler will optimize away the differences and the original code has better readability.

Segfault