Start With the API You Want Then Implement

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

If you can think ahead to how you want your code to look into the future, then you can tackle your problems from the inside out. You design how you want the code to look, then go about implementing the internals. This is similar to a "test driven development" mindset where you test the implementation first, then write the actual implementation of the function after.

John Lindquist: [0:00] When designing your functions, you have complete control over how you want them to look. It's often good to start out with the API you want and then build out functions that can reorder and manage arguments for you.

[0:12] Say, I want myZip to take two broadcasters. We'll do our createInterval at 100 again, then a for...of of myZip. I want that to return a function where I can pass in my operators. It reads more like, "Here's the thing pushing out events, intervals, and for...of combined into zip and then here are the things that happen after taking a letter, filtering, appending, and lowercasing."

[0:45] Defining myZip would be simply let myZip and then the two broadcasters, so broadcasterOne and broadcasterTwo. This one is here and this one is here. This returns a function, which takes a number of operators. This returns our original zip with the original API, so broadcasterOne, broadcasterTwo. We wrap this with a piped version of our operators. I'll hit Save. Now myZip should behave the same way we had our original zip behaving.

[1:27] This approach expresses what this is doing in the browser a bit better, broadcasting stuff and then walking through steps. Whereas if I change this back to our original zip, and then I wrap this with the operators...We need to delete this. Hit Save. This expresses better what the functions are doing. Zip is returning a broadcaster, and the operators are wrapping around the broadcaster.

[1:54] Because this is educational content, I'm going to stick with the approach that expresses what the functions are doing. I'll leave this as a challenge to you as you're writing your code out, to express how best this would reflect what's happening in the browser.

egghead
egghead
~ 2 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today