JavaScript best practices, Node.js, and ending poverty with Eric Elliott

Interviewed by Christophe Limpalair on 06/17/2015

Take your JavaScript projects and turn them into more maintainable and scalable applications. Eric has a background of building apps that scale, and I asked him to teach us how. We also talk about Node.js use cases and how to get started with it. If you've been interested in trying Node out, time to dive in!

Downloads

Similar Interviews

Codeship CTO explains Continuous Integration and the technology behind it, and shares the company story

Scaling expert Steve Corona teaches us how to scale our web apps


Links and Resources

Expand

Interview Snippets

What did you work on at your previous jobs?
00:54

In the early 2000's, Eric started his own consulting firm helping optimize software, conversion funnels, and viral loops.

He has also worked on software with millions of users at companies like Adobe Systems, The Wall Street Journal, ESPN, and more.

When did you get started with programming?
02:59

Eric started at the early age of 6 years old. A friend had a bunch of computers in his office and he would play video games. Eventually he wanted to create his own and ventured into text adventure games.

What is prototypal inheritance?
04:30

Prototypal inheritance means inheriting from an example object instead of a class... Objects inheriting from objects.

Why is prototypal inheritance better than classical inheritance?
05:35

Classical inheritance is built on the premise that you have a single parent that you inherit from. This creates a class taxonomy and a rigid structure.

Instead of a rigid structure, we can have composition. Composition lets you piece things together.

Think of classical inheritance as buying furniture from IKEA. The furniture comes with pieces that must fit in a certain order and you need to follow specific steps to assemble it. If you don't, you won't end up with the furniture you purchased.

On the other hand, composition is like legos. You can put lego pieces together in many different ways to build different things...in this case, different features.

Would you always use prototypal inheritance, even for smaller apps?
09:48

Yes.

What are factories?
10:52

A factory returns an object, just like a constructor function would in a language like PHP. The difference in JavaScript is that every function can act as a constructor which makes your code more flexible.

With a factory, we don't need to use the new keyword.

Are prototypes faster than classes?
11:39

If you focus on this argument, you are prematurely optimizing. Instead, focus on the layers that matter.

For Javascript, this often means profiling your application. Some thought leaders on this are Paul Irish and Addy Osmani.

Here are a few posts to get you started:

Improving Web App Performance with DevTools

Profiling JavaScript Performance from Google

Paul Irish - Making the www great

What are interfaces and how/why do you program to an interface?
14:28

An interface is like a contract. Think about having a contract with a bank: A banker gets you a mortgage contract with a certain rate for a certain period of time. If you don't agree to it, the deal does not work. Same thing here, except for software. If your code doesn't implement the interface properly, it doesn't work.

This is very useful because you then know exactly what methods and properties an object will supply. You can then program to that interface without worrying about the logic behind it. Manages complexity, for one.

JavaScript doesn't have these interfaces, but you should still create contracts. That way when you're building objects, they're going to follow your standards.

This really helps with extending functionality without breaking your already exiting features.

What are modules and why use them?
17:59

Modules allow you to encapsulate functionality and then share that functionality with the rest of your app. Wait, what?

Have you ever used namespacing in your app? For example, say you have a file /model/app.php. Namespacing it lets you use it elsewhere in your app. Otherwise, how would the app know how to find code in this file?

namespace Model/App;

use Model/App;

Modules have a similar concept for JavaScript.

What are closures and why use them?
20:55

Eric calls closures "critical to successful application development" in his book.

Closures are inner functions that have access to the outer function's variables. So that means that a closure has access to:
1) Its own variables
2) The outer function's variables
3) Global variables

For example:


function printProductReceipt(productName, price) {
var prefix = "You ordered the ";

function print() {
return prefix + productName + " for " + price;
}

return print();
}

printProduct("MacBook Pro", "$1,500");


How do you debug closures?
24:24

In your function callstack, you can see a stack of the different function calls and trace back to the issue.

Functional programming in JavaScript
25:33

Functional programming can reduce the amount of functions necessary. For example, why create a different function to handle Strings and Arrays if one function can manipulate both at the same time?

