官术网_书友最值得收藏!

The DRY principle

Now, there is one more thing I want to tackle before we wrap up this section. Inside app.js we now have the same code in two places. We have the space or title body in the add command as well as in the read command:

if (command === 'add') {
var note = notes.addNote(argv.title, argv.body);
if (note) {
console.log('Note created');
console.log('--');
console.log(`Title: ${note.title}`);
console.log(`Body: ${note.body}`);
} else {
console.log('Note title taken');
}
} else if (command === 'list') {
notes.getAll();
} else if (command === 'read') {
var note = notes.getNote(argv.title);
if (note) {
console.log('Note found');
console.log('--');
console.log(`Title: ${note.title}`);
console.log(`Body: ${note.body}`);
} else {
console.log('Note not found');
}

When you find yourself copying and pasting code, it's probably best to break that out into a function that both locations call. This is the DRY principle, which stands for Don't Repeat Yourself.

主站蜘蛛池模板: 青阳县| 甘谷县| 安徽省| 德化县| 清河县| 晋江市| 金堂县| 广水市| 珲春市| 柳河县| 贡嘎县| 囊谦县| 金堂县| 张家界市| 东源县| 新闻| 旺苍县| 澎湖县| 临沂市| 平顶山市| 师宗县| 沁水县| 内黄县| 万安县| 新津县| 新蔡县| 阿克苏市| 巴林左旗| 义乌市| 沾化县| 东莞市| 泰顺县| 金山区| 缙云县| 鄂托克前旗| 台山市| 黔江区| 大新县| 兴业县| 高阳县| 东乡族自治县|