1.4 Programming Paradigms and Languages

Aligned outcomes:

SLO 1

Describe the software development life-cycle.

Choosing a language is a real life-cycle decision, and this section gives you the terms to argue it: languages are purpose driven, and low-level versus high-level is a trade of speed against programmer time. It sets up what the design phase chooses between rather than teaching the phases themselves.

SLO 2

Describe the principles of structured programming.

This is where structured programming gets its definition: sequence, selection and repetition, with no arbitrary jumps, and the claim that any computation can be built from just those three. You leave able to name the principles and say why giving up goto made programs easier to reason about.

Learning Objectives

After this section, you will be able to:

In this section, you will learn to:
  • Explain why so many programming languages exist rather than one.
  • Describe the difference between a low-level and a high-level language.
  • Say what a programming paradigm is.
  • Describe procedural and structured programming, and the three structures every program is built from.
  • Describe object-oriented programming and what it bundles together.
  • Explain what it means to call JavaScript a multi-paradigm language.
Video 1.4 — Why Are There So Many Programming Languages? Four and a half minutes answering the section's opening question with everything it teaches. Captions available.

1.4.1 Why There Are So Many Languages

There are thousands of programming languages, and new ones appear every year. A reasonable first reaction is that this seems wasteful — surely one good language would do?

It would not, and the reason is that languages are purpose driven. A language is designed for a kind of work, and the choices that make it good at that work make it worse at something else:

Notice that this is a list of jobs, not a ranking. Asking which language is best is like asking which tool in a toolbox is best — the honest answer is another question: best for what?

The languages come and go faster than the ideas do. Variables, conditions, loops, functions and data structures appear in nearly all of them, which is why this course is called Programming Concepts rather than JavaScript. Learning your second language is dramatically faster than learning your first, because the second time you are only learning new spellings for things you already understand.

Concepts in Practice: Choosing a language

1. Why can a program written for a web page not simply be written in SQL?

  1. SQL is too slow.
  2. SQL is designed for asking questions of a database, not for building interfaces.
  3. SQL is an older language.
Solution

b. Languages are purpose driven. SQL is extremely good at the job it was designed for and cannot do this one at all.

1.4.2 Low-Level and High-Level

Languages differ in how much detail they make you handle. That is called their level of abstraction.

A low-level language makes you describe the work in terms the hardware understands directly — where each value sits in memory, exactly how each calculation happens. Programs written this way can run extremely fast, and they take much longer to write and are much easier to get wrong.

A high-level language hides those details. You say what you want; the language works out how. Writing is faster, whole categories of mistake become impossible, and you give up some control and a little speed.

Definition 1.4.1: High-Level Language

A high-level language is one with a high level of abstraction: it hides hardware details such as memory addresses, so the programmer describes what should happen rather than exactly how the hardware should do it. JavaScript is a high-level language.

A comparison that holds up well: cooking a meal entirely from scratch gives you control over every ingredient and takes all afternoon. Cooking with prepared ingredients is far quicker and you accept someone else's choices about what is in them. Neither is the right answer in general — it depends on whether the afternoon or the control matters more.

You have been enjoying high-level abstraction since Section 1.2 without noticing:

Editor
runs in your browser
▶ Press Run to see the output…

What you should see:

8

Two lines. Nothing about where 5 and 3 are stored, nothing about how the processor adds them, nothing about how text reaches your screen. In a low-level language, the same result takes considerably more code and a great deal more thought about the machine.

Definition 1.4.1 — A high-level language hides hardware details such as memory addresses, so the programmer describes what should happen rather than exactly how the hardware should do it.

Concepts in Practice: Levels

1. Mai Vang leads a small team that has three weeks to ship a working web app. Which is the better trade-off for her?

  1. A low-level language, because the program will run faster.
  2. A high-level language, because programmer time is the scarce resource here.
  3. Neither — level of abstraction does not affect development speed.
Solution

b. Both trade-offs are real, so the answer depends on what is scarce. Mai has three weeks and a small team, so developer time matters more to her than the last few percent of speed — and a high-level language wins. That describes most web work.

1.4.3 What a Paradigm Is

