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

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.

主站蜘蛛池模板: 竹北市| 启东市| 当涂县| 赣州市| 潜山县| 平武县| 临朐县| 阳高县| 东乡| 揭东县| 泗洪县| 黄陵县| 通辽市| 江油市| 萨迦县| 阿拉尔市| 马关县| 芜湖市| 冕宁县| 鄯善县| 大足县| 武乡县| 朝阳市| 界首市| 蓝山县| 基隆市| 汽车| 邵武市| 奇台县| 阳城县| 德钦县| 射阳县| 汪清县| 杭州市| 鄂托克前旗| 临沂市| 磐安县| 扎赉特旗| 晋中市| 江油市| 牙克石市|