I completely agree with the other answers - just try to give another inspiration...
I remember when I started to code I was over engaged in micro optimization like which variable will perform better but after all I personally came up with this rules for my coding style explicitly about variables and function names also:
- Others also have coding styles. Learning from the experienced ones means using theirs experience on the one hand and getting closer to an -so to say- "global" style which leads to better readability among each others code.
- Choose names as discriptive as possible. Not only that it makes your code much more readable and maintable but also this leads to focus on functionality inside of the code structure itself.
- Stay consistent doesn't mean to be flexible anymore and not to develop your style after new experiences but its a good practice in general.
- Let the name tell you the type also.
- Let the name tell you the scope also.
So after this general rules here are some practical examples:
I just choosed an very abstract example to show the general functionality...
var _outerScope = 'I prefer to mark my variables with the most global scope with a leading underscore';
(function (){
var innerScope = 'while this variable has its scope inside of this function its just meant to be used in here'
if (_outerScope !== innerScope) {
'everything is a bit more clear';
}
var fSetAttrTitle = function ( $Selector, iSelector ) { // read as: "function set attribute title" awaits an "jQuery object" and "integer"
sOriginalTitle = $Selector.attr('title'); // ra: "string original title" = "jQuery object selectors attribute title"
$Selector.attr('title', 'this container has id: ' + iSelector); // ra: "jQuery object selectors attribute title" = "this container has id: " plus "integer selector"
return sOriginalTitle; // ra: "return string original title"
};
var isIdSel2inArray = false; // this should be self explanatory
var aSelector = ['#sel1', '#sel2', '#sel3']; // ra: "array selector" = [...]
var iSelector = aSelector.length; // ra: "integer selector" = length of "array selector" | normally i would use "i" instead of iSelector in this case but for illustration lets stay with it
while ( iSelector-- ) // ra: "iterate until iSelector is 0"
{
sSelector = aSelector[ iSelector ]; // ra: "string selector" is a piece out of "array selector" with number "integer selector"
if (sSelector !== '#sel2') { // ra: "if string selector is not '#sel2' then array selector is return value of set attribute title"
aSelector[ iSelector ] = fSetAttrTitle( jQuery( sSelector ), iSelector );
} else { // ra: "if string selector is '#sel2' then '#sel2' is in array"
isIdSel2inArray = true;
}
}
if (isIdSel2inArray === true) {
alert('ra: "if boolean is id sel2 in array is true alert this text"');
}
}).call(this);
if (typeof innerScope === 'undefined') {
'Of course I can not use this variable here while it has no underscore '
+ 'its not in the outer scope but the next one has an underscore so it is '
+ 'save to use it here ' + (typeof _outerScope !== 'undefined');
}
hope its a bit inspiring :)