Principles of Programming

You might also like

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 9

10.

Principles of programming
Programming paradigms
A programming paradigm is the fundamental structure and approach of a programming
language. Programs written in a procedural language will run from the start to the end
sequentially unless a control statement, such as a loop or IF.

Procedural
 Procedural languages are used in traditional programming based on algorithms or a
logical step-by-step process for solving a problem
 An imperative language in which program statements can be grouped into self-
contained blocks called procedures and functions
 They obey ordered instructions
 They carry out actions or calculations
 A procedural programming language provides the programmer a way to define precisely
each step when performing a task
 Allows tight control over the underlying operation of the hardware
 Used in large complicated programs where the same procedures are run out at varying
stages of the program execution.

Visual
These make use of intuitive user interfaces to help build programs rather than relying solely on
text. Suitable use for visual language is writing applications that require a GUI or for a visual
environment. One of the first visual languages you are likely to have encountered is Scratch.
Scratch is made up of a series of characters that can have actions applied to them by dragging
and dropping code blocks. Scratch makes use of the event-driven paradigm to allow the
developer to program interaction. By eliminating the need to remember the syntax of a
language, budding developers can spend more time considering the logic of the problem. One
downside of using visual programming languages is that they are not ideal for larger or more
advanced problems. They are limited due to the fact that there is only so much screen space
available. With most of it taken up by the user interface, there is not much room left for your
code. Development time can also be longer as typing commands can be much faster for
experienced developers. However, visual programming languages are great for starting to learn
to code.

Event-driven
Programs that rely heavily on user interaction will normally be written using an event-driven
paradigm. The essential premise is that a program will sit in a loop waiting for the user to
perform an action. When an action occurs, a function (known as a listener) will run to process
that action. When a user interacts with an input device, the OS will process it in the usual way
and then send then the event to the applications event queue. The event processor of the
application will then process the event and send it to the relevant listener. Events themselves
tend to take the form of an object or some specialised data structure. Developers are
responsible for creating the code for the listeners and selecting which ones they are interested
in processing. The mechanism of the event processor is normally abstracted away from the
developer. The event processor, in the simplest form, will be a nested loop structure which uses
either IF or select/case statements to direct the event to the correct listener. Pygame, a game
library for Python, is event driven. A code sample below shows the event processor of a simple
Pygame project. The outer loop will listen until the user asks the program to quit. The inner loop
will iterate over the event queue and act upon the events that the program is interested in. The
actual processing of the events occurs in the listener functions. Event-driven programming
requires the developer not to think of their code in a linear fashion but rather as a set of actions
triggered by the user. Listener functions will not run one after another. Graphical user interfaces
and games will always make use of the event-driven paradigm. It is fairly common for most
modern languages to offer some form of event processing. For some languages, like Java and
Python, there is more than one way to implement events.

Mark-up languages
 Creating web pages
 Mark-up languages add commands, or mark-up, to a text document to offer meaning to
the text.
 The commands give instructions to the program reading the file on how to interpret,
format and display the text.
 One of the most common mark-up languages is HTML.
 The commands in HTML are called tags, are surrounded by chevrons.
 Commands are opened, for example, <h1>, so that any text that follows will have that
format applied to it.
 Commands are then ended using a forward slash inside the tag, for example </h1>
 XML (eXtensible Mark-up Language) is another mark-up language that is commonly used
in web applications
 XML is used for structuring and marking-up data for storage rather than information for
display
 The developer is free to create their own tags and specify their own meaning to them
 Mark-up languages are commonly combined with other languages, such as JavaScript
with HTML.

Object-oriented programming
Most modern programming projects use the object-oriented paradigm, so it is important, once
you understand the basics of procedural languages, to move on to object-oriented
programming.
Object-oriented (OO) programming is known as OO or OOP and has gained popularity among
the programming community. Most languages offer some form of OO support, including
languages that were originally designed to be procedural. OO allows programs to be well
structured and potentially offers the potential for reusable code, which is much sought after in
programming. Selection, iteration and assignment, along with identifiers and constants, all basic
to procedural programming, are also available in the object-oriented paradigm. This makes
moving from procedural to OO much more straightforward than it would be otherwise. One of
the key differences, however, is that, in OO, procedures are replaced by classes, objects and
methods, and the whole code is structured and designed differently. Classes and procedures
both offer modularity of code, but classes allow the developer to produce more reusable code
and relate their design more closely to reality.
Three characteristics define an OO language:

