| Total Complexity | 2 |
| Complexity/F | 2 |
| Lines of Code | 25 |
| Function Count | 1 |
| Duplicated Lines | 25 |
| Ratio | 100 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 mongoose = require('mongoose') |
|
|
|
|||
| 2 | var invoiceItem = require('./common/line-item') |
||
| 3 | |||
| 4 | var invoiceSchema = new mongoose.Schema({ |
||
| 5 | date: { type: Date, default: Date.now }, |
||
| 6 | customer: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer', required: true }, |
||
| 7 | items: [invoiceItem], |
||
| 8 | totalVat: { type: Number, default: 0.00 }, |
||
| 9 | total: { type: Number, default: 0.00 } |
||
| 10 | }) |
||
| 11 | |||
| 12 | invoiceSchema.pre('save', function (next) { |
||
| 13 | let overallTotal = 0.00 |
||
| 14 | // let overallVat |
||
| 15 | // All the items should give the total |
||
| 16 | for (let item in this.items) { |
||
| 17 | overallTotal += item.total |
||
| 18 | // overallVat += item.subTotal * item.vatRate |
||
| 19 | } |
||
| 20 | |||
| 21 | this.total = overallTotal |
||
| 22 | next() |
||
| 23 | }) |
||
| 24 | |||
| 25 | module.exports = mongoose.model('Invoice', invoiceSchema) |
||
| 26 |