| Total Complexity | 8 |
| Complexity/F | 1.6 |
| Lines of Code | 44 |
| Function Count | 5 |
| Duplicated Lines | 44 |
| Ratio | 100 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | View Code Duplication | var http = require('http'), |
|
| 2 | url = require('url'), |
||
| 3 | join = require('path').join, |
||
| 4 | extname = require('path').extname, |
||
| 5 | join = require('path').join, |
||
|
|
|||
| 6 | fs = require('fs'), |
||
| 7 | port = process.argv[2] || 3000 |
||
| 8 | |||
| 9 | var mime = { |
||
| 10 | 'html': 'text/html', |
||
| 11 | 'css': 'text/css', |
||
| 12 | 'js': 'application/javascript', |
||
| 13 | } |
||
| 14 | |||
| 15 | http.createServer(function(req, res){ |
||
| 16 | console.log(' \033[90m%s \033[36m%s\033[m', req.method, req.url) |
||
| 17 | |||
| 18 | var pathname = url.parse(req.url).pathname, |
||
| 19 | path = join(process.cwd(), pathname) |
||
| 20 | |||
| 21 | function notFound() { |
||
| 22 | res.statusCode = 404 |
||
| 23 | res.end("404 Not Found\n") |
||
| 24 | } |
||
| 25 | |||
| 26 | function error(err) { |
||
| 27 | res.statusCode = 500 |
||
| 28 | res.end(err.message + "\n") |
||
| 29 | } |
||
| 30 | |||
| 31 | fs.exists(path, function(exists){ |
||
| 32 | if (!exists) return notFound() |
||
| 33 | |||
| 34 | fs.stat(path, function(err, stat){ |
||
| 35 | if (err) return error() |
||
| 36 | if (stat.isDirectory()) path = join(path, 'index.html') |
||
| 37 | res.setHeader('Cache-Control', 'no-cache') |
||
| 38 | res.setHeader('Content-Type', mime[path.split('.').slice(-1)] || 'application/octet-stream') |
||
| 39 | fs.createReadStream(path).pipe(res) |
||
| 40 | }) |
||
| 41 | }) |
||
| 42 | }).listen(port) |
||
| 43 | |||
| 44 | console.log('\n Server listening on %d\n', port) |
||
| 45 |
This check looks for variables that are declared in multiple lines. There may be several reasons for this.
In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.
If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.