Evented programming takes time to get used to. About a year of embarrassing 
code, I would say.

1. JavaScript is object oriented, classes are good, extends and designs 
around data, such as a database connection
2. Flatten your code as much as variable scope allows. A source file is 
preferably no more than 200 lines
3. Use function names to label the 3-4 lines of code inside the function. 
You'll love it in the debugger and stack traces
4. Avoid module-level variables that are not truly singleton. It limits 
code usability

so, (hand-typed not run code):

function Tokenstore(opts) { // this is a constructor
  this.connect = connect
  this.getUserToken = getUserToken
  var db
  var collName

  function connect(opts, cb) {
    collName = opts.coll
    mongo.connect(opts, saveDb)
  }

  function saveDb(err, dbx) {
    if (!err) db = dbx
    cb(err)
  }

  function getUsertoken(userId, cb) {
    db.collection(colName).find(...)
  }

  function hasDb() {
    return !!db
  }
}

a. There's more than one way to do this with JavaScript itself, but a 
library is rarely required. I would discourage funny libraries unless there 
is a concrete need.
b. Software is easier the more you break it down. This is why we love npm, 
because now a single function can be distributed to others.
c. Node breaks down the multithreading problem. As in simple not 
sophisticated.
d. A price is that cpu-hogging tasks delays all other processing, so those 
need to be moved out-of-process if response time is critical.
e. As opposed to popular belief, JavaScript 5.1 does not need fixing, but 
can be very much improved.

People may have different opions, and I may only be half as smart as the 
people on this thread (eg. Isaac) but those are hard earned learnings.

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to