473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Does this code snippet look okay?

The code section works just fine, but I want to see if

A) I'm writing this type of code correctly, and
B) I'm actually getting what I expect.

The code looks like this (it is for mapping file extensions to the
document types recognized by SAP's DMS):

var Extensions = function() {
this.types = new Array();
this.extensions = new Array();

this.addType = function(type, ext) {
this.types.push(type);
this.extensions[type] = ext;
}

this.getTypeForExtension = function(ext) {
for (var i = 0; i < this.types.length; i++) {
var type = this.types[i];
var extension = this.extensions[type];
if (containsExtension(ext, extension)) {
return type;
}
}
return null;
}

var containsExtension = function(value, array) {
for (var i = 0; i < array.length; i++) {
if (array[i].toLowerCase() == value.toLowerCase()) {
return true;
}
}
return false;
}
}

What I do is create an Extensions variable, populate it through
addType and then later retrieve
the document type for a given extension via getTypeForExtension.

Is there anything "wrong" with this code? Would any experts out there
recommend changes (i.e. adding methods(functions) to the prototype).
While the code I'm writing always appears to work, I have to admit
that with it's loose typing, I don't always know exactly what's going
on behind the scenes with the code. It just works the way I expect it
to, which I guess is better than the alternative :). I've heard the
term closure thrown around, but I have not been able to fully
understand the concept. I'm a java developer, so these things are a
little foreign to me.

From what I've written I expect that addType and getTypeForExtension
are visible and can be called by any Extensions "instance", but
containsExtension cannot:

i.e.
var extensionList = new Extensions();
extensionList.addType(type, extensions); //this is visible, no
problem...
extensionList.getTypeForExtension(".gif"); //this is visible, no
problem...
extensionList.containsExtension(".gif", extensions); //this is NOT
visible and throws an error that says
//
extensionList.containsExtension is not a function...

Jun 27 '08 #1
1 895
"Tom Cole" wrote:
The code section works just fine, but I want to see if

A) I'm writing this type of code correctly, and
B) I'm actually getting what I expect.

The code looks like this (it is for mapping file extensions to the
document types recognized by SAP's DMS):

var Extensions = function() {
this.types = new Array();
this.extensions = new Array();

this.addType = function(type, ext) {
this.types.push(type);
this.extensions[type] = ext;
}

this.getTypeForExtension = function(ext) {
for (var i = 0; i < this.types.length; i++) {
var type = this.types[i];
var extension = this.extensions[type];
if (containsExtension(ext, extension)) {
return type;
}
}
return null;
}

var containsExtension = function(value, array) {
for (var i = 0; i < array.length; i++) {
if (array[i].toLowerCase() == value.toLowerCase()) {
return true;
}
}
return false;
}
}

What I do is create an Extensions variable, populate it through
addType and then later retrieve
the document type for a given extension via getTypeForExtension.
I am certainly not an expert, but my comments may be of help nevertheless.
Basically, what you want is an aray with elements of the kind:
knownTypes["gif"] = "picture"
knownTypes["doc"] = "text"
So, adding is via knownTypes[extension] = type;
Retrieval via type = knownTypes[extension];
Note that the addition will overwrite an existing entry for this extension.

You can package this in an object with methods add and retrieve, like you
have aimed to do.
But what is the purpose of your array types ? The various types are already
in the array extensions.
I don't think you need to loop thru the types array in containsExtension.
Just try the retrieval I mentioned.
(In a loop like that I would always move the value.toLowerCase() outside the
loop for performance.)
In getTypeForExtension, extension = this.extensions[type] is (most likely)
just a string. But you pass it as an array argument to
containsExtension(ext, extension). I don't understand that.
Why do you use var Extensions = function() ... instead of function
Extensions() ?
Same with ContainsExtension.

HTH
Tom
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dafella | last post by:
The following is code from Xeoport. It is suppose to access my pop3 account and load email into Mysql database. It's not inserting and there is no log or anything to tell me why. Here is the...
8
by: Gensek | last post by:
(I don't know how to make the title more specific in a useful way, sorry.) http://www.geocities.com/fusionary_2000/PingGUI.zip Requires wxPython. The part that fails is the function...
2
by: Kevin R | last post by:
This doesnt look right, though it appears to be working BinaryWriter fileWriter; for(i = 0; i < someNumber; i++) { fileWriter = new BinaryWriter(File.OpenWrite(filename)); // Write some...
3
by: ATS | last post by:
PRB - UseShellExecute option does not return STD-ERR Please help, I'm using the Process class to start an external process that "may" fail, for all kinds of reasons, but in particular, it may...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
14
by: hsharsha | last post by:
Consider the below code snippet: #include <iostream> using namespace::std; class myclass { public: myclass() { cout << "constructor" << endl;
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
2
by: daokfella | last post by:
Hi all, I've created a code snippet in VS 2005 and saved it in the proper snippet folder. When I right-click and select Insert Snippet, I navigation to My Code Snippets and it shows my snippet....
13
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.