Programming: Turn a Problem into a Reliable Procedure
Programming turns a problem into explicit steps that a computer can execute.
- Author
- absequ
- Published
- Updated
- Category
- Programming
- programming
- fundamentals
- practical guide
Programming turns a problem into explicit steps that a computer can execute. Good programs state what inputs they accept, what result they produce and what happens when an assumption fails. A short, understandable solution is often a better starting point than a clever one.
On this page
Key concepts
Values represent information; variables give values names. Conditions choose between paths, loops repeat work and functions package a task behind inputs and a result. Data structures determine how information is organized. Separate calculation from input and output so you can reason about each independently.
Practical example
Suppose a workshop allows at most twelve attendees. Write a function that computes remaining places from a list of confirmed names. Decide whether duplicates represent the same person and whether cancelled entries belong in that list before writing code. These are requirements, not details a language can decide for you.
The example below accepts a nonnegative integer count and rejects inconsistent inputs rather than silently reporting a misleading result. With capacity 12 and confirmed 5 it returns 7; with confirmed 13 it raises ValueError. The function does not read a database or print messages, so another layer can decide how to present the result.
def remaining_places(capacity, confirmed):
if type(capacity) is not int or type(confirmed) is not int:
raise TypeError("Counts must be integers")
if capacity < 0 or not 0 <= confirmed <= capacity:
raise ValueError("Counts are inconsistent")
return capacity - confirmed
print(remaining_places(12, 5)) # 7Common mistakes
Avoid catching every exception and pretending success. Syntax errors prevent parsing; runtime exceptions can arise even in valid syntax. Handle expected failures near the place that can recover, and preserve useful context for unexpected failures without logging private data.
Practice
Try zero capacity, a full workshop and a negative count. Explain the expected result before changing the code. Then extend the design to represent cancellations, keeping the core calculation independent of storage.