- 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.
推薦閱讀
- 社交網絡對齊
- Aptana Studio Beginner's Guide
- Cisco OSPF命令與配置手冊
- 面向物聯網的CC2530與傳感器應用開發
- Windows Server 2003 Active Directory Design and Implementation: Creating, Migrating, and Merging Networks
- Socket.IO Real-time Web Application Development
- 電力物聯網工程技術原理與應用
- Kong網關:入門、實戰與進階
- 6G:面向2030年的移動通信
- jQuery Mobile Web Development Essentials
- 物聯網工程概論
- 物聯網工程導論(第3版)
- 轉化:提升網站流量和轉化率的技巧
- 數據血緣分析原理與實踐
- Dart Cookbook