views:

45

answers:

6

Every time i pass some parameters to a JavasScript or jQuery functon, i use some random letters. What are the correct letters for the corresponding variable types?

function(o){} for example is for a object. But what are the other letters? Do someone have a list of those?

+3  A: 

I advise you not to assign names according to types but according to the data that the variable contain. If you pass an object that contains the configuration for a function call the variable configuration if you pass a variable that contains a name call it name and so on. In this way the code is more readable and understandable.

mck89
thanks for that. But aren't there simple letters, that anyone understands? Can i use "o" if my variable contains a jQuery object for example? Its pretty rare to see code where the variables are written out. Why is that?
meo
The variable name doesn't matter every programmer has his style to write code. If you prefer to write variable name according to their types do it, but you won't find any standard about that because there isn't. I've always used the method that i wrote to choose a variable name and trust me if i say that if you write a large project and you use variables named "o" or "s" you will go crazy if you want to change the code.
mck89
+2  A: 

Any letter will do, there is no such thing as a standard for using letters as parameters.

However, using letters for parameters is bad practise, because a letter does not describe what the parameter is intended for.

Consider this example:

function ShowBusyCursor(s) {
    //...
}

If you look at the function header, you cannot guess what the parameter s stands for. But if you change the code to this:

function ShowBusyCursor(cursorIsDisplayed) {
    //...
}

The parameter cursorIsDisplayed is a lot more developer-friendly and tells you it's a boolean that can be set to true to display the cursor. If you used s, you would have to look into the code to figure that out.

Prutswonder
+1  A: 

Here is a good list of letters I could think of:

o - object
a - array
s - string
d - date
n - number
r - regexp
b - boolean

But seriously, jokes apart, I think you might be mixing up the packed/minified source with the actual source that developers write. For example, this little function looked like this originally:

function myFunction(personName) {
    this.name = personName;
}

But after minifying, it becomes:

function myFunction(a){this.name=a}

packer is one such utility by Dean Edwards that will minify your Javascript. Play with your source code at this website and see the compressed output. Check the Shrink variables option on the right to see how parameters and other variables get renamed.

This however shouldn't affect how you write your code at all. Be it Javascript, or any other language, all identifiers - variables, function names, class names, etc. should be named after what they represent or do rather than obscure shorthands.

Anurag
thanks for this explanation and the links. Ok i gonna use real names, and minify my code once done! Oh boy i love you stackoverflow guys!
meo
A: 

Its subjective. Devise your own convention or adopt an existing one, and STICK TO IT.

Salman A
+1  A: 

Re-iterating what mck89 said, it's better to go with explanatory variable names rather than just a letter for the type. What if you have more than one parameter of the same time, do you start appending numbers?

I have often seen explanatory variable names which include the type, for instance sFirstName would be a string denoted by the "s", bForce would be a boolean, oConfig would be an object and nSubTotal would be a number.

Andy E
that is a nice approach to! thx
meo
A: 

This is a variation of Hungarian notation, and I'd advise against using single-letter variable names. What if you need to pass in 2 of any one data type? Like others have said, it also doesn't represent the meaning of the variable very well. I think this is much better:

function foo(sFirstName, sLastName)
{
    alert("Hi, my name is " + sFirstName + " " + sLastName);
}
ken