Programming paradigms Who came first? The chicken or the egg? Did a computer come pre-coded, or did someone write the code for it? If so, what did they write it on?
Paradigms A Paradigm can be thought of as a philosophical or theoretical framework. This framework is then used as the basis of a new way of viewing problems. This topic investigates the three major programming paradigms:
Imperative (or procedural)
Logically, these two books should contain the sum of all human knowledge.
The Towers of Hanoi game is a good way to demonstrate the difference in paradigms.
Download the worksheet on programming paradigms as a Google Doc here.
Imperative paradigm Development of the imperative paradigm Imperative languages use sequencing, decisions and repetition as their main problem solving methods. Variables are created to store data and be processed.
The imperative or procedural paradigm on which languages such as Pascal, C, Fortran and BASIC are based, restrict the developer in many ways. Most notably, a function can only accept data as input, and only return data as output. As human beings, we do not accept input and produce output. The human brain is able to make inferences and deductions based on experience and intuition. The imperative paradigm also requires every single element of code to be completed in detail before the software will operate.
Imperative programing has evolved over the years from lower-level languages that accessed computer's hardware functions with assembly and machine language. Computers are now powerful enough to engineer ways to speed up the process...
Object oriented paradigm Using an object oriented approach, involves considering each component of the problem as a self-contained unit. Each unit is able to remember things (data/attributes), and do things (methods/operations).
Understanding object oriented There are a number of terms in the object oriented paradigm and these are defined here with some examples from the C# code and designer interface in Visual Studio
Class A class is commonly known as the blueprint or concept. It describes what something is, such as the idea of a button. Classes will exist for different parts of the program. Many languages have pre-defined classes. All classes define two things attributes and behaviours - or better known as properties and methods.
Object An object is created from a class. It is the element created with the properties and methods defined by the class. An example of an object is a button with the name 'Exit' that closes the program.
Encapsulation The principle of encapsulation is essential to the OOP. It establishes a firewall between the user of an object and the code implementing it, thus achieving information hiding. The user does not need to know how the data or attributes of an object are defined. The picture below is taken from the designer view of Visual Studio - where the information is hidden from the developer's primary code screen. There are further elements hidden that are not able to be shown.
Instantiation When a user instantiates something, they are creating an instance of an object. An instantiated object is given a name and created in memory using the structure described within a class declaration. In C++ and other similar languages, to instantiate a class is to create an object.
Polymorphism Polymorphism is when the programmer has the ability to creates a method that can be performed at different times, with different sets of data, and at any time during the execution of the program. Programmers have the ability through polymorphism to process objects from different sub classes.
Logic paradigm From a logic approach, we focus on facts stored in memory called the knowledge base. The logic paradigm is largely utilised in the construction of artifical intelligence. Once facts have been established through a series of conditional statements, an inference engine carries out processing on a particular problem.
The inference engine applies the knowledge contained with the knowledge base to reach concusions about goals. It does so in an organised systemic manner. The logic paradigm can effectively make conclusions based on a question.
One of the most commonly used logic based programming languages is Prolog (programming - logic). The program is available to download and interact and this version is today, known as Strawberry Prolog.
Predicates In the logic paradigm, we use predicates instead of functions to assign rules. This gives us more flexibility because relations treat arguments and answers in the same way. The inference engine makes no assumptions and shows no prejudice towards what data can be computed.
Forward Chaining vs Backward Chaining As the names imply, forward chaining involves moving from a problem to a solution, with the inverse occuring in backward chaining.
Given a series of statements, FC means the inference engine toggles through all its facts in the knowledge base matching up criteria that the problem has identified. It repeats this process over and over gathering new intel as it goes, until it reaches a rule that proves the problem correct or incorrect.
From a BC perspective, the inference engine identifies the criteria that must exist for the problem to prove true, and steps 'backwards' up the rules of the knowledge base until it finds statements that match the current position of the question.
It is also worth noting that Forward Chaining can result in multiple results.
Expert Systems An expert system is used to perform functions that would normally be performed by a human expert in the field. They use knowledge bases to support decision making.
A knowledge base is a technology used to store complex structured and unstructured information used by a computer system. The initial use of the term was in connection with expert systems which were the first knowledge-based systems.
A neural network is able to operate heuristically, which means it can learn new things or develop new ideas and conclusions from its existing knowledge base. Neural networks are composed of neurons (similar to biological neurons in brains). As expert systems can not learn new things, they are different from neural networks. See the page on artificial intelligence for some further information on expert systems.
Programmer productivity The overriding motivation for introducing programming languages based on different paradigms is to increase programmer productivity. Some approaches will inhibit and some will promote the productivity, so management must weight up the positive and negative effects of a particular language choice.
The most common areas influencing productivity are:
Speed of code generation
Approach to testing
Effect on maintenance
Efficiency of solution once coded
Learning curve or training required
Remember that whatever language is used, programmers can still write efficient or rubbish programs.
Speed of code generation Programming languages that increase the speed of code generation must increase the productivity of programmers. The same can be said for the choice of paradigm. For instance, a program requiring interaction between objects that contained data and methods would clearly benefit from a object oriented programming language.
Approach to testing It could be argued that the largest gains in productivity come from the approaches to testing - whether it be unit testing or testing individual modules. Since imperative languages can not be tested until the full solution is complied, much time could be wasted attempting to fix errors. Whereas in an Object Oriented language, modularised testing is encouraged which then translates to saved time. Classes that have been thoroughly tested, and subsequently duplicated need not be retested over and over. Essentially, they could be considered black-box tested.
Languages than can encapsulate their data in such a way that it cannot be altered by other processes will greatly reduce the magnitude and breadth of the final test data sets. OOP languages not only encourage data encapsulation, they often enforce it.
Effect on maintenance Large software companies are reluctant to develop products using languages that are not widely understood. This is a reasonable response given the number of programmers available to maintain the code. As maintenance requires modification of changed requirements, implementation and updates of user support, any maintenance is going to be time consuming.
Efficiency of solution once coded Program efficiency is typically measured in terms of speed. Since technology is still based on on the Von Neumann architecture, programming languages that interact direct to maching code are always going be more efficient. For this reason, imperative languages take the lion's share of efficiency. Other languages are in a sense crippled by their ties to physical hardware.
Learning curve or training requirements Some languages (including logic) have not gained wide acceptance in the software development field. Most universities and schools teach object oriented languages due their popularity in the industry, and thus the compounding snowball effect is that the majority of graduates are familiar with this type of programming paradigm.
It is not an easy task to learn any new programming language, but when the paradigm on which it is based is also new, the problem is compounded.
There is no doubt that software developers and the greater community will benefit if they can find the time to examine and learn new ways of doing things. Note that entrepeneurialism thrives in an industry where everyone else is conforming to the norm.
There are a number of coding applications in both Logic and Object Orientated paradigms. This Google Doc requires you to manipulate and write in languages based on both paradigms, and then view the past HSC questions and exercises on this Google Doc. Once you have a good understanding of all the techniques for defining and analysing a problem, attempt the scavenger hunt starting on this Google Doc.