Two programs can solve the same problem and be organized in completely different ways. A paradigm is a style of organizing a program — a set of ideas about what a program is made of.

Definition 1.4.2: Programming Paradigm

A programming paradigm is an approach to organizing and structuring code. It shapes how a program is broken into pieces and how those pieces fit together.

The three worth knowing at this stage are procedural, object-oriented, and functional. The rest of this section describes each. You will write all three later in the book; the goal here is to recognize them.

Definition 1.4.2 — A programming paradigm is an approach to organizing and structuring code, shaping how a program is broken into pieces and how those pieces fit together.

1.4.4 Procedural and Structured Programming

Procedural programming treats a program as a sequence of instructions carried out in order, grouped into reusable procedures. It is the style you have already been writing:

Editor
runs in your browser
▶ Press Run to see the output…

What you should see:

Total: 60

Four steps, top to bottom, each acting on data held in variables. Data and instructions are separate things: the variables hold the values, the statements do the work.

Structured programming is procedural programming with a discipline attached, and it is the subject of this course's second learning outcome. Its claim is a surprisingly strong one: every program can be built from just three structures.

  1. Sequence — do this, then this, then this. The four lines above.
  2. Selection — choose between paths depending on a condition. Chapter 2 calls this if and switch.
  3. Repetition — do something more than once. Chapter 2 calls this loops.
Definition 1.4.3: Structured Programming

Structured programming is a discipline in which programs are built only from sequence, selection, and repetition, with no arbitrary jumps between parts of the program. Any computation can be expressed with these three structures.

That last claim is the important one. Every program on your computer — the browser, the operating system, a video game — is built from those three ideas and nothing more exotic.

Early languages let a program jump to any line at any time, with an instruction usually called goto. It worked, and it produced programs nobody could follow, because reading one line told you nothing about how you had arrived there. Structured programming was the argument that giving up that freedom made programs possible to reason about. The freedom is rarely missed. This is the first example in the course of a restriction being a feature.

Definition 1.4.3 — Structured programming is a discipline in which programs are built only from sequence, selection, and repetition, with no arbitrary jumps between parts of the program.

Try It Now 1.4.1

Run this code and predict the total before you check the output.

let subtotal = 25;
let tax = subtotal * 0.08;
let total = subtotal + tax;
console.log(total);

Type it into the editor and run it:

Editor
runs in your browser
▶ Press Run to see the output…

What you should see:

27

Which of the three structures does this program use — sequence, selection, repetition, or some combination?

Solution

Sequence only. Three assignments and a print, each running once, top to bottom, with no branching and nothing repeated. There is no if (no selection) and nothing runs more than once (no repetition) — Chapter 2 introduces both.

Concepts in Practice: The three structures

1. Which of the three structures does "keep asking until the password is correct" need?

  1. Sequence only
  2. Selection and repetition
  3. Neither — this cannot be done with the three structures
Solution

b. Repetition to keep asking, and selection to decide whether the password was right. Chapter 2 gives you both.

1.4.5 Object-Oriented Programming

Object-oriented programming (OOP) organizes a program around objects — values that bundle data together with the operations that work on that data.

You met object literals in Section 1.2.7:

Editor
runs in your browser
▶ Press Run to see the output…

What you should see:

JavaScript Guide
200

That object holds data. The object-oriented idea is to put the behaviour in there too — so a bank account object would hold the balance and also know how to deposit into it, rather than the balance sitting in one place and the depositing happening somewhere else.

Definition 1.4.4: Object-Oriented Programming

Object-oriented programming is a paradigm that organizes a program around objects, each bundling data (its properties) with the behaviour that operates on that data (its methods).

The contrast with procedural style is the whole point:

Table 1.4.1 — Procedural versus object-oriented programming compared.
Procedural Object-oriented
Data and behaviour kept separate bundled together
A program is a sequence of steps on data a set of objects that interact
You mostly write procedures objects and their methods

Neither is better in the abstract. Procedural code is direct and easy to follow for a small job. Object-oriented code pays off as a program grows, because each object keeps its own data in order and you can use one without knowing how it works inside.

