2.1.2 Thinking Ahead Flashcards
Why is thinking ahead good
-maximise code efficiency
-minimise errors
Examples of outputs
Visual (on-screen, printed, etc.)
Audio
Haptic feedback
Data to be input into another process or system
First step of thinking ahead
Determine
What inputs the system needs.
What outputs the system should produce.
“Write a program to calculate the volume of a fish tank based on its dimensions and report the result to the user.”
Inputs outputs example
Inputs:
Length: Real/Float
Height: Real/Float
Depth: Real/Float
Processes:
Length x Height x Depth
Outputs
Volume: Real/Float
“Write a program that asks the user for the number of students in their class and prompts them to enter each student’s test score within the range of 0 - 100.
It should then output the highest, lowest and average score.”
Inputs:
NumOfStudents: Integer
CurrentScore: Integer
Processes:
TotalScore = TotalScore + CurrentScore
AverageScore = TotalScore / NumOfStudents
• Loop through the array and return the lowest score
• Loop through the array and return the highest score
Outputs:
MinScore: Integer
MaxScore: Integer
AveScore: Real/Float
What is a precondition
“Conditions that already exist and could affect how you devise a solution to a problem.”
Imagine preparing a three-course meal for a group of friends.
What preconditions might you need to consider before you start?
-Do you already have any of the ingredients?
-Specialist cooking equipment
-Costs
-Dietary requirements
-Number of friends
-Available time
Caching
Instructions or data retrieved from secondary storage and placed into main memory often remain there in case they are needed again before the program using them ends.
• Caching results in faster retrieval times, as instructions or data don’t need to be fetched from secondary storage, which is much slower.
Prefetching
data being requested from main memory by the processor before it is actually required.
Prefetching pros and cons
• Although it is much quicker to access items from main memory than secondary storage, it is quicker still to access items from registers or dedicated cache memory located on or near the processor.
• For prefetching to work, clever algorithms need to be designed to predict - with a high degree of certainty - that an instruction or piece of data will be required soon
Caching web
Caching is also used with web pages to reduce the number of requests from the client back to the web server and vice versa.
Pros and cons of caching
-caching is a good thing, improving speed and efficiency.
Cons:
However, there are potential drawbacks - the nature of predictive logic means caching algorithms can be very complicated to implement.
The wrong data is often fetched and cached - and subsequently, it has to be removed or flushed.
Maintaining the correct sequence of instructions or data items in these circumstances can be problematic.
Reusing code pros
Shortens development time
Saves systems resources
Lowers development costs
Reduces redundant code
• All of this can be achieved by reusing existing sections of code that have been tested by a previous developer.