| 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. |
| 10 | For instance, developers sometimes use a <div> tag where a <p> would be more appropriate. |
| 11 | It'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. |
| 20 | A 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. |
| 22 | If the subprograms happen to have global variables that share the same names, |
| 23 | then 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; }}}. |
| 25 | This was intended as a convenience to beginners by making it unnecessary to declare variables before using them. |
| 26 | Unfortunately, forgetting to declare a variable is a very common mistake. |
| 27 | JavaScript’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. |
| 32 | Do 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. |
| 34 | Consider the consequences of semicolon insertion on the return statement. |
| 35 | If 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 | }}} |
| 42 | This appears to return an object containing a status member. |
| 43 | Unfortunately, semi-colon insertion turns it into a statement that returns undefined. |
| 44 | There is no warning that semicolon insertion caused the misinterpretation of the program. |
| 45 | The 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 | {{{ |
| 47 | return { |
| 48 | status: true |
| 49 | }; |
| 50 | }}} |
| 51 | * IMPORTANT : The curly brackets should be put on the line the 'if', 'else', 'function', etc is defined. |
| 52 | That 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 !=. |
| 58 | The good ones work the way you would expect. |
| 59 | If the two operands are of the same type and have the same value, then {{{===}}} produces true and !== produces false. |
| 60 | The evil twins do the right thing when the operands are of the same type, but if they are of different types, |
| 61 | they attempt to coerce the values. The rules by which they do that are complicated and unmemorable. |
| 62 | * WHY : |
| 63 | {{{ |
| 64 | |
| 65 | '' == '0' // false |
| 66 | 0 == '' // true |
| 67 | 0 == '0' // true |
| 68 | |
| 69 | false == 'false' // false |
| 70 | false == '0' // true |
| 71 | |
| 72 | |
| 73 | false == undefined // false |
| 74 | false == null // false |
| 75 | null == 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. |
| 85 | It is the single most misused feature of JavaScript. |
| 86 | It is most commonly used by people who have an incomplete understanding of the language. |
| 87 | * WHY : The eval form is much harder to read. |
| 88 | This form will be significantly slower because it needs to run the compiler just to execute a trivial assignment statement. |
| 89 | The eval function also compromises the security of your application because it grants too much authority to the eval’d text. |
| 90 | And 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). |
| 92 | Be 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. |
| 99 | It offers the advantage of saving two characters, a dubious advantage. |
| 100 | It obscures the program’s structure so that sub-sequent manipulators of the code can easily insert bugs. |
| 101 | {{{ |
| 102 | |
| 103 | if (ok) |
| 104 | t = true; |
| 105 | |
| 106 | can become: |
| 107 | |
| 108 | if (ok) |
| 109 | t = true; |
| 110 | advance( ); |
| 111 | |
| 112 | which looks like: |
| 113 | |
| 114 | if (ok) { |
| 115 | t = true; |
| 116 | advance( ); |
| 117 | } |
| 118 | |
| 119 | but which actually means: |
| 120 | |
| 121 | if (ok) { |
| 122 | t = true; |
| 123 | } |
| 124 | advance( ); |
| 125 | |
| 126 | }}} |
| 127 | |
| 128 | Programs that appear to do one thing but actually do another are much harder to get right. |
| 129 | A disciplined and consistent use of blocks makes it easier to get it right. |
| 130 | * IMPORTANT : This thing is important for every programming language... |
| 131 | Some '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. |
| 138 | JavaScript doesn’t have integers. |
| 139 | It only has double precision floating-point numbers. |
| 140 | So, the bitwise operators convert their number operands into integers, do their business, and then convert them back. |
| 141 | In most languages, these operators are very close to the hardware and very fast. |
| 142 | In JavaScript, they are very far from the hardware and very slow. |
| 143 | JavaScript is rarely used for doing bit manipulation. |
| 144 | As a result, in JavaScript programs, it is more likely that & is a mistyped && operator. |
| 145 | The 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. |
| 150 | Don’t use new Boolean or new Number or new String. |
| 151 | Also 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 | |