Programming principles and design · Lesson 34 of 60 · Concept lesson · Optional
Separation of concerns
Separation of concerns means each part of a program deals with one kind of problem.
A common split is the screen (how it looks), the rules (what it decides) and the data (where it is stored).
When the concerns are separate, a change to one rarely forces changes to the others.
Picture it
Code example
function priceWithTax(price) {
return price * 1.1;
}
function format(value) {
return "$" + value.toFixed(2);
}
console.log(format(priceWithTax(10)));Output: $11.00
priceWithTax is a rule and format is presentation. You can change the tax rate or the currency symbol independently.
def price_with_tax(price):
return price * 1.1
def format_price(value):
return f"${value:.2f}"
print(format_price(price_with_tax(10)))Output: $11.00
price_with_tax is a rule and format_price is presentation. You can change the tax rate or the currency symbol independently.
Check yourself
Pick an answer for each question. You can retry as many times as you like.
Loading the questions…