🧭Programming Paradigms
Styles of writing code: objects, functions, async code and types · 42 terms
Paradigm Basics
3The main styles of writing code, and how they differ.
A way to shape your code. One style uses objects; one uses small functions.
Writing code as a list of steps that change values one at a time.
Writing code that says what you want, and lets the tool work out how.
Object-Oriented Programming
16Building programs from objects that hold their own data and actions.
Code built from objects. Each object holds its own data and actions.
Classes and Objects
9The plans, the things made from them, and what they can do.
A plan for making objects. It lists their data and their actions.
One real thing made from a class, with its own values. Also called an instance.
A function that belongs to an object. It can use that object's data.
A method that belongs to the class, not to any one object.
A method that runs when a new object is made. It sets the first values.
A list of methods a class promises to have. It holds no code inside.
A named value kept in an object, like a dog's name or age.
The word a method uses to mean the object it was called on.
A simple type that groups a few named values, like x and y of a point.
Core Principles
6The big ideas behind object-oriented design.
Hiding an object's data. Other code must use safe methods to change it.
A word like public or private that says which code may use a part.
A new class that gets all of an old class's parts, then adds its own.
One name that works on many kinds of thing, each doing it its own way.
Showing only what you need to use a thing. The details stay hidden.
Building an object from the parts it holds. A car has an engine.
Functional Programming
9Building programs from small functions that change nothing outside.
Code that hands back new values from small functions. Nothing changes in place.
A function that gives the same answer for the same input. It changes nothing else.
Work a function does on the side. Saving a file is one; so is printing.
Data that cannot change once made. To change it, you make a new copy.
A function that takes another function as input, or gives one back. map is one.
Three list tools. Map changes each item, filter keeps some, reduce makes one value.
A function treated as a value. You can store it, pass it or return it.
A small function in one line, often with no name. You use it once.
A function that keeps the variables near it. It still has them later.
Asynchronous Code
8Starting slow work and carrying on, then handling the result later.
Code that runs one line at a time. Each line waits for the last to finish.
Code that starts a slow task and keeps going. It handles the result later.
A function you give to other code. That code runs it later.
An object for a result not ready yet. It ends in a value or an error.
Async code that reads top to bottom. await waits for each result.
The loop that picks which waiting job runs next. It keeps one thread busy.
An object that hands out items one at a time, each time you ask.
A function that pauses at each yield and hands out one value at a time.
Types
6Rules for the kind of each value, and how a language checks them.
Rules that sort values into kinds. They check how each kind is used.
A note that says what kind of value a name holds, like count: number.
The language works out each value's type. You do not have to write it.
Code written once that works with many types. A stand-in like T fills in.
A type rule. A value can be empty only if its type says so.
A type for a value that can be one of a few kinds. It may be text or a number.