Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

triadic handlers optimization

  • Loading branch information...
commit f96340ea1b0f14f7fba2286ce84c8d5943ef256f 2 parents d913bca + f8ebd69
pibi pibi authored
Showing with 35 additions and 25 deletions.
  1. +22 −12 README.markdown
  2. +1 −1  package.json
  3. +12 −12 zen-http.js
34 README.markdown
View
@@ -4,18 +4,6 @@ _Z-engine (Zen)_ is the most simple, minimal, fast, full featured, general purpo
It's like Ruby's Rack or Python WSGI, but for Node.js.
-## Optimized version
-
-_Zen_ bundles optimized versions for specific purpose:
-
- - zen-http: a HTTP 'request,response' modules engine. Connect and Stack compatible, faster in real world use cases
-
-## Benchmarks
-
-Internal benchmarks show how _Zen_ versions are faster than Stack and Stack2
-
-<img src="https://github.com/pblabs/zen/raw/master/results.png">
-
## Install
If you use npm, then install zen via npm.
@@ -81,6 +69,28 @@ errorHandler and resultHandler could be overridden by custom functions. As handl
Due its general purpose, _Zen_ does not provide any middleware modules of any kind. Take a look at http://github.com/pblabs/zen-garden
+#_Triadic subscriptions
+
+The Book Clean Code (http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882) states (@ chapter #3):
+ `The ideal number of arguments for a function is zero (niladic).
+ Next comes one (monadic), followed closely by two (dyadic).
+ Three arguments (triadic) should be avoided where possible.
+ More than three (polyadic) requires very special justification -- and then shouldn't be used anyway.`
+
+If you need multiple arguments there is a big chance that the method is doing more than it should or that you could abstract
+the operation better. Mind the Javascript objects!!!
+Keep classes and functions as small as possible, break it into multiple "modules"... it is usually easier to
+understand what is going on.
+
+_zen-http_ is _Zen_ for triadic handlers, includes proper result and error handlers and default 404 response. Connect and Stack compatible, really faster in real world use cases.
+Use `next(err)` to push a 500 error message to the client, `next(null,result)` to send the result with status 200.
+
+## Benchmarks
+
+Internal benchmarks show how _Zen_ versions are faster than Stack and Stack2
+
+<img src="https://github.com/pblabs/zen/raw/master/results.png">
+
# Conclusion
_Zen_ is available on github <a href='https://github.com/plabs/zen'>here</a>
2  package.json
View
@@ -1,6 +1,6 @@
{
"name": "zen",
- "version": "0.1.0",
+ "version": "0.1.1",
"description": "is a simple, basic, fast, general purpose module engine",
"keywords": ["engine", "stack", "module", "wsgi", "rack", "next"],
"repository": "git://github.com/pblabs/zen.git",
24 zen-http.js
View
@@ -1,5 +1,5 @@
/**
- * Optimized HTTP request,response Zen
+ * Zen for triadic handler. Plus proper http error handling and responder
*/
/**
@@ -31,38 +31,38 @@ var resultHandler = function result(req, resp, result) {
* Zen uses a 'setup' pattern, returning a callable engine function
*/
module.exports= function (/*layers*/) {
- var error = function(req, resp, err) {
- return engine.errorHandler(req, resp, err);
+ var error = function(a, b, err) {
+ return engine.errorHandler(a, b, err);
};
var layers=Array.prototype.concat([error],Array.prototype.slice.call(arguments).reverse());
var L=layers.length-1;
var first=layers[L];//first access optimization
- var nextHandler= function(req,resp,err,res) {
+ var nextHandler= function(a,b,err,res) {
try {
if (err) {
- return engine.errorHandler(req, resp, err); //err
+ return engine.errorHandler(a, b, err); //err
}
- return engine.resultHandler(req, resp, res);
+ return engine.resultHandler(a, b, res);
} catch (ex) {
- return engine.errorHandler(req, resp, ex);
+ return engine.errorHandler(a, b, ex);
}
}
// The real Zen Engine
- var engine= function (req, resp) {
+ var engine= function (a,b) {
var i=L;
try {
var next= function(err,res) {
if(!err&&!res) {
- return layers[--i](req,resp,next);
+ return layers[--i](a,b,next);
}
- return nextHandler(req,resp,err,res);
+ return nextHandler(a,b,err,res);
}
- return first(req,resp,next);
+ return first(a,b,next);
} catch (err) {
- return engine.errorHandler(req, resp, err);
+ return engine.errorHandler(a, b, err);
}
}
if (L==0){engine=first}; //no next
Please sign in to comment.
Something went wrong with that request. Please try again.