1. Classes and objects


In OO, code is developed around objects and classes. An object is a combination of data and the
actions that can operate on that data. A class is the definition of an object; it embodies all the
information needed to create and manipulate objects of a particular type. A car is an example of
a class while a specific car, such as a Jaguar, is an example of an object. The process of creating
an object is known as instantiation and the code below shows how an instance of the class car
could be instantiated.
Car
regNum: String
make: String
model: String Attributes: a description
Accelerate(amount:int) Methods: codes that implements the behaviours of a class
TrunLeft(degree:int)
TurnRight(degree:int)
Brake(amount:int)
2.Encapsulation
Encapsulation is the hiding of the implementation of a class and controlling access to its
methods and attributes. Classes can hide how they work from developers using them in their
programs. Anyone using the class needs only to understand the interface to the class like its
methods and attributes. One common use of OO programming that relies on encapsulation is
the implementation of data structures, such as stacks.
Stack
start:int
list:Node
free:int
Push(data)
Pop():Node
Node
Data
nextNode:Node

3. Inheritance
Car

Inheritance is the means by which properties and methods from a class are copied to another
class.
Vehicles
Accelerate(amount:int)
Brake(amount:int)
CurrentGear():int
Gearup()
Geardown()

Van
Summary:
 Uses objects and classes: include both data and associated processing
 Encapsulation is a technical implementation is hidden within the object
 Inheritance is the means by which properties and methods from a class are copied to another
class
 Polymorphism is a form of overloading which allows us to create general object structures
which can be used with a wider range of data types
 Enables production of buttons or icons which is useful in a visual environment
 A class defines the methods properties data for a group of similar objects
 Once an object is created, knowledge of its implementation is not necessary for its use.
 Objects control how other objects interacts with themselves or prevent other kinds of errors
like setting the width of a window to -500
 In some languages, the programmer places objects on forms. These are event-driven
languages
 An event like click a command button, initiates a sequence of code to be executed
 Objects created using object-oriented languages can easily be reused in other programs
