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

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.

主站蜘蛛池模板: 铁岭市| 平谷区| 盱眙县| 房山区| 河西区| 内丘县| 乐清市| 肥城市| 景洪市| 同仁县| 吴旗县| 垣曲县| 长治市| 财经| 永和县| 赞皇县| 措勤县| 汝城县| 敖汉旗| 万山特区| 广宗县| 新晃| 杭州市| 岐山县| 方城县| 肃南| 巴东县| 镇坪县| 乌恰县| 博客| 松溪县| 昌都县| 乌鲁木齐县| 宜州市| 金秀| 陈巴尔虎旗| 罗定市| 沾化县| 丰顺县| 高台县| 皮山县|