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

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.

主站蜘蛛池模板: 汝城县| 色达县| 资溪县| 渑池县| 曲水县| 和硕县| 慈溪市| 巴青县| 青铜峡市| 新乡县| 湛江市| 独山县| 巴楚县| 普陀区| 安远县| 周宁县| 光山县| 杭州市| 监利县| 沂水县| 临清市| 上饶县| 上饶县| 新昌县| 磐安县| 浦东新区| 启东市| 紫云| 新河县| 麻江县| 宁南县| 石台县| 丰原市| 沁水县| 武冈市| 绥棱县| 江安县| 克拉玛依市| 紫金县| 大姚县| 缙云县|