Principles of programming
This topic covers:
Standardisation of programming languages
Natural language & Syntax
High level & Low level languages
Paradigms are the fundamental structures and approach of each programming language. This topic introduces the ones we need to know about
These types of language are composed of commands intended to be ran one after another, being made up of iterations, selections, functions & procedures. These languages allow for the change of state of variables throughout the program. Examples include C and Python, with these types of languages being the most commonly learnt. They are great in many cases, but will lack use for tasks such as GUI problems.
These types of program rely heavily on user actions. The program waits for the user to do something and once they do, a function will run to perform the required task. This function is known as a listener. This means code is not written linearly, but rather in sections that can be triggered based on the input. The way it works is that the OS will process the input as normal and then send the event to the application event queue, where it is then sent to the relevant listener to provide an output. Python and VB.Net are examples.
These are often used for beginners learning to program. A common example is Scratch. The user is provided with visual commands, meaning only certain lines of code can be written, highly reducing the risk for errors and also showing the user the kinds of commands possible within a language. They are not suitable for larger, advanced programs due to the limit of code that can be written
These are used in web technologies; commands are added to text to give a meaning to the text by giving commands to the program reading the file on how it should display the text to the user.
This is by far the most common paradigm used in modern programming projects, as it allows programs to be well structured and reusable. As with procedural programming, OO allows for selection, iteration, identifiers & constants. However, the main difference is that OO replaces procedures with classes, objects and methods. It also uses Attributes, inheritance and encapsulation.
Objects & Classes
A class is the definition of an object, embodying all the attributes and operations needed to create and manipulate objects of a certain type. It consists of a description (attributes) and actions (methods). An object is all the data and actions that can operate on that data. An easy way to consider the two, is through an analogy. Let’s say we see a car coming down the road. We instantly know that that it is a car, but may not know the type or model etc. A class would represent the car, whilst the object would be the make of the car, the type of car, the cost of the car etc.
Methods & Attributes
A method is a sub-routine, contained within an object, that performs a specific task on data within the object. Using the car example again, a method could be ‘TurnLeft’. An attribute is a feature of a class. They are pieces of data about the class & considered metadata. They are set to be equal to an object. For example, saying the car make (object) is a jaguar (attribute) and the cost is equal to £30,000.
This is also known as ‘data hiding’, meaning that data belonging to a class can be accessed only by methods belonging to that class. This is can useful because:
Allows classes to be used between programs & reused in new programs
Once a class is imported, all its public methods also become available for use in the program
Anyone using the class only needs to be able to understand the interface and know its methods and attributes.
Can be used to implement data structures, such as stacks and queues.
We all inherit genes from our parents, and inheritance works in a similar way in OO programming. Say we have 2 classes, a van and a car. Both classes would contain very similar objects, such as accelerate, currentgear and brake. Therefore, rewriting the same operations twice, for both classes would be very inefficient and waste memory. To fix this, a superclass is created, in this case called Vehicles. This class would consist of all the methods and attributes needed for a car and a van and class van and class car will now ‘inherit’ these methods and attributes. Van and car are now subclasses, but can also have subclasses within them, creating a hierarchy. Each class can also have additional methods specific to themselves.