2.1.2 Thinking Ahead Flashcards
Thinking Ahead
Thinking Ahead : Identify desired output and work back to find inputs needed to solve the problem and helps identify preconditions.
- Important to consider preconditions : Requirements that must be met before program can be executed (e.g. Check stack is not full before adding item)
- Preconditions ensure necessary checks are carried out before execution of subroutine so no errors occur.
- Also Includes identifying data that can be Cached so faster access speeds to frequently used data and instructions.
-Also includes identifying reusable components that can be called multiple times or packaged into libraries so less code needs to be written.
Thinking Ahead - Preconditions
Preconditions : Requirements that must be met before program can be executed (e.g. Check stack is not full before adding item or list must be sorted for Binary search or non - empty values/lists)
- Preconditions ensure necessary checks are carried out before execution of subroutine.
Advantages :
- Can make program components reusable - clearly documenting preconditions helps make functions reusable - explicitly ensure preconditions are met.
- Cutting out unnecessary checks - clear statement of preconditions helps programmer know what checks are needed before calling sub-procedure and which checks are unnecessary.
- Helps make program easier to debug and maintain - clearer shorter programs.
Thinking Ahead - Caching
Caching : The process of storing frequently used data in cache memory after they have been used in case it is needed again, allowing for faster access for future use.
- Can be web caching which stores HTML pages and images or CPU caching
Advantages :
- Faster access for previously searched items
- Saves on costly bandwidth
- Reduced load on web services
Disadvantages :
- Relies on same data being needed multiple times
- increased complexity of programs to make use of caching
Thinking Ahead - Reusable Components
- Can decompose problem into smaller simpler tasks and think ahead about how each task can be solved and reuse program components developed in past / externally and package into libraries for future use.
- Identify Components needed in more than one place and write as independent modules.
- Can program as self contained subroutines or classes - both can be reusable.
Advantages :
- Commonly used functions packaged into libraries - can be reused across multiple programs and projects which saves time.
- Reusable components may have already been tested - saves time as do not need to be debugged again.
- Saves times as code does not need to be rewritten
- More efficient code which will use less memory//be easier to maintain
- May require less technical knowledge as code can used rather than written.
Problem Decomposition Advantages
Procedures can be re-used in same program or packaged into libraries
- No need to reprogram/saves time
Program can be split between programmers and worked on in parallel.
- Can specialize in their area
Speed up completion time
- As multiple procedures worked on concurrently
Easy to test/debug and maintain
- As each module can be tested on its own then combined.
- and smaller sun problems easier to understand and solve