10 First-class functions: Part 1

 
Image

In this chapter

  • Discover the power of first-class values.
  • Learn how to make first-class function versions of syntax.
  • Learn to wrap syntax using higher-order functions.
  • Apply two refactorings that use first-class and higher-order functions.

You are now in the waiting room to the second part of this book. This room contains a door labeled “first-class functions.” This chapter will open that door and reveal a new world of powerful ideas related to first-class functions. What are first-class functions? What are they used for? How can we make them? All of these questions will be addressed in this chapter. We’ll explore a small part of the vast array of uses for them in the rest of the chapters of this part.

In this chapter, we’re going to learn a code smell and two refactorings that help us eliminate duplication and find better abstractions. We’ll apply these new skills throughout this chapter and the whole of part 2 of this book. Don’t worry about learning them on this page. This is just a summary. We’ll learn each one as we need them in this chapter.

Code smell: Implicit argument in function name

This code smell identifies aspects of code that could be better expressed as first-class values. If you are referring to a value in the body of a function, and that value is named in the function name, this smell applies. The solution is the next refactoring.

Marketing still needs to coordinate with dev

Code smell: Implicit argument in function name

Refactoring: Express implicit argument

Recognize what is and what isn’t first-class

Will field names as strings lead to more bugs?

Will first-class fields make the API hard to change?

We will use a lot of objects and arrays

First-class functions can replace any syntax

For loop example: Eating and cleaning up

Refactoring: Replace body with callback

What is this syntax?

Why are we wrapping the code in a function?

Conclusion

Summary

Up next…

sitemap