Refactoring For Reusability: Day Seven of "The Complete Node.js Developer Course"

in #javascript7 years ago

photo5012998412878063555.jpg

Missing: the normal picture of myself holding a laptop

It was dark outside before I thought about taking a picture today. So you get to have a snap of just my laptop screen instead.

More complexity, for simplicity's sake

The course is starting to become a little harder for me to follow. In this section, the first part was really easy to follow what was going on. It was actually about simplicity and refactored the code previously inside the addNote function to separate functions outside so they could be reused in other note functions later on.

And that did make sense. Here are those two generalized functions:

var fetchNotes = () => {
  try {
    var notesString = fs.readFileSync('notes-data.json')
    return JSON.parse(notesString)
  } catch (e) {
    return []
  }
}

var saveNotes = (notes) => {
  fs.writeFileSync('notes-data.json', JSON.stringify(notes))
}

Reusability

Now the fetchNotes and saveNotes functions can be used inside the other note action functions later on.

Section Challenge

I was able to complete the challenge and it worked, but it was noticeably different than the solution given by the instructor. There's a lot going on here and this is where I struggle with how much to explain in a Steemit post. I don't want to talk through everything in a section line by line, but rather give you the general idea.

My solution, which worked:

if (command === 'add') {
  var note = notes.addNote(argv.title, argv.body);
  if (note === undefined) {
    console.log(
      'Note title taken. Try another title.'
    )
  } else if (note.title === argv.title) {
    console.log('---')
    console.log(`Title: ${note.title} :: Note Added`)
    console.log(`Body: ${note.body}`)
  }

Instructors Solution

The instructors solution started with:

if (note) { ... }

I was a little confused by this and it took me a while to figure out how it worked. I'm still a little fuzzy. You also don't see all the code from the other notes.js file. But essentially, the other code returns note if the addNote function worked and the note title was note a duplicate. if (note) then just checks to see if note was returned and then does it's thing. I did a check to see if note was undefined first.

Section completed

Thanks for following my progress. Please let me know if these posts are making sense and are helpful. If you are also learning node.js, what kind of writing are you looking for?

In any case...

Thanks for reading!

--- @matthewdavid

Sort:  

I believe the "if (note)" handles checks against undefined and also null so a little more complete.

Yep always good to refactor so everything does one thing and does it well. They become easier to plug together, smaller lego blocks are easier to build into complex things than less but bigger blocks, you get more control :)

Okay, so if (note) checks to see if note exists. If note exists, it can't be undefined or null. I think I get it.

All languages have their quirks. Take this in C

int a = 0;
if (a){...}  // = false

int b = anything but 0
if (b) {...} // = true

It gets abused in that language a fair bit :)

Exactly. By default, every uninitialized object in Javascript equals to undefined. If you want to learn a bit more about this behaviour, you should check this link that talks about Truthy and Falsy elements: some objects that don't contain true or false values, but behave the same way.


I've learned a couple of new things with your posts @matthewdavid, your explanations are very simple and accurate. Keep up the good work.

Just found out about @originalworks tag also. It should trigger an upvote for you :)

The @OriginalWorks bot has determined this post by @matthewdavid to be original material and upvoted it!

OW2.gif

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!
Do you like what @OriginalWorks does? Give it an upvote to support this project and keep it free!
For more information, Click Here!

Always do type and value checking of the variable before you continue working with it. You are checking if ´note´ exists so you can avoid unexpected errors.

Congratulations @matthewdavid! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of comments

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @matthewdavid! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!