Welcome to "Thinking Ahead"!

In this chapter, we are going to explore how programmers plan their work before they even touch a keyboard. Think of it like a professional chef preparing for a busy dinner service: they don't just start cooking; they check their ingredients, prep their tools, and think about how to speed things up when it gets busy. In Computer Science, we call this Thinking ahead.

By the end of these notes, you’ll understand how to identify what a program needs to start, what it should produce, how to handle requirements, and clever ways to make programs run faster through caching and reusability.


1. Identifying Inputs and Outputs

Every computer program is essentially a processing machine. It takes something in, does something to it, and gives something back.

What are Inputs?

Inputs are the data or signals that are put into a system. These aren't always just things typed by a user! Inputs can come from:

  • User input: Typing a name, clicking a button, or using a game controller.
  • Sensors: A thermometer reading or a GPS location.
  • Files/Databases: Reading a saved high score or a customer list.

What are Outputs?

Outputs are the results produced by the system after processing the inputs. Examples include:

  • Visual: Information displayed on a screen or a printed receipt.
  • Physical: A robot arm moving or a heater turning on.
  • Data: Saving a file or sending an email.

Real-world Analogy: Think of a vending machine. The inputs are your money and the code you type (e.g., "A1"). The output is the delicious snack that drops down.

Quick Review:

To identify inputs and outputs, always ask yourself: "What does the computer need to know to start?" and "What is the final result the user wants to see?"


2. Determining Preconditions

Preconditions are requirements that must be met before a program or a specific function can execute successfully. If preconditions aren't met, the program might crash or give a wrong answer.

Why do we need Preconditions?

Setting clear preconditions makes programs more reliable and easier to debug. Instead of checking for errors inside a complex function, you make sure everything is perfect before the function starts.

Example: If you are writing a function to calculate a square root, a precondition is that the input number must not be negative (unless you're working with complex numbers!).

Common Mistakes to Avoid:
Don't confuse preconditions with the code itself. A precondition is a state that must exist before the code runs. For example, for a Binary Search to work, the list must already be sorted. "The list is sorted" is the precondition.

Key Takeaway:

Preconditions are like the "Terms and Conditions" of a function. If they aren't met, the function won't guarantee a correct result.


3. The Power of Caching

Caching (pronounced like "cashing") is the process of storing data that has been used once in a temporary, high-speed storage area so that it can be accessed much faster if it is needed again.

How it works

Imagine you are doing homework and keep needing to look up the same definition in a massive textbook. Instead of walking to the bookshelf every time, you write the definition on a sticky note and put it on your desk. That sticky note is your cache.

Benefits of Caching:

  • Speed: Retrieving data from a cache is significantly faster than fetching it from the main source (like a hard drive or the internet).
  • Efficiency: It reduces the workload on the Central Processing Unit (CPU) and saves internet bandwidth.

Drawbacks of Caching:

  • Complexity: Deciding what to store and when to delete it is hard for programmers.
  • Stale Data: If the original data changes but the cache isn't updated, the program will use "stale" (old/incorrect) information.

Did you know? Web caching happens every time you browse. Your browser saves images from websites you visit often so it doesn't have to download them every single time you refresh the page!

Summary of Caching:

Cache = Speed. It's great for performance, but you have to be careful that the data doesn't become out of date.


4. Reusable Program Components

In the "Thinking ahead" stage, a good programmer looks for ways to write code once and use it many times. These are called reusable program components.

Types of Reusable Components:

  • Functions and Procedures: Small blocks of code that perform a specific task (e.g., calculating tax).
  • Libraries: Collections of pre-written functions that anyone can use (e.g., a "Math" library for trigonometry).
  • Subroutines: Specific sections of code that can be "called" from anywhere in the program.

Benefits of Reusability:

1. Saves Time: You don't have to "reinvent the wheel."
2. Easier Testing: If a component is reused, it has likely already been tested and proven to work.
3. Standardisation: It ensures that the same task is performed in the same way throughout a large project.

Don't worry if this seems tricky at first... Just remember that reusability is like using Lego bricks. You don't mould a new plastic brick every time you want to build a house; you use the standard bricks you already have!

Memory Aid: The "T.E.D." Rule for Reusability

T - Tested (Already works)
E - Efficient (Saves coding time)
D - Dependable (Consistent results)


Quick Review Quiz

Check your understanding with these three questions:

1. If a program asks for your "Date of Birth," is that an input or an output? (Answer: Input)
2. What is the main danger of using a cache? (Answer: Stale or out-of-date data)
3. Why is it a good idea to use pre-written libraries? (Answer: They are already tested and save development time)

Final Tip for the Exam: When asked about "Thinking ahead," always mention that it's about planning for efficiency and reducing future work.