Programming · Design Considerations & Programming Practices
up to +100 XP
Stop 2.9 · Programming · 3 min

Design Considerations & Programming Practices

Learn best practices for writing quality code

Learning objectives:
  • Determine the logical order of actions and order of operations (2.9.1–2.9.2)
  • Choose data structures and order actions for execution time and memory (2.9.3–2.9.4)
  • Know naming conventions and code style (2.9.5–2.9.6)
  • Judge the suitability of an algorithm (2.9.7)

Logical order of actions

Steps must happen in an order that makes sense: input the values, then validate them, then calculate, then output. Getting this wrong gives wrong answers or errors.

Order of operations

Python follows mathematical precedence: brackets, then exponentiation, then multiplication and division (including // and %), then addition and subtraction.

print(2 + 3 * 4)      # 14
print((2 + 3) * 4)    # 20
average = (a + b + c) / 3   # without brackets only c would be divided

Use brackets to make outputs accurate and to avoid errors.

Efficiency

Choosing data structures

Ordering actions

Naming conventions

Pick one convention and use it consistently.

Code style and readability

Judging an algorithm

When asked if an algorithm is suitable, consider whether it meets the requirements, how efficiently it uses storage and execution time, whether the data structures, data types, variables and constants are appropriate, and how readable and maintainable it is.

Quick check · 11 questions · up to 100 XP

Check what stuck.

+10 XP each
Question 1

Question 1 of 11

What does (2 + 3) * 4 evaluate to?

Question 2

Question 2 of 11

Why does average = a + b + c / 3 give the wrong answer?

Question 3

Question 3 of 11

Which is an example of snake_case?

Question 4

Question 4 of 11

Which is an example of camelCase?

Question 5

Question 5 of 11

Why use meaningful names?

Question 6

Question 6 of 11

How does keeping a maximum line length help?

Question 7

Question 7 of 11

Which change makes a loop more efficient?

Question 8

Question 8 of 11

Which data structure finds a value by a label most efficiently?

Question 9

Question 9 of 11

Which is part of judging whether an algorithm is suitable?

Question 10

Question 10 of 11 · Fill in the blanks

Writing words in lower case joined by underscores, such as total_cost, is . Writing totalCost is .

Question 11

Question 11 of 11 · Match the pairs

Match each style convention to how it helps readability.

Meaningful names
White space
Maximum line length
Consistent naming