Immutability is important in functional programming. If anything is allowed to change a value at any time, you might create conflicts where multiple functions are depending on the same variable but one of them changes the result of that variable, while the other function is counting on that variable to remain consistent.

What are Pure (or Stateless) Functions?
32:21

A pure function is a function that only opperates on its local variables. These functions don't access any global variables. They also don't mutate things that gets passed into them.

Basically, a pure function does not have side effects. More info.

What is partial application and how does it differ from currying?
38:23

Partial application returns a function with a smaller number of arguments. You do that by fixing the value of an original argument.

For example, if you have a function that just multiplies x and y, you could create a partial function by fixing the value for x to 2. That would allow you to double the value of y by only passing in y.

var multiply = function(x) {
return function(y) {
console.log(x * y);
}
}
var double = multiply(2);
double(4); // returns 8
double(10); // returns 20


Currying is a little bit different. Whereas partial application returns a function with smaller arity, Currying returns a sequence of functions.

Currying takes a function that has multiple arguments and creates a chain of functions, each of which can't have more than one argument. Here's an example:

If we have a function add(1, 2, 3), currying would transform it into add(1)(2)(3). The first call returns a function that returns another function, etc...

These two are easily confused. In fact, I confused them myself and Eric had to correct me ;). Thanks for catching the error!

Do you use a Test-Driven Development approach?
42:00

Yes. Eric tends to write tests before he implements code. This has quite a few benefits. For example, if you write your test first, you imagine the implementation from the perspective of your user. That tends to give you an API that's easy to use.

Another benefit comes from the fact that a test should fail when you first write it. If it doesn't fail, you write code, and it still passes, you wrote a completely useless code. While this sounds obvious, it can definitely happen.

What tools do you use for testing?
44:35

A lot of people use a BDD approach which has libraries like Should.

Eric hates those. They typically make tests more verbose than they need to be.

He prefers equality tests. If a test does not = what you expected, the test can give you the expected value and the actual value, so you can compare them.He says they're also really simple to read and write and take little time.

What is Node.js? Why is it so popular?
48:40

Node is asynchronous by default, or non-blocking. This means you don't wait around for results to happen, you are free to accept more requests and process those.

It listens for jobs to come back and then handles the next process. Simply put, it's very easy to build something with Node that scales very well.

All that to say that Node is really fast and powerful, which leads to the next point:

What are some use cases for Node?
51:25

Webservers, chat applications. Anything real-time/streaming.

Do you pretty much just use Node for all your projects?
53:08

For all of Eric's web based server side needs, he uses Node.js.

Is Express.js your recommendation to build apps?
55:30

Express is Eric's go-to when building apps because it has such a thriving ecosystem-- it's very popular. On top of that, it's also minimal.

There are definitely more options than just Express, and it may not be the best for you.

Coming from a Rails background? You might like Sails.js better.

What's the best way to learn Express or Node? Read docs?
57:35

Looking at documentation for modules that sound interesting is a good step in the right direction. You can take an example from documentation and modify it to see what it does.

But if you're reading this, you probably already know that ;)

What are some of the most common bottlenecks you see in applications?
59:20

Pageload is the most important place to start with. If your page load is slow, nothing else will matter because your user will already be gone. After that, as we mentioned earlier, profiling applications is the next step. Are there any parts of your code slowing down scroll? Any animations that lag? These are great places to start.

What's the story behind your cause to end poverty?
01:02:17

After the 2008, Eric lost a lot (and later all) of his clients. These were ad agencies and magazines, and they got hit very hard.

He later moved to San Francisco and saw so many homeless people sleeping on sidewalks. This moved him to take action.

What can we do, right after watching this video, to help?
01:08:19

If you're interested in the documentary, sign up for email notifications about the film:
http://www.programmingliteracy.com/

And here's an article Eric wrote to explain why they are working so hard to spread the word.

Another article on the topic of fighting poverty with code by Eric Elliott.

Follow up?
You can contact @JS_Cheerleader on Twitter.
Of course, you can also leave comments right below.


How did this interview help you?

If you learned anything from this interview, please thank our guest for their time.

Oh, and help your followers learn by clicking the Tweet button below :)