2.2.1 Programming Techniques

Cards (31)

  • Programming Constructs
    • Sequence: executed one after the other, in order presented
    • Branching (Selection): decision made based on state of a boolean expression
    • Iteration: repetition - count controlled and condition controlled loops
  • Recursion
    • programming construct in which a subroutine calls itself during its execution
    • continues until a certain condition - called the stopping condition - is met, at which point the recursion stops
  • Advantages and Disadvantages of Recursion
    • represented in fewer lines of code, which makes them less prone to errors
    • inefficient use of memory
    • If the subroutine calls itself too many times, there is a danger of a stack overflow, which is when the call stack runs out of memory. This would cause the program to crash.
    • difficult to trace, especially with more and more function calls
  • Global and Local Variables
    • Variables can be defined with either global or local scope.
    • Scope refers to the section of code in which the variable is available
  • Local Variables
    • Limited scope
    • only be accessed within the block of code in which they were defined
    • If a local variable is defined within a subroutine, it can only be accessed within that subroutine
    • multiple local variables with the same name can exist in different subroutines and will remain unaffected by each other
    • better for memory - deleted when program completed
  • Global Variables
    • accessed across the whole program
    • useful for values that need to be used by multiple parts of the program
    • using global variables is not recommended because they can be unintentionally overwritten and edited
    • not deleted until the program terminates
    • require more memory than local variables which are deleted once the subroutine has been completed
  • Modular Programming
    • programming technique used to split large, complex programs into smaller, self-contained modules.
    • making a problem easier to understand and approach
    • modular design makes it easier to divide tasks between a team and manage, each component can be dealt with individually
    • improves the reusability of components, as once a module has been tested, it can be reused with confidence
  • Top-down approach
    • problem is continually broken down into sub-problems, until each can be represented as an individual, self-contained blackbox which performs a certain task
  • Top-down approach also known as stepwise refinement
    • These modules form blocks of code called subroutines, categorised as either functions or procedures
    • each named blocks of code perform a task
  • Procedures do not have to return a value
  • Functions must always return values
  • Parameters
    Values passed into a function
  • Parameter Passing by Value:
    • treated as a local variable
    • a copy of the value is passed to the subroutine and discarded at the end
  • Parameter Passing by Reference
    • address of the parameter is given to the subroutine
    • so the value of the parameter will be updated at the given address
  • Integrated Development Environment (IDE)
    a program which provides a set of tools to make it easier for programmers to write, develop and debug code
    e.g IDLE and Microsoft Visual Studio
  • Features of an IDE
    • Source code editor
    • Stepping
    • Variable Watch
    • Breakpoint
    • Debugging tools
  • Source code editor
    • autocompletion of words, indentation, syntax highlighting and automatic bracket completion
    • making the coding process easier
  • Stepping
    • allows you to monitor the effect of each individual line of code by executing a single line at a time
  • Variable Inspection, Watches, Watchpoints
    • Allows programmers to inspect the value of variables while debugging
    • If there are particular variables you want to keep an eye on, you can set a variable watch which keeps programmer updated on their values
    • Watchpoint which pauses execution when variable reaches certain value
  • Breakpoint
    • users to set a point in the program at which the program will stop to let programmer try and detect where and why errors are occurring
  • Debugging tools
    • run-time detection of errors with a guide as to where in the code they are likely to have occurred through line numbers and highlighting
  • Refactoring tools
    • process of improving code without changing its functionality
  • Stack inspection
    every time subroutine is called, current state of program put onto a stack
    allows programmer to see chain of subroutines which have been called
    • A class is a template for an object
    • defines the state and behaviour of an object
  • Attributes
    Define state of the object
    Gives an object properties
  • Methods
    Defines behaviour of the object
    Describes actions it can perform
  • Instantiation
    • a process where classes can be used to create objects
    • An object is an instance of a class
    • a class can be used to create multiple objects with same set of attributes and methods
  • Encapsulation
    • attributes are declared as private so can only be altered by public methods
    • implements the principle of information hiding
    • protects data from being accidentally edited
  • Polymorphism
    a concept in programming that allows objects to take on different forms or behaviours.
    • Different objects can share the same name or behaviour but can work in different ways
    • It helps make code more flexible, reusable, and easier to maintain
    • Sub class inherits from parent class
  • Creating a Constructor
    • public procedure new(pAttribute1, pAttribute2, pAttribute3)
  • Modular Programming:
    • the breaking down of a program into subroutines
    • helps with readability and maintainability of the code