High- and low-level languages
Low-level languages reflect the design of the processor on which they are to run much more
closely than their high-level equivalents. In high-level languages, a lot of the detail of memory
addressing, assigning registers and accessing hardware is abstracted from the programmer; in
tow-level languages the developer has direct access to these. Each CPU architecture has an
instruction set; low-level languages offer a direct one-to-one mapping between low-level
commands and the machine code instructions in this instruction set. The most common low-
level language is assembly code. Conversely high-level languages hide the inner workings of how
things like registers are assigned or where data should be stored in memory. Each line of high-
level code could translate into multiple lines of machine code. Because a lot of the low-level
details are hidden from the developer, they are free to focus their attentions on developing
algorithms. However, they must sacrifice closer control of the hardware in return.
Features:
High-level languages Low-level languages
Each line of code translates into multiple lines of machine Each line of code translates into a single line of machine code
code
Does not give direct access to registers or other CPU features Allows the developer to manipulate registers and the CPU
directly
Instructions are semantic and do not reflect the design of the Instructions are directly linked with the instruction set of the
processor processor and are based on the design of the processor
Reasons of using them:
High-level languages Low-level languages
Easier to understand, learn and program as commands are Tasks connected with the running of the computer (operating
more English like and identifiers can be long and meaningful system) because execution speed is critical or size of code
needs to be small
Availability of powerful commands that perform quite Embedded system because size of code needs to be small or
complex tasks primitive processor with limited instruction set
May reflect the nature of the problem (problem orientated) Real time systems like controlling an aeroplane because
and can be easier to program a solution control is required over the hardware and they have to run
fast and respond immediately
Suitable for general program production Computer games because hardware producing graphics will
need to be programmed and program has to run quickly
Can be translated to run on different platforms
Machine code instructions
When code is written in languages such as Python or Visual Basic, the code is readable and
understandable by humans and is known as source code. Processors have no understanding of
source code and cannot execute it directly unless it is translated into machine code. A translator
is a name given to software that takes source code and converts it to machine code. Source
code, written as a text file, allows the software to be created without the need to understand
the inner workings of the instruction set of the target processor. The programming language
hides a lot of the complexities of the processor, such as memory addressing modes. Each
processor architecture, for example, x86 has its own set of instructions. The compiler must
specify the target architecture before compiling, as the differing architectures are not
compatible. Each line of source code can be compiled into many lines of machine code.
Machine code is the binary representation of an instruction, split into the opcode and the
operand (the instruction's data). Assembly code uses text to represent machine code
instructions, or mnemonics. Each processor architecture has a set of instructions that it can run;
this is known as the instruction set.
Bits 1-6 Bits 7-16
opcode Operan
d
The size of instructions differs from architecture to architecture and between different
instructions. In the example, the instruction is 16 bits in size. Every instruction available, in a
given instruction set, will have a unique binary value known as the opcode. When an instruction
is executed by the processor, it uses the opcode to determine which instruction to run. Note
that ACC stands for accumulator. Each opcode represents a different instruction and mnemonics
are used to make development in assembly language easier, because dealing with binary can get
very complex.
Opcode Assembly mnemonic Description
000001 MOV Moves a value to a register
000010 ADD Adds a value and stores in ACC
000100 SUB Subtracts a value and stores in ACC
001000 MUL Multiples a value and stores in ACC
Attempting (4+5)*2:
Opcode Data Value of ACC
MOV value 0 to ACC 000001 0000000000 0000000000000000
ADD 4 to ACC 000010 0000000100 0000000000000100
ADD 5 to ACC 000100 0000000101 0000000000001001
MUL 2 to ACC 001000 0000000010 0000000000010010
Relationship between machine code and Assembly code
Assembly code is the step above machine code, allowing the coder to write code using
mnemonics, which represent machine code instructions. Each Assembly code instruction has a
one-to-one mapping with a machine code instruction. This is unlike source code, which has a
one-to-many mapping. When converting assembly code to machine code you will use an
assembler.
C language
Although Assembly code is the most obvious low-level language, there are others. The most
notable example is C. C is different to Assembly code in that it can act as a high-level language as
well. It needs to be translated and a single line of code can produce multiple lines of machine
code. C also has the ability to run machine code directly and give direct access to memory
through the use of pointers. C is therefore the language of choice for most applications requiring
a low-level language, as it offers the best of both worlds.

Scenario for low level language


Device driversDevice drivers need to be small and as efficient as possible. You need to be able to manipulate memory
addresses in order to fully control the hardware.
Embedded Software which runs on simple devices such as a washing machine, modern central heating systems or
software any other device with a simple microprocessor. Software on these devices needs to be small and have
direct access to the hardware. They commonly will only have a simple operating system.
Interfacing This runs along the same lines as device drivers. If you create your own hardware, for example a robot,
with custom and connect it to a PC then you will effectively need to write software that can directly control it. This will
hardware require direct access to the connecting cable and the ability to manipulate control signals.

Scenario for high level language


Create web  Ability to insert hyperlinks because navigation between pages will be required
pages  Ability to format text and graphics or ability to create style sheets to make the pages more
interesting and ensure web site meets client specification
 Ability to include platform independent code to allow code to be run on any machine that
displays the web page
 Ability to include video and animation to make web site more interesting and ensure web site
meets client specification
 Ability to include sound to make web site more interesting and ensure web site meets client
specification and play music
Create  Ability to add buttons, menus and frames to allow creation of easy-to-use interface and meet
graphical user client specification
interface  Ability to use colours, icons and images to create GUI that looks or attractive and meet client
specification
 Ability to create or use events for buttons and menus as GUIs are event driven

You might also like