- Learning Node.js Development
- Andrew Mead
- 146字
- 2021-06-30 18:57:01
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.
推薦閱讀
- INSTANT PhpStorm Starter
- Django 2 by Example
- HTML5 Game development with ImpactJS
- 物聯網時代
- Getting Started with WebRTC
- 物聯網技術與應用
- Getting Started with Grunt:The JavaScript Task Runner
- 語音信號處理及Blackfin DSP實現
- 物聯網場景設計與開發(初級)
- 深入理解OpenStack Neutron
- 基于IPv6的家居物聯網開發與應用技術
- React Design Patterns and Best Practices(Second Edition)
- 物聯網導論
- 走近2050:注意力、互聯網與人工智能
- 5G重塑數字化未來