Changes between Initial Version and Version 1 of javascriptRules


Ignore:
Timestamp:
01/19/12 15:58:02 (13 years ago)
Author:
meddle
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • javascriptRules

    v1 v1  
     1 = List Of Rules = 
     2 * Here I'm putting some rules we all have to follow when writing javascript applications. 
     3  * They can be applied not only for Sophie, I've read some books and compiled the rules (mainly from the JavaScript The Good Parts) 
     4 * There are some simple rules for writing HTML too. 
     5  
     6 == HTML/CSS rules == 
     7 * The style attribute of HTML tags should not be used, if possible. 
     8 * Presentational HTML tags such as <font> should not be used at all. 
     9 * Tags should be used for their semantic meaning, not because of how browsers render them by default.  
     10For instance, developers sometimes use a <div> tag where a <p> would be more appropriate.  
     11It's also favorable to use <strong> and <em> instead of <b> and <i> as the latter describe the visual presentation rather than the meaning. 
     12 * Minimize the number of <script> tags. 
     13 * Avoid inline event handlers. (like <div onclick='function (e) {code, code, code, code}'>) 
     14 * Dynamically add markup that has no purpose when JavaScript is disabled by the user. 
     15 
     16 == JavaScript rules == 
     17 
     18 === Avoid global variables === 
     19 * WHAT : The worst of all of JavaScript’s bad features is its dependence on global variables.  
     20A global variable is a variable that is visible in every scope. 
     21 * WHY : Global variables make it harder to run independent subprograms in the same program.  
     22If the subprograms happen to have global variables that share the same names,  
     23then they will interfere with each other and likely fail, usually in difficult to diagnose ways. 
     24 * AWFUL : You can use a variable without declaring it. This is called implied global - {{{ foo = value; }}}.  
     25This was intended as a convenience to beginners by making it unnecessary to declare variables before using them.  
     26Unfortunately, forgetting to declare a variable is a very common mistake.  
     27JavaScript’s policy of making forgotten variables global creates bugs that can be very difficult to find. 
     28 
     29 
     30 === Avoid semicolon insertion (not putting explicitly semicolons) === 
     31 * WHAT : JavaScript has a mechanism that tries to correct faulty programs by automatically inserting semicolons.  
     32Do not depend on this. It can mask more serious errors. Put semicolons at the end of every expression. 
     33 * WHY : It sometimes inserts semicolons in places where they are not welcome.  
     34Consider the consequences of semicolon insertion on the return statement.  
     35If a return statement returns a value, that value expression must begin on the same line as the return: 
     36{{{ 
     37  return 
     38  { 
     39      status: true 
     40  }; 
     41}}} 
     42This appears to return an object containing a status member.  
     43Unfortunately, semi-colon insertion turns it into a statement that returns undefined.  
     44There is no warning that semicolon insertion caused the misinterpretation of the program.  
     45The problem can be avoided if the '{' is placed at the end of the previous line and not at the begin-ning of the next line: 
     46{{{ 
     47return { 
     48      status: true 
     49}; 
     50}}} 
     51 * IMPORTANT : The curly brackets should be put on the line the 'if', 'else', 'function', etc is defined.  
     52That convention is better ofr every programming language, even C++. So we will use it. 
     53 
     54 
     55 === Avoid using '==' and '!=' === 
     56 
     57 * WHAT : JavaScript has two sets of equality operators: {{{===}}} and !==, and their evil twins == and !=.  
     58The good ones work the way you would expect.  
     59If the two operands are of the same type and have the same value, then {{{===}}} produces true and !== produces false. 
     60The evil twins do the right thing when the operands are of the same type, but if they are of different types,  
     61they attempt to coerce the values. The rules by which they do that are complicated and unmemorable.  
     62 * WHY :  
     63{{{ 
     64 
     65'' == '0' // false 
     660 == ''   // true 
     670 == '0'  // true 
     68 
     69false == 'false' // false 
     70false == '0'     // true 
     71 
     72 
     73false == undefined // false 
     74false == null      // false 
     75null == undefined  // true 
     76' \t\r\n ' == 0    // true 
     77 
     78 
     79}}} 
     80 
     81 
     82 === Avoid using 'eval' === 
     83 
     84 * WHAT : The eval function passes a string to the JavaScript compiler and executes the result. 
     85It is the single most misused feature of JavaScript.  
     86It is most commonly used by people who have an incomplete understanding of the language. 
     87 * WHY : The eval form is much harder to read.  
     88This form will be significantly slower because it needs to run the compiler just to execute a trivial assignment statement. 
     89The eval function also compromises the security of your application because it grants too much authority to the eval’d text.  
     90And it compromises the performance of the language as a whole in the same way that the with statement does. 
     91 * IMPORTANT : Do not use the Function constructor ofr creating functions (it acts like the eval). 
     92Be carefull using setInterval and setTime. 
     93 
     94 
     95 === Avoid block-less statements === 
     96 
     97 * WHAT : An if or while or do or for statement can take a block or a single statement.  
     98 * WHY : The single statement form is another attractive nuisance.  
     99It offers the advantage of saving two characters, a dubious advantage.  
     100It obscures the program’s structure so that sub-sequent manipulators of the code can easily insert bugs. 
     101{{{ 
     102 
     103if (ok) 
     104  t = true; 
     105 
     106can become: 
     107 
     108if (ok) 
     109  t = true; 
     110  advance( ); 
     111 
     112which looks like: 
     113 
     114if (ok) { 
     115  t = true; 
     116  advance( ); 
     117} 
     118 
     119but which actually means: 
     120 
     121if (ok) { 
     122  t = true; 
     123} 
     124  advance( ); 
     125 
     126}}} 
     127 
     128Programs that appear to do one thing but actually do another are much harder to get right.  
     129A disciplined and consistent use of blocks makes it easier to get it right. 
     130 * IMPORTANT : This thing is important for every programming language...  
     131Some 'knowledge' which comes from the university is actually harmfull. 
     132 
     133 
     134 === Avoid bitwise operators === 
     135 
     136 * WHAT : JavaScript has the same set of bitwise operators as Java: {{{ & | ^ ~ >> >>> << }}} 
     137 * WHY : In Java, the bitwise operators work with integers.  
     138JavaScript doesn’t have integers.  
     139It only has double precision floating-point numbers.  
     140So, the bitwise operators convert their number operands into integers, do their business, and then convert them back. 
     141In most languages, these operators are very close to the hardware and very fast.  
     142In JavaScript, they are very far from the hardware and very slow.  
     143JavaScript is rarely used for doing bit manipulation. 
     144As a result, in JavaScript programs, it is more likely that & is a mistyped && operator. 
     145The presence of the bitwise operators reduces some of the language’s redundancy, aking it easier for bugs to hide. 
     146 
     147 === Avoid typed wrappers === 
     148 * WHAT : JavaScript has a set of typed wrappers. For example: {{{ new Boolean(false) }}} produces an object that has a valueOf method that returns the wrapped value.  
     149 * WHY : This turns out to be completely unnecessary and occasionally confusing.  
     150Don’t use new Boolean or new Number or new String. 
     151Also avoid new Object and new Array. Use {} and [] instead. 
     152 
     153 
     154 == List of values that return 'false' == 
     155 * 0 - the type is Number 
     156 * NaN - the type is again Number(!) 
     157 * '' - the empty string 
     158 * false - the Boolean false 
     159 * null - the type is Object(!) 
     160 * undefined - the type is special - undefined 
     161 
     162 
     163