Lesson’s Goals
- Using the
fs
module to work with files - Installing and using 3rd party packages to perform HTTP requests
- Have student implement their solution
- Review solution
The point of refactoring is to make code easier to understand, reason about, and maintain. NOT to make a code file as small as possible
- A bit about process.argv
- A refactored solution is often arrived to rather than immediately apparent
- All refactors are changes, but all changes are not refactors
- Changes that diminish the readability and maintainability of the code do not fit the definition of a refactor.
Point out that fs
is a Node module and therefore doesn’t need to be installed.
- Have everyone guess what the function name for reading a file is
- Create movies.txt
- Walk through implementation
A callback in the context of JavaScript is a function passed as an argument into another function. This allows the calling function to execute arbitrary code so long as the function passed as an argument matches the signature required. This make code more generic and in JavaScript allows code to run asynchronously
- Know that the callback passed to
readFile
must be a function that excepts, as arguments, two values:err
anddata
- The names can be anything, the important part is the order and that your usage is inline with their implied types
- It is very important that you read the documentation on any of the frameworks, libraries, or languages that you use.
- Some projects have better documentation than others and that they may have to go to other sources like message boards, Stack Exchange, vendor support, or even read the code (if the product is open source)
- Sometimes it may be useful to see how something is used. Tutorial sites, open-source projects, and event blogs are perfect for this
- Instruct partners to do this assignment. Hint that the JavaScript string type has a method called
split
docs - Review solution
- I chose to use iterators
- Benefits include less code, no need to manage state, and elimination of an entire class of errors
Why? Because writeFile
will simply over-write a file if it already exists. Most of the time, what we actually want to do is add to a file.
appendFile
will actually create a file if it does not exists. This is the perfect opportunity to discuss design: function/methods should do one thing)
- Demo what the program should do
- Show how we persist state in
bank.txt
- Review solution
- Go over importance of handling errors and avoiding side-effects where possible. It’s better to allow the user of a class or function to determine what to do. In our case, we return a value where it makes sense and allow the consumer of the function to use it how they see fit
- Attempt to use package without installing it
- Ask students to guess what when wrong
- Talk a little bit about Node packages and how to install them
- Run
http_request
- Review callbacks and discuss what the function signature of the callback passed into
Request
. Due to JavaScript’s dynamic nature, it is up to you to either ready the docs (if they exist or are any good) or intuit what each argument is to be used forerror
: an errorresponse
: the HTTP status code, if one existsbody
: the body of your HTTP request, if one exists; read Hypertext Transfer Protocol - Wikipedia
Cannot do this example due to this API being private
- chron: means time; root
- syn: with; prefix
- a-: not; prefix
- ous: turns word into adjective having the quality of possessing
Synchronous means with time and ** Asynchronous** means not with time
In JavaScript Synchronous code is executed in the order it is written. That means if you have two functions that take 500s to execute, the total running time of your program will be at least 1000s. Asynchronous code may not be execute in the order that it is written.
- Synchronous
- Easy to reason about
- Deterministic
- Can decrease the speed of a program
- Asynchronous
- Harder to reason about
- Non-deterministic
- Potential race conditions
- Can increase the speed of a program
This is an advanced, complex, and complicated topic in computer science. Truly understanding this topic requires you to know computer architecture and how operating systems work. Thankfully much of this complexity has been abstracted away from us by the OS, Node, and JavaScript.
The thing you need to know is that asynchronous functions will allow your program to continue working when some blocking operation occurs.
#UTHouston