OOP is the fourth of this course's learning outcomes and the target of Chapter 5, so it is fine — expected, even — to find this description abstract right now. You cannot really see the point of bundling data with behaviour until you have written a program big enough to be annoying without it. Chapter 5 builds one.

Definition 1.4.4 — Object-oriented programming organizes a program around objects, each bundling data (its properties) with the behaviour that operates on that data (its methods).

Concepts in Practice: Bundling

1. In object-oriented style, where does the code that changes a bank balance live?

  1. In a separate procedure, away from the balance.
  2. In the account object itself, alongside the balance.
  3. Balances cannot be changed in object-oriented programming.
Solution

b. That bundling of data with the behaviour that acts on it is exactly what makes the style object-oriented.

1.4.6 Functional Programming

Functional programming organizes a program around functions in the mathematical sense: give one the same input and it always returns the same output, and it changes nothing else along the way.

You will meet this style properly in Chapter 3, where you write functions that take values in and hand results back. Its distinctive habit is avoiding change: rather than modifying a list, a functional program produces a new list and leaves the original alone. Section 3.7 does exactly that.

Functional style tends to make programs easier to test and easier to reason about, because a function that depends on nothing but its inputs can be understood on its own — you never have to ask what else might have happened first.

1.4.7 JavaScript Is Multi-Paradigm

Some languages commit to one paradigm. JavaScript does not: it is multi-paradigm, and supports all three of the styles above. That is unusual and it is a real advantage for a first language, because you can meet each idea without changing languages to do it.

Everything in this book is JavaScript, and you will write all three styles in it:

The important thing is that these are choices, not rules. Real programs mix them, using whichever fits the piece of work in hand. A game might hold each character as an object, use a loop to update them, and transform a list of scores functionally — all in the same file, all in one language.

Try It Now 1.4.2

Both snippets below print the same line, and both are valid JavaScript. Run them and compare.

// Snippet A
let title = "JavaScript Guide";
let pages = 200;
console.log(title + " has " + pages + " pages.");

// Snippet B
let book = { title: "JavaScript Guide", pages: 200 };
console.log(book.title + " has " + book.pages + " pages.");

Type it into the editor and run it:

Editor
runs in your browser
▶ Press Run to see the output…

What you should see:

JavaScript Guide has 200 pages.
JavaScript Guide has 200 pages.

Which snippet is procedural and which is object-oriented? What tells you?

Solution

Snippet A is procedural: the data (title, pages) sits in two separate variables, and console.log — the procedure — reaches in from outside to use them. Snippet B is object-oriented: the same data is bundled into one book value. It does not yet carry a method of its own (methods arrive in Chapter 5), but the data is already organized the object-oriented way. What marks the style is not whether an object appears, but where the data lives relative to the code that uses it — spread across separate variables, or bundled into one value.

Concepts in Practice: Multi-paradigm

1. What does it mean to say JavaScript is multi-paradigm?

  1. It can be written in several human languages.
  2. It supports more than one style of organizing a program.
  3. It runs on several kinds of computer.
Solution

b. Procedural, object-oriented and functional code are all valid JavaScript, and real programs mix them.

Problem Set 1.4

1.4.1 Why can a program for a web page not simply be written in SQL?

Solution

A web page needs an interface the user can see and interact with — buttons, layout, behaviour when clicked. SQL is designed to do exactly one thing: ask questions of a database. It has no concept of a screen, a button, or a click. Languages are purpose driven, and this is the clearest possible example: SQL is excellent at its job and cannot do this one at all.

1.4.2 Mai Vang's team has three weeks to ship a working web app. Which level of abstraction fits her better, and what is she trading away?

Solution

A high-level language, such as JavaScript, fits better. What Mai is trading away is some low-level control and a little raw speed — but with three weeks on the clock, programmer time is her scarce resource, and a high-level language is faster to write in and harder to get wrong.

1.4.3 Which of the three structured-programming structures does "keep asking until the password is correct" need, and why?

Solution

Selection and repetition. Repetition is needed to keep asking after a wrong attempt, and selection is needed to check each attempt against the correct password and decide whether to stop. Sequence alone cannot loop back on a wrong answer.

1.4.4 In object-oriented style, where does the code that changes a bank balance live?

Solution

