Programming principles and design · Lesson 30 of 60 · Concept lesson · Optional
Modularity
Modularity means building a program from small, separate parts called modules, each with one clear purpose.
A module can be changed, tested or replaced without rewriting everything else.
Parts connect through a few clear entry points.
Picture it
Code example
function parse(text) {
return text.split(" ").map(Number);
}
function total(numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
function show(value) {
console.log("Total: " + value);
}
show(total(parse("1 2 3")));Output: Total: 6
Three small functions, each with one job, joined in one line.
def parse(text):
return [int(part) for part in text.split()]
def total(numbers):
return sum(numbers)
def show(value):
print("Total: " + str(value))
show(total(parse("1 2 3")))Output: Total: 6
Three small functions, each with one job, joined in one line.
Check yourself
Pick an answer for each question. You can retry as many times as you like.
Loading the questions…