liveBook · Manning

Chapter 10: First-class functions: Part 1

What you will learn in chapter 10 of Grokking Simplicity

This introduction previews first-class functions and the code smell and refactorings the chapter will teach.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction previews first-class functions and the code smell and refactorings the chapter will teach.

10.1 Code smell: Implicit argument in function name

Introduces a code smell that hides values in function names and outlines two refactorings that make values or behavior first-class.

Key terms: code smell, implicit argument in function name, Express implicit argument, first-class, hard-coded value, Replace body with callback, before, body, and after, first-class function, higher-order functions

10.2 Marketing still needs to coordinate with dev

The section shows why the shopping-cart abstraction barrier has not given marketing the independence it was meant to provide.

Key terms: abstraction barrier, API, marketing team, dev team

10.3 Code smell: Implicit argument in function name

The section identifies a code smell in similar cart-update functions whose field names are embedded in their function names rather than passed as arguments.

Key terms: implicit argument in function name, field name, duplication, code smell

10.4 Refactoring: Express implicit argument

This section shows how to turn a field hidden in a function name into an explicit argument, making the field name a first-class value and simplifying the API.

Key terms: express implicit argument, implicit argument, explicit argument, setPriceByName(), setFieldByName(), first-class value, field name, API specification, abstraction barrier, generic setFieldByName()

10.5 Recognize what is and what isn’t first-class

Defines first-class values in JavaScript, contrasts them with language constructs that are not values, and shows how a string argument made part of a function name first-class.

Key terms: first-class, first-class value, non-first-class, string argument, fields on objects

10.6 Will field names as strings lead to more bugs?

This section explains how to catch typos when field names are passed as strings.

Key terms: field names, compile-time checks, run-time checks, static type system, TypeScript

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

This section explains how first-class field names can preserve an API’s abstraction barrier, then practices making field names explicit and checking which fields an API accepts.

Key terms: first-class field name, abstraction barrier, API spec, implicit argument in function name, express implicit argument, explicit argument, run-time checks, field name, 'size', 'quantity'

10.8 We will use a lot of objects and arrays

The section explains why the book represents general entities with generic data structures, while acknowledging the tradeoffs of interpreting data and the limits of type checking.

Key terms: data orientation, generic data structures, bespoke interfaces, objects, arrays, statically typed languages, dynamically typed languages, JavaScript, strings, JSON

10.9 First-class functions can replace any syntax

The section shows how functions can turn non-first-class syntax into values and explains how first-class functions make higher-order functions possible.

Key terms: first-class values, operator, function, first-class, higher-order function, argument, callback

10.10 For loop example: Eating and cleaning up

The section refactors two similar array loops into a reusable forEach() function by passing their differing behavior as a function argument.

Key terms: implicit argument in function name, express implicit argument, extract function, forEach(), higher-order function, function as an argument

10.11 Refactoring: Replace body with callback

This section shows how to remove repeated try/catch code by extracting the shared structure into a function and passing the varying body as a callback.

Key terms: replace body with callback, before-body-after, withLogging(), callback, handler functions

10.12 What is this syntax?

The section explains three ways to define a function and clarifies why a function defined where it is passed is called inline and anonymous.

Key terms: globally defined, locally defined, local scope, inline function, anonymous function

10.13 Why are we wrapping the code in a function?

This section explains why wrapping code in a first-class function lets a higher-order function run it later inside a reusable context.

Key terms: first-class, defer the execution, function, withLogging(), try/catch, callback, higher-order functions, context, duplication

10.14 Conclusion

The conclusion recaps the chapter’s introduction to first-class values, first-class functions, and higher-order functions, then points ahead to further exploration.

10.15 Summary

10.16 Up next…

This section previews further uses of higher-order functions and the two refactorings in the next chapter.

Where this is used later

Concepts explained in this chapter

← Chapter 9: Stratified design: Part 2 Chapter 11: First-class functions: Part 2 →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.