| 1 | = Coding Style Guidelines = |
| 2 | |
| 3 | Those guidelines where defined based on mixed common standards mainly related to Java and C# programming. |
| 4 | |
| 5 | It is quite common to take the Java style to write !JavaScript code, assuming it is a "cut-down" version of Java. Although !JavaScript has an indirect relation with Java, apart from the similar "C style" syntax, it has nothing to do with it. It is not a class based language. It is an interpreted, object based, loosely typed scripting language. It can run only in the presence of a "host" application, like a web browser. |
| 6 | |
| 7 | If we instead take a closer look at C# code standards (from Microsoft), we'll not more modern and readable proposals, which help also on identifying custom code from the underlying host environment. |
| 8 | |
| 9 | The following definitions are based on long years of experience with pure !JavaScript coding. |
| 10 | |
| 11 | == General Recommendations == |
| 12 | |
| 13 | Any violation to the guide is allowed if it enhances readability. |
| 14 | |
| 15 | The name "FCKeditor" is used, it must always be written with "FCK" uppercased concatened with "editor" in lowercase. The following are wrong: "!FckEditor", "FCKEditor", "fckEditor", "FCK Editor", and so on. Exception is made for directory names, where all chars "should" be in lowercase. |
| 16 | |
| 17 | The name "!JavaScript" should be written with both the "J" and "S" in uppercase. The following are wrong: "Javascript", "javascript", etc. |
| 18 | |
| 19 | == Naming Conventions == |
| 20 | |
| 21 | === Generic Name Conventions === |
| 22 | |
| 23 | Names representing packages must be in CamelCase. Whenever applicable, the package name will be prefixed with "FCKeditor.". |
| 24 | |
| 25 | {{{ |
| 26 | FCKeditor, FCKeditor.Net, FCKeditor.Java |
| 27 | }}} |
| 28 | |
| 29 | Names representing public classes and public global objects must be in CamelCase and prefixed with "FCK". |
| 30 | |
| 31 | {{{ |
| 32 | FCKBrowserInfo, FCKToolbarButton |
| 33 | }}} |
| 34 | |
| 35 | Names representing constants must be all uppercase. |
| 36 | |
| 37 | {{{ |
| 38 | FCK_STATUS_COMPLETE, FCK_EDITMODE_WYSIWYG, CTRL |
| 39 | }}} |
| 40 | |
| 41 | Names representing public methods must be verbs and written in CamelCase. |
| 42 | |
| 43 | {{{ |
| 44 | AppendStyleSheet(), GetElementDocument(), SetTimeout() |
| 45 | }}} |
| 46 | |
| 47 | Names representing public properties must be verbs and written in CamelCase. |
| 48 | |
| 49 | {{{ |
| 50 | Name, Document, TargetWindow |
| 51 | }}} |
| 52 | |
| 53 | Names representing private methods must be verbs and written in CamelCase prefixed with an underscore. |
| 54 | |
| 55 | {{{ |
| 56 | _DoInternalStuff() |
| 57 | }}} |
| 58 | |
| 59 | Names representing private properties must be verbs and written in CamelCase prefixed with an underscore. |
| 60 | |
| 61 | {{{ |
| 62 | _InternalCounter, _Prefix |
| 63 | }}} |
| 64 | |
| 65 | Names representing private global function must be in CamelCase prefixed with an underscore. |
| 66 | |
| 67 | ((( |
| 68 | function _DoSomething() |
| 69 | ))) |
| 70 | |
| 71 | Abbreviations and acronyms should not be uppercase when used as name. |
| 72 | |
| 73 | {{{ |
| 74 | SetHtml(), XmlDocument |
| 75 | NOT |
| 76 | SetHTML(), XMLDocument |
| 77 | }}} |
| 78 | |
| 79 | All names should be written in English. |
| 80 | |
| 81 | === Specific Name Conventions === |
| 82 | |
| 83 | The prefixes "Get" and "Set" may be used on methods that get or set properties which require computation. |
| 84 | |
| 85 | {{{ |
| 86 | SetArrayCount(), GetFormattedValue() |
| 87 | }}} |
| 88 | |
| 89 | The prefix "!CheckIs" may be used on methods which return boolean states which require computation. |
| 90 | |
| 91 | {{{ |
| 92 | CheckIsValid(), CheckIsEmpty() |
| 93 | }}} |
| 94 | |
| 95 | Abbreviations in names should be avoided. |
| 96 | |
| 97 | {{{ |
| 98 | GetDirectoryName(), applicationCommand |
| 99 | NOT |
| 100 | GetDirName(), appCmd |
| 101 | }}} |
| 102 | |
| 103 | == Files == |
| 104 | |
| 105 | All files must be named in all lower case. |
| 106 | |
| 107 | Classes and global objects should be declared in individual files with the file name matching the class/object name. |
| 108 | |
| 109 | {{{ |
| 110 | fckbrowserinfo.js, fcktoolbarbutton.js |
| 111 | }}} |
| 112 | |
| 113 | == Layout == |
| 114 | |
| 115 | For indentation, the TAB should be used. Development IDEs should be configured to represent TABs as 4 spaces. |
| 116 | |
| 117 | {{{ |
| 118 | if ( test ) |
| 119 | { |
| 120 | if ( otherTest ) |
| 121 | DoSomething() ; |
| 122 | DoMore() ; |
| 123 | } |
| 124 | }}} |
| 125 | |
| 126 | Block layout should be as illustrated. |
| 127 | |
| 128 | {{{ |
| 129 | while ( !done ) |
| 130 | { |
| 131 | doSomething() ; |
| 132 | done = moreToDo() ; |
| 133 | } |
| 134 | }}} |
| 135 | |
| 136 | Single statement if/else/while/for should be written without brackets, but never in the same line. |
| 137 | |
| 138 | {{{ |
| 139 | if ( condition ) |
| 140 | statement ; |
| 141 | |
| 142 | while ( condition ) |
| 143 | statement ; |
| 144 | |
| 145 | for ( initialization ; condition ; update ) |
| 146 | statement ; |
| 147 | }}} |
| 148 | |
| 149 | The incompleteness of split lines must be made obvious. |
| 150 | |
| 151 | {{{ |
| 152 | totalSum = a + b + c + |
| 153 | d + e ; |
| 154 | |
| 155 | method( param1, param2, |
| 156 | param3 ) ; |
| 157 | |
| 158 | setText( 'Long line split' + |
| 159 | 'into two parts.' ) ; |
| 160 | |
| 161 | }}} |
| 162 | |
| 163 | === Whitespace === |
| 164 | |
| 165 | Conventional operators should be surrounded by a space (including ternary operators). |
| 166 | |
| 167 | {{{ |
| 168 | if ( i > 3 && test == 'yes' ) |
| 169 | }}} |
| 170 | |
| 171 | Commas should be followed by a space. |
| 172 | |
| 173 | {{{ |
| 174 | function MyFunction( parm1, param2, param3 ) |
| 175 | }}} |
| 176 | |
| 177 | Semi-colons in for statements should be surrounded by a space. |
| 178 | {{{ |
| 179 | for ( var i = 0 ; i < count ; i++ ) |
| 180 | }}} |
| 181 | |
| 182 | Semi-colons should be preceded by a space. |
| 183 | {{{ |
| 184 | DoSomething() ; |
| 185 | var name = 'John' ; |
| 186 | }}} |
| 187 | |
| 188 | Colons should be surrounded by a space. |
| 189 | {{{ |
| 190 | case 100 : |
| 191 | NOT |
| 192 | case 100: |
| 193 | }}} |
| 194 | |
| 195 | Opening parenthesis must be followed by a space and closing parenthesis must be preceded by a space. |
| 196 | |
| 197 | {{{ |
| 198 | if ( myVar == 1 ) |
| 199 | }}} |
| 200 | |
| 201 | Functions/method calls should not be followed by a space. |
| 202 | |
| 203 | {{{ |
| 204 | DoSomething( someParameter ) ; |
| 205 | NOT |
| 206 | doSomething ( someParameter ) ; |
| 207 | }}} |
| 208 | |
| 209 | Logical units within a block should be separated by one blank line. |
| 210 | |
| 211 | {{{ |
| 212 | // Create a new identity matrix |
| 213 | var matrix = new Matrix4x4() ; |
| 214 | |
| 215 | // Precompute angles for efficiency |
| 216 | var cosAngle = MathCos(angle) ; |
| 217 | var sinAngle = MathSin(angle) ; |
| 218 | |
| 219 | // Specify matrix as a rotation transformation |
| 220 | matrix.SetElement( 1, 1, cosAngle ) ; |
| 221 | matrix.setElement( 1, 2, sinAngle ) ; |
| 222 | matrix.setElement( 2, 1, -sinAngle ) ; |
| 223 | matrix.setElement( 2, 2, cosAngle ) ; |
| 224 | |
| 225 | // Apply rotation |
| 226 | transformation.multiply( matrix ) ; |
| 227 | }}} |
| 228 | |
| 229 | == Variables == |
| 230 | |
| 231 | Variables must be always defined with "var". |
| 232 | |
| 233 | Function parameters must be in pascalCase. |
| 234 | |
| 235 | {{{ |
| 236 | function Sum( firstNumber, secondNumber ) |
| 237 | }}} |
| 238 | |
| 239 | Local variables must be in pascalCase. |
| 240 | |
| 241 | {{{ |
| 242 | function DoSomething() |
| 243 | { |
| 244 | var colorOne = 1 ; |
| 245 | var colorTwo = 2 ; |
| 246 | |
| 247 | return colorOne + colorTwo ; |
| 248 | } |
| 249 | }}} |
| 250 | |
| 251 | == Comments == |
| 252 | |
| 253 | Tricky code should not be commented but rewritten: In general, the use of comments should be minimized by making the code self-documenting by appropriate name choices and an explicit logical structure. |
| 254 | |
| 255 | All comments should be written in English. |
| 256 | |
| 257 | There should be a space after the comment identifier. |
| 258 | |
| 259 | {{{ |
| 260 | // This is a comment NOT: //This is a comment |
| 261 | /** NOT: /** |
| 262 | * This is block *This is a block |
| 263 | * comment *comment |
| 264 | */ */ |
| 265 | }}} |
| 266 | |
| 267 | ---- |