liveBook · Manning

Specification

As explained in Functional Programming in JavaScript

A formal or informal description of the required behavior or properties of a software system or component.

A specification describes the behavior a function or system must satisfy. Good specifications state universal rules rather than isolated if-then cases, so they also clarify what must not happen and avoid boundary-condition gaps. section 6.4

From a grade rule to a testable property

Instead of writing “if the average is 90 or above, award an A,” write “only an average of 90 or above awards an A.” A QUnit test can capture the resulting runtime specification for computeAverageGrade, while JSCheck can generate many valid grade arrays and verify the property repeatedly. section 6.4

QUnit.test('Compute Average Grade', function (assert) {
    assert.equal(computeAverageGrade([80, 90, 100]),'A');
    assert.equal(computeAverageGrade([80, 85, 89]), 'B');
    assert.equal(computeAverageGrade([70, 75, 79]), 'C');
    assert.equal(computeAverageGrade([60, 65, 69]), 'D');
    assert.equal(computeAverageGrade([50, 55, 59]), 'F');
    assert.equal(computeAverageGrade([-10]),        'F');
});

The test lists grade boundaries from A through F and includes a negative input, making the expected behavior concrete. section 6.4

How Functional Programming in JavaScript covers it

How deep it goes
Explains case-based versus universal wording, side effects, context assumptions, and property-based verification with generated inputs.
Examples
Uses grade classification tests, generated grade arrays, custom Social Security number generators, and QUnit-JSCheck integration.
What you can do after
Apply it via QUnit and JSCheck to express properties, generate cases, and detect invalid assumptions.
How it connects
Connects specifications with unit testing, property-based testing, referential transparency, and side effects.

Also explained in

Compare how 4 books explain specification →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.