JavaScript Patterns 2.11 Writing Comments

Document all
functions, their arguments and return values, and also any interesting or
unusual algorithm or technique. Think of the comments as hints to the future
readers of the code; the readers need to understand what your code does without
reading much more than just the comments and the function and property names.
When you have, for example, five or six lines of code performing a specific
task, the reader can skip the code details if you provide a one-line description
describing the purpose of the code and why it’s there. There’s no hard and fast
rule or ratio of comments-to-code; some pieces of code (think regular
expressions) may actually require more comments than code.

The most important habit, yet hardest to follow, is to keep the comments up
to date.

JavaScript Patterns 2.11 Writing Comments,布布扣,bubuko.com

时间: 2024-08-06 03:43:57

JavaScript Patterns 2.11 Writing Comments的相关文章

JavaScript Patterns 2.12 Writing API Docs

Free and open source tools for doc generation: the JSDoc Toolkit (http://code.google.com/p/jsdoc-toolkit/) and YUIDoc (http://yuilibrary.com/projects/yuidoc). Process of generating API documentation ? Writing specially formatted code blocks ? Running

JavaScript Patterns 2 Essentials - Writing Maintainable Code

Revisiting the code after some time has passed requires: ? Time to relearn and understand the problem ? Time to understand the code that is supposed to solve the problem As the application matures, many other things happen that require your code to b

JavaScript Patterns 5.4 Module Pattern

MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var uobj = MYAPP.utilities.object, ulang = MYAPP.utilities.lang, // private properties array_string = "[object Array]", ops = Object.prototype.toStr

JavaScript Patterns 3.7 Primitive Wrappers

Primitive value types: number, string, boolean, null, and undefined. // a primitive number var n = 100; console.log(typeof n); // "number" // a Number object var nobj = new Number(100); console.log(typeof nobj); // "object"  One reason

JavaScript Patterns 7.1 Singleton

7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. This means that the second time you use the same class to create a new object, you should get the same object that was created the first time. var obj =

JavaScript Patterns 2.2 - Minimizing Globals

Access a global variable in a browser environment: myglobal = "hello"; // antipattern console.log(myglobal); // "hello" console.log(window.myglobal); // "hello" console.log(window["myglobal"]); // "hello"

JavaScript Patterns 4.7 Init-Time Branching

When you know that a certain condition will not change throughout the life of the program, it makes sense to test the condition only once. Browser sniffing (or feature detection) is a typical example. // BEFORE var utils = { addListener : function(el

JavaScript Patterns 3.6 Regular Expression Literal

1. Using the new RegExp() constructor // constructor var re = new RegExp("\\\\", "gm"); 2. Using the regular expression literal // regular expression literal var re = /\\/gm;  when using the RegExp()constructor, you also need to escape

JavaScript Patterns 2.10 Naming Conventions

1. Capitalizing Constructors var adam = new Person(); 2. Separating Words camel case - type the words in lowercase, only capitalizing the first letter in each word. upper camel case, as in  MyConstructor(), lower  camel  case,  as  in  myFunction(),