What’s the difference between static and dynamic scoping? And how do those differences impact the development process and tool selection?
- What You Should Say: “Static scoping is based on the lexical scope model, and relies on variable declarations and function closures. Static scoping is fixed at the time you write the code, and can usually be optimized by the JS engine. Dynamic scoping with the ‘this’ keyword is based on run-time conditions. It may be less performance-optimized, but the flexibility of sharing the function across different contexts can be useful. Sometimes the predictability of static scoping is more preferable, and sometimes the flexibility of ‘this’ scoping is more what you want.”
- Why You Should Say It: Being able to explain how a language works shows that you’re an experienced developer who possesses in-depth knowledge of a tool. The lexical scoping model and the dynamic ‘this’ model are at the very heart of JS, and any good JS developer knows how to navigate both mechanisms and properly make the tradeoffs.
What is function closure? What are some ways that closure can be used to maintain state?
- What Most People Say: “Closure is about callbacks. Setting a timeout is an example of closure.”
- What You Should Say: “Closure is how a function is able to remember the variables in its enclosing scope when it runs later in a different scope. For example, when I pass a function to an event handler or Ajax call, and its surrounding variables later when the event fires to remember its context. That’s an example of the function closure maintaining state.”
- Why You Should Say It: Closure describes how a function maintains access to the scope of surrounding variables. It is one of the most foundational aspects of the language, and is used in many different contexts, including functional programming.
So, what role does closure play in functional programming?
- What Most People Say: “Functional programming is about breaking up your program into many functions.”
- What You Should Say: “Functional programming relies on closure to maintain state instead of side effects on surrounding variables. This lets you perform operations on values in a more predictable way.”
- Why You Should Say It: Closure is one of the building blocks of functional programming, and JS’s ability to mix procedural and OO concepts with the power of functional programming is its most unique and important language design characteristic. This powerful combination gives developers many more tools to solve problems.
In your opinion, what’s the difference between easy and simple software and why does it matter?
- What Most People Say: “I prefer to make easy software because I want other developers and users to work faster and create more code in less time.”
- What You Should Say: “Simple software is modular. And subdividing a computer program into separate sub-programs makes it easier to develop, test and maintain code. Simple doesn’t necessarily mean faster, however. Simple software makes it easier to write great code but it’s not always faster.”