Practical software engineering foundations · Lesson 52 of 60 · Concept lesson · Optional
Code readability
Readable code is written for people first, because code is read far more often than it is written.
Clear names, consistent formatting and small functions make the meaning obvious.
A comment should explain why, not repeat what the code already says.
Picture it
Code example
function calc(a, b) {
return a * b * 0.2;
}
function discountAmount(price, quantity) {
const DISCOUNT_RATE = 0.2;
return price * quantity * DISCOUNT_RATE;
}
console.log(calc(10, 2), discountAmount(10, 2));Output: 4 4
Both functions give the same answer. Only the second explains itself.
def calc(a, b):
return a * b * 0.2
def discount_amount(price, quantity):
DISCOUNT_RATE = 0.2
return price * quantity * DISCOUNT_RATE
print(calc(10, 2), discount_amount(10, 2))Output: 4.0 4.0
Both functions give the same answer. Only the second explains itself.
Check yourself
Pick an answer for each question. You can retry as many times as you like.
Loading the questions…