Programming Language Paradigms
There are 4 major paradigms or models of programming languages namely,- Imperative:
- Object Oriented:
- Declarative:
- Functional:
It is all about changing the program state. Consider the following simple C snippet,
int i = 25;
i = sqrt(i);
The object(or variable) 'i' changes state(or mutates) from 25 to 5 and the objective was to find the square root of a given number.
The main focus is on data rather than on procedures. Data is represented by means of objects and data is manipulated by means of functions.
Any 'Class' from a C++ or Java text book would serve as a classic example here.
This paradigm of programming perhaps suits those 'business' oriented people who are concerned more about 'what they want' than on 'how they want it done'.
Classic example would be any SQL query.
SQL> Select c.name from customer_db c where c.city = 'london';
The above query says - "Get me the names of all customers who reside in london" and note that nothing is said about "how to do that".
Perhaps the most fancied paradigm amongst the computing circle, the functional paradigm is devoid of concepts like "state", "mutable", "data", "object" or whatsover.
Every statement of a functional program is evaluated as a Mathematical expression. Functions are the only means of "getting things done" aka computing whatever you need.
Functions are considered as first class objects and they have no side effects. That means functions can be nested, passed as arguements to other functions, returned from other functions etc. and they do not affect any state in memory. Some of the functional languages are devoid of the assignment operator. How cool is that?
Alright thats enough of basics, now a simple puzzle.
Given below is a C# 3.0 code snippet that defines a singleton class 'Tournament' (Object Oriented approach),
Uses a lambda expression (functional approach) to generate a bunch of text boxes on the UI and
Queries (Declarative) from the list of players, the winner of the Tournament.
The question is pretty simple - "Which paradigm does the C# programming language fits in?"
Turns out that C# started out as an OO language but the recent version (3.0) of the language has adopted some constructs from the Functional world.
Code Snippet:
// The OO Stuff
public class Tournament
{
public Bracket CommonBracket { get; set; }
public int NumofPlayers { get; set; }
public Bracket Winners { get; set; }
public Bracket Losers { get; set; }
public static Tournament newTour = new Tournament();
protected Tournament()
{
common = new Bracket();
Winners = new Bracket();
Losers = new Bracket();
}
}
public class Play
{
private void GenerateUI()
{
// Lambda Expression
Action< List< TextBox>> genUItextboxes =
(T)
=>
{
int yIndex = 5;
foreach (var item in T)
{
item.Size = new System.Drawing.Size(120, 15);
item.Location = new System.Drawing.Point(140, 40 + yIndex);
item.Name = "text" + iCount;
item.Text = "";
item.Visible = true;
this.Controls.Add(item);
iCount++;
yIndex += 20;
}
};
List
for (int i = 0; i < Tournament.newTour.NumofPlayers; i++)
{
nameList.Add(new TextBox());
}
genUItextboxes(nameList);
}
private void PopulateBrackets()
{
Bracket Initial = Tournament.newTour.CommonBracket;
//Declarative Query
var winner = from p in Initial.PlayersList where p.Name == winnerName select p;
}
}
* My main focus in this article was to outline the differences between the different paradigms.
The explanation given for each paradigm is very brief and doesn't cover much ground.
If you are interested in knowing more about each paradigm, click on their respective names in the above article, that'll take you to their corresponding wiki.