In the account object itself, alongside the balance. Object-oriented programming bundles data (the balance) with the behaviour that operates on it (the code that changes it) into the same object, rather than keeping the balance in one place and the deposit logic somewhere else.

1.4.5 What does it mean to say JavaScript is multi-paradigm?

Solution

It means JavaScript supports more than one style of organizing a program — procedural, object-oriented, and functional — rather than committing to just one. A real JavaScript program can freely mix all three.

1.4.6 Name three programming languages and the kind of work each was designed for.

Solution

Answers will vary; any three of the following are correct. JavaScript was built to make web pages interactive. Python is popular for data analysis and teaching because it is quick to read. C is used where a program must talk closely to the hardware, such as operating systems and device drivers. SQL is used to ask questions of a database. Swift and Kotlin are built for phone apps, on iOS and Android respectively.

1.4.7 Explain the difference between a low-level and a high-level language in your own words, and give one advantage of each.

Solution

A low-level language makes the programmer describe the work in terms the hardware understands directly, such as where each value sits in memory. Its advantage is speed: programs written this way can run extremely fast. A high-level language hides those hardware details so the programmer says what should happen rather than exactly how. Its advantage is development speed: writing is faster and whole categories of mistake become impossible.

1.4.8 Name the three structures of structured programming and give a one-sentence everyday example of each.

Solution

Sequence — following a recipe's steps in order, one after another. Selection — deciding whether to bring an umbrella based on whether it looks like rain. Repetition — washing each dish in the sink one at a time until none are left.

1.4.9 Explain why giving up the goto instruction made programs easier rather than harder to work with.

Solution

Without goto, a program can only move through sequence, selection, and repetition — so reading any single line tells you something real about how execution could have reached it. With goto, a jump could arrive from anywhere in the program, so a line's neighbors say nothing about the path that led there. Giving up arbitrary jumps trades a rarely-needed freedom for programs that are actually possible to reason about.

1.4.10 Give one difference between procedural and object-oriented programming, and one situation where each is the better fit.

Solution

Procedural programming keeps data and behaviour separate — variables hold values, and procedures act on them from outside. Object-oriented programming bundles data and the behaviour that operates on it into the same object. Procedural style is a better fit for a small, direct job, such as a short script. Object-oriented style pays off as a program grows, since each object manages its own data without the rest of the program needing to know how it works inside.

1.4.11 What is the distinctive habit of functional programming, and why does it make a program easier to test?

Solution

Its distinctive habit is avoiding change: a functional piece of code depends only on its inputs, always returns the same output for the same input, and does not modify anything else along the way. That makes it easier to test, because a function that cannot be affected by outside state — and cannot affect it either — can be tested completely on its own, with no need to set up or check anything beyond its inputs and its return value.

1.4.12 This course is called Programming Concepts rather than JavaScript. Explain in one or two sentences why that distinction matters.

Solution

Because the point of the course is the ideas that outlast any one language — variables, conditions, loops, functions, data structures, and the paradigms that organize them — not the specific spelling JavaScript happens to use for them. Once those concepts are understood, learning a second language is mostly a matter of learning new syntax for things already known.

Key Terms

Purpose driven -- The principle that a language is designed for a kind of work, which is why so many exist and why "which is best" is not a well-formed question.

Level of abstraction -- How far a language sits from the hardware; how much machine detail it hides.

Low-level language -- One where the programmer describes the work in terms the hardware handles directly. Fast to run, slow to write.

High-level language -- One that hides hardware details so the programmer says what should happen rather than how. JavaScript is one.

Programming paradigm -- A style of organizing a program; a set of ideas about what a program is made of.

Procedural programming -- Organizing a program as a sequence of instructions acting on separately held data.

Structured programming -- Building programs only from sequence, selection and repetition, with no arbitrary jumps. Any computation can be expressed this way.

Sequence, selection, repetition -- The three structures: in order, choose a path, do it again.

Object-oriented programming -- Organizing a program around objects that bundle data with the behaviour acting on it.

Functional programming -- Organizing a program around functions that always give the same output for the same input and change nothing else.

Multi-paradigm -- A language supporting more than one of these styles. JavaScript supports all three.